You are on page 1of 14

1a. Script to print passed arguments in a reverse order. for x do i="$x $i" done echo "$i" 1b.

Script to check whether file permissions are Identical or not. echo "Enter 2 FileNames " read a b set -- `ls -l $a` a1=$1 set -- `ls -l $b` a2=$1 if [ $a1 = $a2 ] then echo " Files are Identical " else echo " Properties of file $a : $a1 " echo " Properties of file $b : $a2 " fi

2a. Script to descend all directories & find maximum length of any file and print to output. echo -e "Enter a valid directory name : " read dir if [ -d $dir ] then find $dir -type f -exec ls -l {} \; | tr -s " " | cut -d " " -f 5,9 | sort nr | head -n 1 else echo "$dir is not a directory" fi 2b. Script to accept pathname and create all components in that path as directories. a1=`echo "$*" | tr "/" " "` for x in $a1 do mkdir $x cd $x pwd done

3a. Script to accept valid Log-In names and print their Home directories. for x in $* do grep "^$x" /etc/passwd > linux if [ $? -eq 1 ] then echo "$x is invalid username" else homedir=`cut -d \: -f 6 linux` echo -e "the homedir of $x : $homedir\n" fi done 4a. Script to display file properties of any desired file. echo " Enter The Filename " read f if [ -f $f ] then echo " Properties of the File $f : \n `ls -l $f`" else echo " File does not exist " fi

4b. Script to convert all filenames passed as arguments to Uppercase characters. if [ $# -eq 0 ] then echo "No Arguments" exit else for file in $* do if [ -e $file ] then fname=`echo $file|tr a-z A-Z` echo "The $file is converted to $fname " else echo "$file does not exit" fi done fi

5a. Script to display Hard Link & Soft Links to any desired file. if [ -f $1 ] then if [ $# -eq 1 ] then dir=. else dir=$2 fi node=`ls -i $1 | cut -d " " -f 1` echo "Hard links to $1:\n" find $dir -inum $node -print echo "Soft links to $1:\n" #find $dir -lname "$1" -print ls -lR $dir | sed -n "/^l.*$1$/p" | tr -s " " | cut -d " " -f 9-11 else echo "File does not exist" fi

5b. Script to accept Filenames as arguments and display their Creation/Modification time. if [ -f $1 ] then set -- `ls -l $1` echo " The file execution time : $6 $7 $8 " else echo "file not found" fi 6a. Script to display calendar with the current date replaced by * or ** depending on current date digits. echo "Current date is" echo `date` set -- `date` x=`date +%m` cal $x $6 > emp.dat y=$3 if [ $y -lt 10 ] then sed 's/[^0-9]'$y'[^0-9]/ * /' emp.dat else sed 's/[^0-9]'$y'[^0-9]/ ** /' emp.dat fi

6b. Script to find Smallest of 3 Number's. echo " Enter 3 No's " read a b c if [ $a -lt $b -a $a -lt $c ] then echo " Sa is Smallest " eleif [ $b -lt $a -a $b -lt $c ] echo " $b is Smallest" else echo " $c is Smallest " fi 7a. Script using 'expr' command to read a string and display suitable msg if it has < 10 characters. echo " Enter the String " read a len=`expr length $a` if [ $len -lt 10 ] then echo " Length is less than 10 characters " else echo " Length is more than 10 characters " fi

7b. Script to compute sum of numbers passed to it as arguments. num=$* res=0 while [ $num -ne 0 ] do ((rem=$num%10)) ((res=$res+$rem)) ((num=$num/10)) done echo "$res" 8a. Script to compute gross salary according to rules specified. echo "Enter the Basic salary : " read basic if [ $basic -lt 15000 ] then hra=`echo "scale=2; $basic*10/100" | bc` da=`echo "scale=2; $basic*90/100" | bc` else hra=500 da=`echo "scale=2; $basic*98/100" | bc` fi gsalary=`echo "scale=2; $basic+$hra+$da" | bc` echo " Gross Salary : $gsalary "

8b. Script to delete all lines containing a specific word in one or more files, passed as arguments. if [ $# -lt 1 ] then echo "usage : $0 filename" exit fi echo "enter the word" read word for file in $* do grep -iv "$word" $file | tee 1>/dev/null $file done 9a. Shell script to display suitable msgs depending upon Time( Good Morn, Aftr, Eve/Nit ) num=`date +%H` if [ $num -ge 1 -a $num -lt 12 ] then echo " Good Morning $LOGNAME " elif [ $num -ge 12 -a $num -lt 16 ] then echo "Good Afternoon $LOGNAME " else echo " Good Evening $LOGNAME " fi

9b. Script that accept a list of filenames as arguments and count & report occurrence of each word that is present in 1st argument file on all other argument files. for word in `cat $1` do for file in $* do if [ "$file" != "$1" ] then echo "The word frequency of $word in file $file is: `grep -iow "$word" $file|wc -w`" fi done done

10a. Script to determine the period for which any specific User is working on system. echo "enter the login name of the user" read name userinfo=`who | grep -w "$name"` if [ $? -ne 0 ] then echo "$name is not logged in yet" exit fi hrs=`echo "$userinfo" | cut -c 34-35 | head -1`

echo "$hrs" min=`echo "$userinfo" | cut -c 37-38 | head -1` echo "$min" hrnow=`date | cut -c 12-13` echo "$hrnow" minnow=`date | cut -c 15-16` echo "$minnow" hour=`expr $hrnow-$hrs|bc` minute=`expr $minnow-$min|bc` echo "mr/ms : $name is working since $hour hrs - $minute min"

10b. Shell script that determines Loggin-In of a User within 1min after he/she logs in. If User doesn't Log-In in1 min then terminate. echo "Enter the login name of the user" read lname period=0 echo "Enter the unit of time in mins" read min until who |grep -w "$lname">/dev/null do sleep 60 period=`expr $period + 1` if [ $period -gt $min ] then echo "$lname has not logged in since $min mins" exit fi done echo "$lname has now logged in"

11a. Script to accept 2 integers and compute the value of 1st No raised to the power of No. echo " Enter 2 Numbers : " read a b c=$b res=1 if [ $b -lt 0 ] then ((b=-1*$b)) fi while [ $b -gt 0 ] do ((res=$res*$a)) ((b=$b-1)) done if [ $c -gt 0 ] then echo $res else echo "scale=4; 1/$res" | bc fi

11b. Script to accept filename,starting & ending line as arguments & display data between specified lines. if [ $# -eq 3 ] then if [ -f $1 ] then if [ $3 ge $2 ] then sed -n $2,$3 p $1 else echo Invalid range fi else echo $1 does not exist fi else echo Pass 3 arguments fi

You might also like