hmp1 Shell
hmp1 Shell
if [ $# -ne 2 ]
then
echo "Usage - $0 x y"
echo " Where x and y are two nos for which I will
print sum"
exit 1
fi
echo "Sum of $1 and $2 is `expr $1 + $2`"
if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given" >&2
exit 1
fi
n1=$1
n2=$2
n3=$3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is Bigest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 is Bigest number"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 is Bigest number"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "All the three numbers are equal"
else
echo "I can not figure out which number is biger"
fi
1
#Q.3.Write script to print nos as 5,4,3,2,1 using while loop.#
Algo:
# 1) START: set value of i to 5 (since we want to start
from 5, if you
# want to start from other value put that value)
# 2) Start While Loop
# 3) Chechk, Is value of i is zero, If yes goto step 5
else
# continue with next step
# 4) print i, decement i by 1 (i.e. i=i-1 to goto zero)
and
# goto step 3
# 5) END
#
i=5
while test $i != 0
do
echo "$i
"
i=`expr $i - 1`
done
2
How to perform real number calculation in shell script and store
result to
third variable , lets say a=5.66, b=8.67, c=a+b?
a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
if [ $# -ne 1 ]
then
echo "Usage - $0 file-name"
exit 1
fi
if [ -f $1 ]
then
echo "$1 file exist"
else
echo "Sorry, $1 file does not exist"
fi
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
3
Q.15. Write script called sayHello, put this script into your
startup file called .bash_profile, the script should run as soon
as you logon to system, and it print any one of the following
message in infobox using dialog utility, if installed in your
system, If dialog utility is not installed then use echo
statement to print message : -
Good Morning
Good Afternoon
Good Evening , according to system time.
if [ $temph -lt 12 ]
then
mess="Good Morning $LOGNAME, Have nice day!"
fi
4
nouser=`who | wc -l`
echo -e "User name: $USER (Login name: $LOGNAME)" >>
/tmp/info.tmp.01.$$$
echo -e "Current Shell: $SHELL" >> /tmp/info.tmp.01.$$$
echo -e "Home Directory: $HOME" >> /tmp/info.tmp.01.$$$
echo -e "Your O/s Type: $OSTYPE" >> /tmp/info.tmp.01.$$$
echo -e "PATH: $PATH" >> /tmp/info.tmp.01.$$$
echo -e "Current directory: `pwd`" >> /tmp/info.tmp.01.$$$
echo -e "Currently Logged: $nouser user(s)" >>
/tmp/info.tmp.01.$$$
if [ -f /etc/redhat-release ]
then
echo -e "OS: `cat /etc/redhat-release`" >>
/tmp/info.tmp.01.$$$
fi
if [ -f /etc/shells ]
then
echo -e "Available Shells: " >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/shells`" >> /tmp/info.tmp.01.$$$
fi
if [ -f /etc/sysconfig/mouse ]
then
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e "Computer Mouse Information: " >> /tmp/info.tmp.01.$
$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e "`cat /etc/sysconfig/mouse`" >> /tmp/info.tmp.01.$$$
fi echo -e "Computer CPU Information:" >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
cat /proc/cpuinfo >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e "Computer Memory Information:" >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
cat /proc/meminfo >> /tmp/info.tmp.01.$$$
if [ -d /proc/ide/hda ]
then
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e "Hard disk information:" >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
5
-----" >> /tmp/info.tmp.01.$$$
echo -e "Model: `cat /proc/ide/hda/model` " >>
/tmp/info.tmp.01.$$$
echo -e "Driver: `cat /proc/ide/hda/driver` " >>
/tmp/info.tmp.01.$$$
echo -e "Cache size: `cat /proc/ide/hda/cache` " >>
/tmp/info.tmp.01.$$$
fi
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
echo -e "File System (Mount):" >> /tmp/info.tmp.01.$$$
echo -e
"---------------------------------------------------------------
-----" >> /tmp/info.tmp.01.$$$
cat /proc/mounts >> /tmp/info.tmp.01.$$$
rm -f /tmp/info.tmp.01.$$$
Q.20.Write shell script using for loop to print the following patterns on
screen
1
22
333
4444
55555
echo "Can you see the following:"
Q.20.Write shell script using for loop to print the following patterns on
screen
1
12
123
1234
12345
echo "Can you see the following:"
6
for (( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done
Q.20.Write shell script using for loop to print the following patterns on
screen
*
**
***
****
*****
echo "Stars"
Q.20.Write shell script using for loop to print the following patterns on
screen
*
**
***
****
*****
*****
****
***
**
*
echo "Stars"
7
do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
Q.20.Write shell script using for loop to print the following patterns on
screen
MAX_NO=0
clear
8
done
Q.20.Write shell script using for loop to print the following patterns on
screen
MAX_NO=0
clear
9
do
echo -n " ."
done
echo ""
done
Q.20.Write shell script using for loop to print the following patterns on
screen
MAX_NO=0
clear
10
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done
11