Shell Scripting Simple Programs
Shell Scripting Simple Programs
#Example 1
#! /bin/bash
echo date
#prints date
echo who
#prints who is logged into the system
#! /bin/bash
echo "User information for userid : $USER"
echo UID : $UID
echo HOME : $HOME
#arithmetic expression example
read x
read y
var3=`expr $x + $y`
# The expr command is used to evaluate a given expression and display its standard output.
echo The result is $var3
#! /bin/bash
var1=$[1+5]
echo $var1
v2=2
var3=`expr $var1 + $v2`
echo $var3
var3=`expr $var1 - $v2`
echo $var3
echo $var3
echo $var3
Notes:
There are three different quote characters with different behaviour. These are:
“ double quote, weak quote. If a string is enclosed in “ ” the references to variables (i.e $variable )
are replaced by their values. Also back-quote and escape \ characters are treated specially.
‘ single quote, strong quote. Everything inside single quotes are taken literally, nothing is treated as
special.
` back quote. A string enclosed as such is treated as a command and the shell attempts to execute it. If
the execution is successful the primary output from the command replaces the string.
#! /bin/bash
var1=100
var2=45
var3=$(echo "scale =4 ; $var1/$var2" | bc)
echo $var3
Notes:
This sets the scale to 4, meaning the result of 100/45 will be calculated with 4 decimal
places.
#! /bin/bash
#Area of a circle
read radi
pivalue=3.14159
echo $area