Sie sind auf Seite 1von 16

Unix Shell Programms

1. Write a program to search a file and directory

echo "enter name of file to be displayed" read fname if [ -f $fname -a -r $fname ] then cat $fname fi if [ -d $fname -a -r $fname ] then echo "enter file name to search in directory $fname" read temp find . -name $temp -type f -print find . -name $temp -type f -exec cat {} fi 2. Write a program to find two no.s addition & remainder

a=10 b=15 a=`expr $a + $b` echo "value of a+b is $a" b=`expr $a % $b` echo "value of remainder is $b" c=`expr $a \* $b` echo $c 3.Write a program to find whether a no is Armstrong or not clear echo "enter any number....." read n temp=$n rno=0 while [ $n -ne 0 ] do r=`expr $n % 10` rno=`expr $rno + $r \* $r \* $r` n=`expr $n / 10`

done if [ $temp -eq $rno ] then echo "it is an amstrong no.." else echo "it is not an amstrong no...." fi 4.program to demonstrates the case usage in Unix echo "enter a number from 1 to 3" read n case $n in 1)echo you entered 1;; 2)echo "you entered 2";; 3)echo you entered 3;; esac 5.====================== echo "enter a number "

read n case $n in 121)echo you entered 121;; 2)echo "you entered 2";; *)echo you entered wrong number;; esac case $1 in cat)echo you entered cat;; 2)echo "you entered 2";; *)echo you entered wrong number;; esac case $1 in cat | dog)echo you entered cat or bird;; [a-z])echo "you entered a...z";; [0-9])echo you have entered a number;; ?)echo you have entered a special symbol;; *)echo you entered wrong number;; esac

6.=============================== while true do stty -echo echo "enter password \c" read pw stty echo if [ $pw = shoban ] then echo correct password break else echo incorrect password fi done 7.===================== if [ -d $1 ] then

echo "file is a directory" else echo "file is not a directory \c" fi 8.========================== read source target if cp $source $target then echo "file copied succesfully" fi 9.================================== count=2 while [ $count -le 10 ] do echo $count count=`expr $count + 2`

done 10.=================================== if [ -f $1 ] then echo "file is a ordinary file" cat $1 else echo "file is not a ordinary file \007" fi 11.==================== for i do echo $i done for i in $* do echo $i

done for i do echo $i done for i in f* do echo $i cat $i sleep 2 done

12.====================== lname=$1 time=0 while true do

who | grep "$lname" > null if [ $? = 0 ] then echo $lname has logged in if [ $time -ne 0 ] then echo he is $time minutes late break else time=`expr $time + 1` sleep 6 fi done 13.================ count=1 while [ $count -le 10 ] do echo $count count=`expr $count + 1`

done 14.============================== count=1 while [ $count -le 10 ] do echo $count count=`expr $count + 2` done 15.=================================== echo "enter any string......." read str rev="" len=`echo $str | wc -c` len=`expr $len - 1` while [ $len -gt 0 ] do

rev=$rev`echo $str | cut -c $len` len=`expr $len - 1` done echo "reverse of a string is ...." $rev 16.======================================= clear echo "enter any number....." read n rno=0 while [ $n -ne 0 ] do r=`expr $n % 10` rno=`expr $rno \* 10 + $r` n=`expr $n / 10` done echo "reverse of digits is ..." $rno 17.====================================

echo "enter file name" read fname terminal=`tty` exec < $fname while read line1 do echo $line1 echo press any key read key < $terminal done 18.============================================== echo "enter string to search " read str echo "enter filename to search" read fname if grep $str $fname > \dev\null then echo "string is found"

else echo "string is not found" fi 19.================== cat $* shift 2 cat $* 20.======================= echo read echo read if [ then echo else echo "enter first string" str1 "enetr second string" str2 $str1 = $str2 ] "strings are equal" "strings are not equal"

fi 21==================================== clear echo "enter any number....." read n sum=0 while [ $n -ne 0 ] do r=`expr $n % 10` sum=`expr $sum + $r` n=`expr $n / 10` done echo "sum of digits is ..." $sum 22.========================================= echo "enter any name" read name

echo "contents of file are" cat $name 23.========================================== t=`echo $1 | tr [a-z] [A-Z]` echo "enter any string to convert from lower to upper..." read n str=`echo $n | tr [a-z] [A-Z]` echo $str echo $t 24.=========================================== echo enter any word read word case $word in [aeiou]*)echo word begins with small case vowel;; *[0-9])echo word ends with digit;; esac

25.*********programs using command line arguments********* if grep "$1" $2 then cat $2 else exit fi [or] grep $1 $2 > devnull && cat $2 exit

26

program to check whether the given file is empty or not***

if [ $# -eq 0 ] then echo "you must give filename" else if [ ! -s $1 ] then echo "file $1 not there" else fi fi

Das könnte Ihnen auch gefallen