04-Linux Shell Scripting
04-Linux Shell Scripting
Now the grand total is printed at the end. In above program BEGIN and END patters are
used. BEGIN instruct awk, that perIorm BEGIN actions beIore the Iirst line (Record) has
been read Irom database Iile. Use BEGIN pattern to set value oI variables, to print
heading Ior report etc. General syntax oI BEGIN is as Iollows
Svntax.
BEGIN
action 1
action 2
action N
}
END instruct awk, that perIorm END actions aIter reading all lines (RECORD) Irom the
database Iile. General syntax oI END is as Iollows:
END
action 1
action 2
action N
}
In our example, BEGIN is used to print heading and END is used print grand total.
#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
# Written by Vivek G. Gite <vivek@nixcraft.com>
#
# Latest version can be found at http://www.nixcraft.com/
#
# Q13
#
# Shell script to print contains of file from given line no to next
# given numberlines
#
#
# Print error / diagnostic for user if no arg's given
#
if [ $# -eq 0 ]
then
echo "$0:Error command arguments missing!"
echo "Usage: $0 start_line uptoline filename"
echo "Where start_line is line number from which you would like to
print file"
echo "uptoline is line number upto which would like to print"
echo "For eg. $0 5 5 myfile"
echo "Here from myfile total 5 lines printed starting from line no.
5 to"
echo "line no 10."
exit 1
fi
#
# Look for sufficent arg's
#
if [ $# -eq 3 ]; then
if [ -e $3 ]; then
tail +$1 $3 | head -n$2
else
echo "$0: Error opening file $3"
exit 2
fi
else
echo "Missing arguments!"
fi
#
# ./ch.sh: vivek-tech.com to nixcraft.com referance converted using
this tool
# See the tool at http://www.nixcraft.com/uniqlinuxfeatures/tools/
#
#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
# Written by Vivek G. Gite <vivek@nixcraft.com>
#
# Latest version can be found at http://www.nixcraft.com/
#
# Q4
#
if test $# = 3
then
case $2 in
+) let z=$1+$3;;
-) let z=$1-$3;;
/) let z=$1/$3;;
x|X) let z=$1*$3;;
*) echo Warning - $2 invalied operator, only +,-,x,/ operator
allowed
exit;;
esac
echo Answer is $z
else
echo "Usage - $0 value1 operator value2"
echo " Where, value1 and value2 are numeric values"
echo " operator can be +,-,/,x (For
Multiplication)"
fi
#
# ./ch.sh: vivek-tech.com to nixcraft.com referance converted using
this tool
# See the tool at http://www.nixcraft.com/uniqlinuxfeatures/tools/
#