0% found this document useful (0 votes)
165 views11 pages

hmp1 Shell

This document contains several examples of UNIX shell scripts with explanations: 1. A script that sums two numbers passed as command line arguments and prints the result. 2. A script that finds the largest of three numbers passed as arguments. 3. A script that prints the numbers 5 through 1 using a while loop. 4. A script that performs basic math operations like addition, subtraction etc. using a case statement. 5. A script that prints the current date, time, username and directory. The document also includes examples of shell scripts for real number calculations, checking if a file exists, printing lines from a file, welcome messages on login, system information, and printing patterns using for loops.

Uploaded by

Amit Srivastava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views11 pages

hmp1 Shell

This document contains several examples of UNIX shell scripts with explanations: 1. A script that sums two numbers passed as command line arguments and prints the result. 2. A script that finds the largest of three numbers passed as arguments. 3. A script that prints the numbers 5 through 1 using a while loop. 4. A script that performs basic math operations like addition, subtraction etc. using a case statement. 5. A script that prints the current date, time, username and directory. The document also includes examples of shell scripts for real number calculations, checking if a file exists, printing lines from a file, welcome messages on login, system information, and printing patterns using for loops.

Uploaded by

Amit Srivastava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIX SHELL SCRIPTS

# Q1.Script to sum to nos


#

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`"

# Q2. Script to find out bigest number


#
# Algo:
# 1) START: Take three nos as n1,n2,n3.
# 2) Is n1 is greater than n2 and n3, if yes
# print n1 is bigest no goto step 5, otherwise goto next
step
# 3) Is n2 is greater than n1 and n3, if yes
# print n2 is bigest no goto step 5, otherwise goto next
step
# 4) Is n3 is greater than n1 and n2, if yes
# print n3 is bigest no goto step 5, otherwise goto next
step
# 5) END

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

Q.4. Write Script, using case statement to perform basic math


operation as
follows
+ addition
- subtraction
x multiplication
/ division
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

Q.5.Write Script to see current date, time, username, and


current directory
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry `pwd`"

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"

Write script to determine whether given file exist or not, file


name is supplied as command line argument, also check for
sufficient number of command line argument

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

Write script to print contains of file from given line number to


next given number of lines. For e.g. If we called this script as
Q13 and run as
$ Q13 5 5 myf , Here print contains of 'myf' file from line
number 5 to next 5 line of that file.

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.

temph=`date | cut -c12-13`


dat=`date +"%A %d in %B of %Y (%r)"`

if [ $temph -lt 12 ]
then
mess="Good Morning $LOGNAME, Have nice day!"
fi

if [ $temph -gt 12 -a $temph -le 16 ]


then
mess="Good Afternoon $LOGNAME"
fi

if [ $temph -gt 16 -a $temph -le 18 ]


then
mess="Good Evening $LOGNAME"
fi

if which dialog > /dev/null


then
dialog --backtitle "Linux Shell Script Tutorial"\
--title "(-: Welcome to Linux :-)"\
--infobox "\n$mess\nThis is $dat" 6 60
echo -n " Press a key to
continue. . . "
read
clear
else
echo -e "$mess\nThis is $dat"
fi

Q.19. Write shell script to show various system configuration


like
1) Currently logged user and his logname
2) Your current shell
3) Your home directory
4) Your operating system type
5) Your current path setting
6) Your current working directory
7) Show Currently logged number of users
8) About your os and version ,release number , kernel version
9) Show all available shells
10) Show mouse settings
11) Show computer cpu information like processor type, speed etc
12) Show memory information
13) Show hard disk information like size of hard-disk, cache
memory, model etc
14) File system (Mounted)

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.$$$

if which dialog > /dev/null


then
dialog --backtitle "Linux Software Diagnostics (LSD) Shell
Script Ver.1.0" --title "Press Up/Down Keys to move"
--textbox /tmp/info.tmp.01.$$$ 21 70
else
cat /tmp/info.tmp.01.$$$ |more
fi

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:"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$i"
done
echo ""
done

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"

for (( i=1; i<=5; i++ ))


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

*
**
***
****
*****
*****
****
***
**
*

echo "Stars"

for (( i=1; i<=5; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done

for (( i=5; i>=1; i-- ))

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

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""

8
done

Q.20.Write shell script using for loop to print the following patterns on
screen

MAX_NO=0

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))

9
do
echo -n " ."
done
echo ""
done

echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"

Q.20.Write shell script using for loop to print the following patterns on
screen

MAX_NO=0

echo -n "Enter Number between (5 to 9) : "


read MAX_NO

if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then


echo "I ask to enter number between 5 and 9, Okay"
exit 1
fi

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done
###### Second stage ######################
##
##
for (( i=MAX_NO; i>=1; i-- ))
do
for (( s=i; s<=MAX_NO; s++ ))
do
echo -n " "
done

10
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done

echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"

11

You might also like