Sie sind auf Seite 1von 12

In this section, we will see some additional exercises for practice and

solutions of some command sets.


A8.2 COMPUTER PRACTICE LABORATORY -II

1. Write a shell script tom check whether a given number is positive or


not?
# Script to see whether argument is positive or negative
exit 1
fi
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

2. Write a shell script to check whether the given argument is positive


or not.
# Script to see whether argument is positive
if test $1 -gt 0
then
echo "$1 number is positive"
fi

3. Write a Shell script for Multiplication Table


#Script to test for loop
if [ $# -eq 0 ]
APPENDIX-8 A8.3

then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i = `expr $i \* $n`"
done

4. Write a Shell script to display a menu and create an user interface.


# Script to create simple menus and take action according to that
# Selected menu item
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
A8.4 COMPUTER PRACTICE LABORATORY -II

echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0 ;;
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esac
done

5. How to write shell script that will add two numbers, which are
supplied as command line argument, and if this two numbers are not
given show error and its usage.

#Linux Shell Script Tutorial


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`"
APPENDIX-8 A8.5

5. Write Script to find out biggest number from given three nos.
Numbers are supplies as command line argument. Print error if
sufficient arguments are not supplied.

# Linux Shell Script Tutorial


# 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 ]
A8.6 COMPUTER PRACTICE LABORATORY -II

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

6. Write script to print nos as 5,4,3,2,1 using while loop.


# To print numbers in reverse order:
# 1) START: set value of i to 5
# 2) Start While Loop
# 3) Check, Is value of i is zero, If yes goto step 5 else
# continue with next step
# 4) print i, decrement 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`
APPENDIX-8 A8.7

done

6. Write Script, using case statement to perform basic math operation


as follows
+ addition
- subtraction
x multiplication
/ division
#A simple calculator
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 invalid 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
A8.8 COMPUTER PRACTICE LABORATORY -II

7.Write Script to see current date, time, user-name, and current


directory
#To display current date, time, user-name and current working
directory
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current directory `pwd`"

7. Write script to print given number in reverse order, for eg. If no is


123 it must print as 321.
# Script to reverse given no
# Algo:
# 1) Input number n
# 2) Set rev=0, sd=0
# 3) Find single digit in sd as n % 10 it will give (left most digit)
# 4) Construct revrse no as rev * 10 + sd
# 5) Decrment n by 1
# 6) Is n is greater than zero, if yes goto step 3, otherwise next step
# 7) Print rev
#
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find reverse of given number"
echo " For eg. $0 123, I will print 321"
APPENDIX-8 A8.9

exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo "Reverse number is $rev"

8. Write script to print given numbers sum of all digit, For eg. If no is
123 it's sum of all digit will be 1+2+3 = 6.
# Script to reverse given no
# Algo:
# 1) Input number n
# 2) Set sum=0, sd=0
# 3) Find single digit in sd as n % 10 it
# 4) Construct sum no as sum=sum+sd
# 5) Decrment n by 1
# 6) Is n is greater than zero, if yes goto
# 7) Print sum
#
if [ $# -ne 1 ]
A8.10 COMPUTER PRACTICE LABORATORY -II

then
echo "Usage: $0 number"
echo " I will find sum of all digit for
echo " For eg. $0 123, I will print 6
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for numner is $sum"

9.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?
# To perform real number calculation in Linux Shell Script
a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
APPENDIX-8 A8.11

10.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
# To determine whether given file exist or not
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

11.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.
# Shell script to print contains of file from given line no to next
# given number lines
# Print error / diagnostic for user if no arguments given
#
if [ $# -eq 0 ]
then
A8.12 COMPUTER PRACTICE LABORATORY -II

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