Vous êtes sur la page 1sur 34

1) read -p 'Enter your name: ' name read -p 'Enter your age: ' age read -p 'Enter your

dept: ' dept read -p 'Enter your course: ' crs echo "Your name is $name" echo "You are $age years old" echo "You are in the $dept department" echo "You are currently pursuing the $crs course"

2) read -p 'Enter your enrollment number: ' eno read -p 'Enter your name: ' name read -p 'Enter name of three subjects: ' a b c read -p "Enter marks for $a: " m read -p "Enter marks for $b: " n read -p "Enter marks for $c: " o echo "Enrollment Number: $eno" echo "Name: $name" echo "Marks obtained in $a=$m" echo "Marks obtained in $b=$n" echo "Marks obtained in $c=$o"

3) a=12 b=23 c=34 d=45 e=56 echo "$a, $e, $b, $c, $d"

4) echo "On `date +"%D"`, the following users where logged in" > Rslt who >> Rslt cat Rslt

5) if test $# -eq 2 then if test -f $2 then echo "File $2 already exists" elif test -f $1 then cp $1 $2 echo 'Copying Complete' echo "Contents of $2:" cat $2 else echo "File $1 doesn't exist" fi else echo 'Two file arguments must be provided' fi

6) if test $# -eq 2 then if test -f $1 -a -f $2 then cat $1 >> $2 echo "$1 contents appended to $2" echo "$2 Contents:" cat $2 else echo "Please ensure both $1 & $2 exist." fi else echo 'Two file arguments must be provided' fi

7) if test $# -eq 1 then if test -f $1 then if test `ls -l $1 | cut -c 4,7,10` = 'xxx' then echo "$1 already has execute permission" else echo "Adding execute permission to $1" chmod a+x $1 ls -l $1 fi else echo "$1 doesn't exist" fi else echo 'Please enter a file name' fi

8) if test $# -eq 2 then if test ! -f $1 then echo "File $1 doesn't exist" elif test ! -d $2 then echo "Directory $2 doesn't exist" elif test -f $2/$1 then echo "$2/$1 already exists" else mv $1 $2/$1 ls -Rl $2 fi else echo 'Please enter a file and directory name' fi

9)

read -p 'Enter a directory name (to create): ' d if test -d $d then echo "Can't create $d since it already exists" else mkdir $d read -p 'Enter a filename (to create): ' fn echo 'Creating file. Please enter contents. Press Ctrl + d to terminate.' cd $d cat > $fn echo "Present path: `pwd`" echo "$fn contents: " cat $fn fi

10) read -p 'Enter your name: ' name echo "You are invited to a party on `date +"%D"`" echo "$name invited on `date +"%D"`" >> party

11) if test $# -eq 1 then if test ! -f $1 then echo "$1 doesn't exist" else echo "Links: `ls -l $1|cut -d" " -f2`" echo "Size: `cat $1|wc -c` bytes" if test -f $1.logname then echo "$1.logname already exists" else mv $1 $1.logname echo 'File Renamed' fi fi else echo 'Please enter a filename' fi

12) echo "Today is `date +"%A %d %B %Y"`"

10

13) i=1 cur=`pwd` while test $i -le 5 do pwd=`pwd` mkdir $pwd/d$i cd $pwd/d$i touch f1 f2 i=`expr $i + 1` done cd $cur ls -RF rm -R $cur/d1

11

14) a=5 b=3 echo "a+b: `expr $a + $b`" echo "a-b: `expr $a - $b`" echo "a/b: `expr $a / $b`" echo "a*b: `expr $a \* $b`" echo "a%b: `expr $a % $b`"

15) read -p "Enter roll no.: " rollno read -p "Enter name: " name read -p "Enter marks for 5 subjects: " m1 m2 m3 m4 m5 avg=`expr $m1 + $m2 + $m3 + $m4 + $m5` avg=`expr $avg / 5` echo 'Average =' $avg case `expr $avg / 10` in [789]|10) echo 'Distinction';; 6) echo 'First Division';; 5) echo 'Second Division';; [01234]) echo 'Fail';; esac

12

16) read -p 'Enter basic salary: ' bs if test $bs -lt 1500 then hra=`expr $bs \* 10 / 100` da=`expr $bs \* 90 / 100` else hra=500 da=`expr $bs \* 98 / 100` fi echo 'HRA =' $hra echo 'DA =' $da echo 'Gross Salary =' `expr $bs + $hra + $da`

17) read -p "Enter a year: " y if [ `expr $y % 4` -eq 0 -a `expr $y % 100` -ne 0 ] then echo 'It is a leap year' elif [ `expr $y % 400` -eq 0 ] then echo 'It is a leap year' else echo 'Not a leap year' fi

13

18) if test $# -eq 2 then if test -f $2 then echo "File $2 already exists" elif test -f $1 then cp $1 $2 echo 'Copying Complete' echo "Contents of $2:" cat $2 else echo "File $1 doesn't exist" fi else echo 'Two file arguments must be provided' fi

19) read -p 'Enter distance (km): ' km m=`expr $km \* 1000` cm=`expr $m \* 100` inch=`echo $cm /2.54 | bc` foot=`echo $inch / 12 | bc` echo "Meters: $m" echo "Feet: $foot" echo "Inches: $inch" echo "Centimeters: $cm"

14

20) read -p 'Enter length: ' l read -p 'Enter width: ' w echo 'Area: '`echo $l \* $w | bc` echo 'Perimeter: '`echo 2 \* $l + 2 \* $w | bc`

21) read -p 'Enter radius: ' rad echo 'Area: '`echo $rad \* $rad \* 3.14 | bc` echo 'Circumference: '`echo $rad \* 2 \* 3.14 | bc`

15

22) read -p 'Enter a number: ' n sum=0 m=0 o=$n while [ $n -ne 0 ] do sum=`expr $sum + $n % 10` m=`expr $m \* 10 + $n % 10` n=`expr $n / 10` done if [ $o -eq $m ] then echo 'It is a palindrome' else echo 'It is not a palindrome' fi echo 'Sum of its digits:' $sum

23) if [ -z $1 ];then echo 'Please enter a file as command line argument' elif [ ! -f $1 -a ! -d $1 ];then echo "$1 doesn't exist" else if [ -d $1 ];then echo "$1 is a directory" if [ -r $1 ];then echo "$1 is readable. Listing contents of directory" ls $1 fi if [ -w $1 ];then echo "$1 is writeable. Creating sub-directory & two files within" if [ -d $1/"$1SubDir" ];then echo 'Sub-directory already exists' else mkdir $1/"$1SubDir" fi
16

touch "$1/$1SubDir/f1" "$1/$1SubDir/f2" ls -R $1 fi else echo "$1 is a regular file" if [ -r $1 ];then echo "$1 is readable. Displaying file contents." cat $1 else echo "$1 is not readable." fi if [ -w $1 ];then echo "$1 is writeable. Appending my name & course." echo '#! Vineet MCA'>>$1 echo "$1 contents:" cat $1 else echo "$1 is not writeable." fi if [ -x $1 ];then echo "$1 is executable. Executing..." sh $1 fi fi fi

17

24) ch='y' echo 'Menu:' echo '1. Print present working directory' echo '2. List contents of user directory' echo '3. Long listing of user directory contents' echo '4. Create a new file' echo '5. Move a file to parent directory' echo '6. Rename a file' echo '7. Create a sub-directory' while [ $ch = 'y' ] do echo 'Chose an option (enter respective number):' read ch case $ch in 1) pwd ;; 2) ls $HOME ;; 3) ls -l $HOME; echo 'Saving list into file lsLong'; ls -l $HOME > lsLong ;; 4) read -p 'Enter a file name ' fn echo "Enter contents for $fn. Press Ctrl + d once done." cat > $fn ;; 5) read -p 'Enter a file name to move ' fn if [ -f $fn ]; then mv $fn ../$fn echo 'File moved to parent directory' else echo 'File does not exist' fi ;; 6) read -p 'Enter a file to rename ' fn read -p 'Enter new name ' fn2 if [ ! -f $fn ]; then echo "$fn does not exist" elif [ -f $fn2 ]; then echo "fn2 already exists" else mv $fn $fn2 echo 'File renamed' fi ;; 7) read -p 'Enter new sub-directory name ' dir if [ -d $dir ]; then echo "$dir already exists" else mkdir $dir echo 'Sub-directory created' fi ;; *) echo 'Please enter a number ranging from 1 to 7' ;;
18

esac read -p 'Continue (Enter y/n)? ' ch done

19

25) flag=1 echo '1. Check contents of /etc/passwd' echo '2. List of users who have currently logged in' echo '3. Present working directory' echo '4. Exit' while [ $flag -eq 1 ] do echo 'Chose an option (enter 1-4)' read ch case $ch in 1) cat /etc/passwd;; 2) who;; 3) pwd;; 4) flag=0;; *) echo 'Chose an option between 1 & 4';; esac done

20

26) read -p 'Enter a character: ' ch if [ `echo $ch|cut -c2-` ] then echo 'You have entered more than one character' else echo 'You have entered: ' case $ch in [a-z]) echo 'A lower case letter';; [A-Z]) echo 'An upper case letter';; [0-9]) echo 'A digit';; *) echo 'a special character' esac fi

27) read -p 'Enter a word: ' w if [ `echo $w|grep ^[aeiouAEIOU]` ] then echo 'Begins with a vowel' if [ `echo $w|grep ^[A-Z]` ] then echo 'Upper case vowel' else echo 'Lower case vowel' fi
21

elif [ `echo $w|grep ^[0-9]` ] then echo 'Begins with a digit' else echo 'Begins with a consonant' fi if [ `echo $w|grep [0-9]$` ] then echo 'Word also ends with a digit' fi

28)

echo 'Checking if word starts with a/s/z & ends with t/p/l or is of four characters' read -p 'Enter a word: ' word n=`echo -n $word|wc -c` case `echo $word|cut -c1,$n` in at ) echo 'Starts with a & ends with t';; sp ) echo 'Starts with s & ends with p';; zl ) echo 'Starts with z & ends with l';; esac if [ `echo $word|grep ^....$` ] then echo 'Word is of 4 characters' fi

22

29)

hr=`date +"%H"` if [ $hr -ge 17 ] then echo 'Good Evening' elif [ $hr -ge 12 ] then echo 'Good Afternoon' else echo 'Good Morning' fi

23

30) if [ $# -eq 3 ] then if [ $1 -lt $2 2>/dev/null ] then echo "Hello $3, here is the month you asked for:" cal $1 $2 else echo 'Please enter proper numerical values for month & year' fi else echo 'Please enter a month, year & name' fi

31) echo 'Enter what you would like to be reminded of' echo 'Terminate input with ctrl + d' cat>.rem echo 'Enter day of the week on which reminder alerts you' echo 'Note: sun=0' read day if [ $day -ge 0 -a $day -le 6 ] then dateS=`date +"%d"` dateE=`expr $dateS + 6` mon=`date +"%m"` term=`tty` echo "00 12 $dateS-$dateE $mon $day cat.rem>$term">c
24

crontab c echo 'You will be reminded at 12:00 pm on that day' rm c else echo 'Please enter a proper integer value for day of the week' fi

32) if [ $# -eq 0 ]; then echo 'Please choose an option: 1/2' echo '1. Sum of first 9 integers' echo '2. Sum of 5 numbers' read ch case $ch in 1 ) read -p 'Enter a nine digit integer: ' nine if [ `echo $nine|grep -E "^[0-9]{9}$"` ]; then sum=0 while [ $nine -ne 0 ]; do sum=`expr $sum + $nine % 10` nine=`expr $nine / 10` done echo 'Sum: ' $sum else echo 'Please enter a nine digit integer' fi;; 2 ) echo 'Enter 5 integers' read a b c d e echo 'Sum: ' `expr $a + $b + $c + $d + $e`;; * ) echo 'Please type either 1 or 2' esac elif [ $# -eq 5 ]; then echo "Sum of five integers are: "
25

expr $1 + $2 + $3 + $4 + $5 else echo 'Please enter either 0 or 5 integers' fi

33) if [ $# -eq 2 ] then grep $1 $2 else echo 'Please enter a search string and a filename as command line args' fi

26

34)

if [ $# -eq 2 ] then grep $1 $2>/dev/null if [ $? -ne 0 ] then echo 'Pattern not found' else echo 'Pattern found' fi else echo 'Please enter a search string and a filename as command line args' fi

if [ $# -eq 2 ] then grep $1 $2>/dev/null if [ $? -ne 0 ] then echo 'Pattern not found' else echo 'Pattern found' fi else echo 'Please enter a search string and a filename as command line args' fi

27

35)

if [ $# -eq 3 ] then grep $1 $2 > $3 echo "Search results saved into $3" else echo 'Please enter a search string and 2 filenames as command line args' fi

36)

if [ $# -eq 1 ] then if [ -f $1 ] then echo "$1 is a file" if [ -r $1 ]; then echo 'It is readable' fi if [ -w $1 ]; then echo 'It is writeable' fi if [ -x $1 ]; then echo 'It is executable' fi elif [ -d $1 ] then echo "$1 is a directory" fi else echo 'Please provide a filename as a command line argument' fi

28

37)

oldExt=.txt newExt=.doc files=`ls|grep -E $oldExt$` if [ $? -eq 0 ] then echo "$oldExt files changed to $newExt" echo 'Files affected: ' for i in $files do echo $i j=`basename $i $oldExt` mv $i $j$newExt done else echo "No $oldExt files located" fi

29

38) echo 'Saving todays date without time into file "dateToday"' date +"%a, %b %d, %Y">dateToday

39) while : do echo 'Current directory contents:-------------------------' ls echo 'Current disk status:--------------------------------' df echo 'Currently logged in users:--------------------------' who echo 'Next update in 30 seconds---------------------------' sleep 30 done

30

40)

if [ $# -eq 2 ] then if [ ! -f $1 -o ! -f $2 ] then echo "Please ensure $1 & $2 exist" else cmp $1 $2>/dev/null if [ $? -eq 0 ] then echo 'Both files contain the same content' echo "Hence $2 is deleted" rm $2 else echo 'Contents of both files are different' fi fi else echo 'Please provide two filenames' fi

31

41) if [ ! -d Old -o ! -d CProgs ] 1then echo 'Please ensure directories Old & CProgs exist' else for i in `ls *old *.c` do if [ `echo $i|grep old$` ] then mv $i Old/$i echo "$i moved to Old" elif [ `echo $i|grep .c$` ] then mv $i CProgs/$i echo "$i moved to CProgs" fi done fi

32

42) for i in * do if [ ! -d $i ] then mv $i $i.$$ fi done echo "All files renamed with .$$ as extension"

33

43) if [ -d mydir ] then echo 'Directory mydir exits' echo 'There are total' `ls mydir|wc -w` 'files in it' else mkdir mydir echo 'Directoy mydir does not exist, hence it has been created' fi for i in $@ do if [ -f $i ] then echo "$i already exists" else echo "$i does not exist" echo "$i is now created in direcoty mydir" touch mydir/$i fi done

34

Vous aimerez peut-être aussi