Basic Shell Programming

Embed Size (px)

Citation preview

EX No: DATE:

BASIC SHELL PROGRAMMING

Roll No: 312211114111 Name: VAIBHAV PRAKASH

1. Write a Shell Script to display the following shell environmental variables like User, Login name, Present Working Directory, OS type and Shell. $cat >shell1.sh echo "User Name : $USER echo -n "Login Name : whoami echo -n "Present Working Directory : $PWD echo "Operating System : $OSTYPE echo "Shell Programming" $sh shell1.sh Output:User Name : SSN Student Login Name : mech111 Present Working Directory : /home/mech111/shell Operating System : Solaris Shell Programming 2. Write a Shell Script to get your name as input from the user and display the output with time. $cat >shell1.sh echo -n "Enter your Name : " read nm echo "Welcome $nm" tm=$(date +"%H:%M:%S") echo "Now the time is $tm" $sh shell1.sh

Output :Enter your Name : mech111 Welcome mech111 Now the time is 10:34:55 3. Write a Shell Program to find the area of the circle and rectangle. $cat >shell1.sh echo "Enter the radius : " read r echo "Area of the circle is" `expr 3.14 \* $r \* $r` echo "Enter the length :" read l echo "Enter the breadth :" read b echo "Area of the rectangle is" `expr $l \* $b` $sh shell1.sh Output :Enter the radius : 5 Area of the circle is 78.50 Enter the length : 10 Enter the breadth : 20 Area of the rectangle is 200 4. Write a Shell Program to convert Fahrenheit to Celsius. $cat >shell1.sh echo "Enter the temperature in fahrenheit :" read f echo "Temperature in celsius = " `expr ($f - 32) \*0.556` $sh shell1.sh

Output :Enter the temperature in fahrenheit : 98 Temperature in celsius = 36.696 5. Write a Shell Program to work out the following commands : list directory, current working directory, list of login users, count of *.sh files and systems date. $cat >shell1.sh echo "Listing Directory" ls echo "Current Working Directory" pwd echo "List of users logged on" who -q echo "Number of *.sh files" ls *.sh | wc -m echo "System's Date" date +"%d/%m/%Y" $sh shell1.sh Output :Listing Directory a.sh s1 s10 s2 s3 s4 s5 s6 s7 s8 s9 Current Working Directory /exam/mech111/shell1 List of users logged on mech111 # users=1 Number of *.sh files 5 System's Date 02/04/2012 6. Write a Shell Script to get two filename as command line arguments. Check

the correct argument count supplied in command line, else produce error message and return. Display the filenames. If both file exists then append the content of second file into first file. $cat>shell1.sh if [$# -eq 2]; then echo Filenames are $1 ,$2 if [-f $1 a f $2 ] then echo File 1 `cat $1` echo File 2 `cat $2` cat $1>>$2 echo File 2 `cat $2` else echo Sorry! Files do not exist! fi else echo Sorry incorrect argument! fi $sh shell1.sh input.txt readtxt.txt Output:Filenames are input.txt readtxt.txt File1 Hello File2 Good Morning File2 Good Morning Hello 7. Write a Shell Script to get 2 filename as command line input. Check for correct count. If both the files exist then Sort the content of both the files and merge it into third filename obtained from the user. $cat>shell1.sh if [$# -eq 2 ] then if [-f $1 a f $2 ] then echo Enter the name of the file in which the sorted contents are to be written read name echo Contents of $1 `cat $1` echo Contents of $2 `cat $2` sort $1 $2 >$name cat $name else echo Sorry! Files do not exist fi else echo Incorrect Argument count fi $sh shell1.sh input.txt readtxt.txt

Output:Enter the name of the file in which the sorted contents are to be written Opfile Contents of input.txt SSN INSTINCTS Contents of readtxt.txt CEG TECHOFES SSN INTINCTS CEG TECHOFES 8. Write a Shell Program to get the filename , lower limit and upper limit as command line arguments. Check for proper count. Display the lines from lower to upper limit of the given file Ex : sh show.sh file1.c 7 10 ---(7 to 10 lines in file1.c should be displayed) $cat >shell1.sh if [$# -eq 3] then c=`wc l $1|cit d f1` if [$c gt $3 a $3 gt $2 ] then c=`expr $3 - $2 + 1` head n $3 $1|tail n $c else echo Invalid Arguments fi else echo Invalid fi $sh shell1.sh opfile 4 7 Output:Gold Green Lavender Orange 9. Write a Shell Script to get a filename and pattern to be searched as command line arguments. Consider file contains student names and branch details. Display the no. of CSE students. Display the details of pattern students. (pattern can be either name or branch). Display the non-cse students information. $cat>shell1.sh if [$# -eq 2] then if [-f $1] then echo Number of CSE students `grep c i CSE $1` echo Details of matching Students `grep $2 $1` echo Details of Non-CSE studnets `grep v CSE $1` else echo File doesnt exist

fi else echo Invalid fi $sh shell1.sh input.txt MECH Output:Number of CSE students 2 Details of Matching students Simpsons 9 MECH 98 Details of Non Matching students Raj 8 BME 11 10. Write a Shell Program to get a file as input from user. Check whether file exits and it has write permission. If so delete the lines which contains the wordunix.

$cat>shell1.sh if [-f $1] then if [-w $1] then echo Contents; cat $1 grep v unix $1>newfile mv newfile $1 echo Contents cat $1 else echo Not writable fi else echo Doesnt exist fi $sh shell1.sh input.txt Output:Contents This is SSN Unix rocks! Contents This is SSN