You are on page 1of 28

Instructions

1. 2. 3. 4. Practicals may be performed through telnet or on linux machines or Virtual managers. Practicals cannot be performed on LIVE CD versions of linux. Try each command with at least THREE options (If are). In Shell programming practicals LET statement can also be used instead of expr. e.g. i=`expr $i + 1` this can be written simply with LET statement as LET i=i + 1 5. Lecturer can develop his/her own assignments. 6. In Practical exam Slips may contain ANY combination of commands and/or shell programmes.

Practical 1: Study of Basic and advanced Linux Commands


Assignment 1: Study of Commands-I (Assignments 1.1.1 to 1.1.4) Try the following commands with atleast three options: date, cal, cat, cp, rm who, ps, history, tty, stty, man, help, echo, pwd, dir, cd, mkdir, rmdir, mv Assignment 2: Study of Commands-II (Assignments 1.2.1 to 1.2.10) Try the following commands with atleast three options: file, lp, head, tail, more(less), wc, tr, sort, split, chmod, find, redirection commands

Practical 2: Study of Filter Commands & Editors Assignment 1: Study of Filter Commands-I (Assignments 2.1.1 to 2.1.6) Try the following commands with atleast three options: od, cmp, comm, diff, cut, paste, uniq, grep, egrep, fgrep, nl, ln. Assignment 2: Study of Editors (Assignments 2.2.1 to 2.2.3) Vi, sed, awk Practical 3: Basic Shell Scripting (Assignments 3.1 onwards) Practical 4: Advanced Shell Scripting (Assignments 3.1 onwards)

Assignment 1.1.1: Write and execute the commands for the following: i. Create three empty files with the name a1, b2, c3. ii. Create three files with the name f6, g7, h8 using cat command with some meaningful contents with atleast five lines each. iii. Copy contents of f6 to a1, g7 to b2, h8 to c3. iv. Create two directories with the name dd1, dd2. v. Copy the files a1 and b2 to the directories dd1.Copy the files f6, g7 to the director dd2. vi. Remove the directory dd2 along with its contents. vii. Rename the files a1 and b2, by newa1 and newb2. viii. Display the login name of all users currently logged in the system. Assignment 1.1.2: Write and execute the commands for the following: Create a file product1<seatno> with following fields and mentioned type of values: Product code Product name Price Quantity character character Numeric Numeric

i. Add at least 5 records in product1<seatno>. Fields are separated by || . Make a copy of


product1<seatno> into product2<seatno>. ii. Create a subdirectory COMP1<seatno>. Change to this directory & copy two files product1<seatno> and product2<seatno>. iii. Make a copy of file product1<seatno> into file product3<seatno>. Display their inode numbers. iv. Rename product3<seatno> with COMP2<seatno>. Assignment 1.1.3: (a) Write and execute the commands for the following: i. Create a copy of file item1<seatno> into file item2<seatno>. ii. Count number of lines, words & characters in a file item1<seatno>. (b) Write and execute the commands to change the directory to /bin and do the following: i. list all filenames. ii. list all filenames with one screen at a time. iii. list all filenames with 2 characters, 3 characters. iv. List all filenames starting with vowel. v. List all filenames with the last character as a or b or c or d. vi. List all filenames with exactly three characters in which the second character is a vowel. Assignment 1.1.4:

Write and execute the commands to change the directory to /etc and do the following: i. List the contents of the directory. ii. List the contents of the directory along with all hidden files. iii. List all files with their attributes and file permissions. iv. List all files identifying directories and executable files. v. Give the file listing displayed in columns. vi. Give the file listing in reverse order.

Assignment 1.2.1: Write and execute the commands for the following: i. Display /usr/sbin one screen at a time ii. Display /usr/sbin beginning with a lowercase c. iii. Display /usr/sbin in capital letters beginning with a lowercase c . iv. Display and count all the lines in the file /etc/mime.types v. Display the last 10 lines of /etc/mime.types vi. Display the first 25 lines of /etc/mime.types vii. To f.ind the files with their type in the following directories: /etc /usr/bin viii.To find out how many files are in the /usr/bin directory. Assignment 1.2.2: Construct the commands and execute them to i. Create a file named fsp<seat_no> having the listing of atleast 50 lines (e.g, listing of /usr/sbin or /usr/bin or /etc or can create your own). ii. Display first 2 lines of fsp<seat_no> and convert all the characters into capital letters. iii. Display the last 15 lines of fsp<seat_no>. iv. Display the lines starting with a vowel. v. Split the file fsp<seat_no> into subparts each having at most 20 lines and display the contents of these subparts and count the number of lines in them. vi. Split the file fsp<seat_no> into three subparts named fspaa, fspab, fspac and display the contents of these files and count the number of lines in them. Assignment 1.2.3: Write and execute the commands for following: i. Create a subdirectory new<seatno>. Change to this directory & create two files new1<seatno> and new2<seatno> with two different contents with atleast five lines each. ii. Append the contents of new1<seat_no> to new2<seat_no>. Redirect the output to the file new3<seat_no>. iii. Create a directory tree dir1/dir2/dir3 in one command. Assignment 1.2.4 : Write and execute the commands to create a file with the name Stud<roll_no> with the following fields separated by a blank space having the below mentioned values: Field RollNo First Name Last Name Date of Birth Marks

Values

Numeric

Character

Character

dd-mm-yy

Numeric out of 600

i Insert at least five appropriate records and do the following: ii Sort the data on first names only. iii Sort the data on the Marks only iv Prepare a ranked merit list with students first and last name only and store in the file Merit<roll_no> and display its contents. Assignment 1.2.5: Construct the commands and execute them to i Create a file named fsp<seat_no> having the listing of atleast 50 lines (e.g, listing of /usr/sbin or /usr/bin or /etc or can create your own). ii Display first 2 lines of fsp<seat_no> and convert all the characters into capital letters. iii Display the last 15 lines of fsp<seat_no>. iv Display the lines starting with a vowel. v Split the file fsp<seat_no> into subparts each having at most 20 lines and display the contents of these subparts and count the number of lines in them. vi Split the file fsp<seat_no> into three subparts named fspaa, fspab, fspac and display the contents of these files and count the number of lines in them. Assignment 1.2.6 : Write and execute commands to create a file named name<roll_no> with fields (first name, second name, last name, salary) separated by : .Insert at least five appropriate records in above file. Perform following sort operations: i Sort on first names only. ii Display only those records whose first names start with a vowel. iii Sort on last names only. iv Display the names with salary above 10000 and and add two more records and redirect the output to the file named namenew<roll_no>. Assignment 1.2.7: Write and execute the commands for the following: i. Create a file named fsp<seat_no> having the listing of atleast 50 lines (e.g, listing of /usr/sbin or /usr/bin or /etc or can create your own). ii. Display first 2 lines of fsp<seat_no> and convert all the characters into capital letters. iii. Display the last 15 lines of fsp<seat_no>. iv. Display the lines starting with a vowel. v. Split the file fsp<seat_no> into subparts each having at most 20 lines and display the contents of these subparts and count the number of lines in them. vi. Split the file fsp<seat_no> into three subparts named fspaa, fspab, fspac and display the contents of these files and count the number of lines in them. Assignment 1.2.8 : Write and execute the commands for the following: Create a file with the name Stud<seat_no> with the following fields separated by a blank space having the below mentioned values: Field RollNo First Name Last Name Date of Birth Marks Values Numeric Character Character dd-mm-yy Numeric out of 600 i. Insert at least five appropriate records and do the following: ii. Sort the data on first names only. iii. Sort the data on the Marks only iv. Prepare a ranked merit list with students first and last name only and store in the file Merit<seat_no> and display its contents.

Assignment 1.2.9: (a) Write and execute commands for the following: Create a file item1<seatno> with following fields having the mentioned type of values Item code Item name Unit of measure Price Quantity Numeric Character Character Numeric Numeric i. Add at least five records in item1<seatno>. Fields are separated by : . ii. Sort them according to their prices. iii. Display the line containing the highest quantity. iv. Sort the records according to their names (b) Create a file named student<seatno> with fields (Rollno, Student name, Marks). Fields are separated by colon (:). Write a shell script to perform the following options on that file: Print the content of the file. Rename the file. Delete the given file. If above three are not then print error message. Assignment 1.2.10: Write down the commands and execute them for the following: a. Create a file called test<seat_no>. Create a hard link called h_test and a symbolic links_test. Find out the inode number of the files. b. Remove the original file test. Can you still get the contents of the original file? c. Display the contents of the two files h_test and s_test. Justify the output.

Assignment 2.1.1: Write and execute commands 1. To create a file named fod1 with some contents having the following contents and display it in (i) octal form only and (ii) octal form along with its text contents. 2. To create two files named fcmp1 and fcmp2 which consists of at least five lines with two or three similar lines. Construct the commands using cmp (i) to check whether the files differ (ii) to compare the two files byte by byte. Assignment 2.1.2 : (a) Create two files named fcmp1 and fcmp2 which consists of at least five lines with two or three similar lines. Construct the commands 1. using diff : -(i) to display the lines which are common to both , the lines which are not common to both (ii) to display the difference in context output format (iii) to display the unified output format. 2. Using comm. : - (i)to compare the files fcmp1 and fcmp2 (ii)to display the lines which are unique to fcmp1 and fcmp2 (iii)to display the lines which are common to the fcmp1 and fcmp2. Assignment 2.1.3 : Create two files named fcmp1 and fcmp2 which consists of at least five lines with two or three similar lines. Construct the commands 1. Using nl to give the line numbers to lines in fcmp1. 2. Using cat to append the fcmp1 to fcmp2 and sort this appended fcmp2 and store it in a file named funiq1. 3. Using uniq (i) to remove the duplicate lines in funiq1. (ii) to count the duplications and prepend number to each line (iii) to display the duplicate lines only

(iv) to display unique lines only. Assignment 2.1.4 : Write and execute the commands to i. Create a file fcmp1<seat_no> with six lines containing six names. ii. Add two more names and save the contents in fcmp2<seat_no>. iii. Sort the contents of file fcmp1<seat_no> . iv. Display the names, which are common to fcmp1 <seat_no>, and fcmp2<seat_no>. v. Display the difference between fcmp1 <seat_no> and fcmp2<seat_no>. vi. Append the contents of fcmp1 <seat_no> to fcmp2<seat_no> and store it in the file fcmp3<seat_no>. vii. Sort the contents of fcmp3<seat_no> and display the contents without any duplicate lines. Assignment 2.1.5 : (a) Create two files with at least three fields(columns) each with the names fcut1, fcut2 and do the following: 1. Cut first two columns from fcut1 and store the contents in the file cutlist1 and cut the second and the third column from the fcut2 and store it in cutlist2. 2. Paste the contents of cutlist2 to contents of cutlist1. 3. Translate the first three lines into capital letters using tr command.

Assignment 2.1.6: (a) Create the file with the name gre2 and the following contents: The grep is an acronym for globally search a regular expression and print it. The command searches the specified input globally for a match with the specified pattern and displays it. While forming the pattern to be searched we can use shell metacharacters, or regular expressions as professional unix users call them. Write and execute the commands to 1. Search for the word line and display the lines containing it. 2. Search for the word the or The in both the files gre1 and gre2 and display the lines containing it. 3. Search for 4 letter words in gre1 and gre2 whose first character is p and last character is n . 4. Display the lines, which end with the characters from s to z from gre1 and gre2.

Assignment 2.2.1: Create a file with the name fvi1 with atleast ten lines.Perform the vi commands to the following cursor movements: -3 words to the right -4 words to the left -Bottom of screen -Beginning of current line -Beginning of the file -End of current line Assignment 2.2.2: Create a file course<seat_no> with following fields and the values of corresponding type: Course code Course name Batch code Duration (in days) Fees Character Character Character Numeric Numeric Fields are separated by ":" (colon). Open a file course using vi editor. Write and execute vi

commands for the following: i. Move three lines down at a time. ii. Delete the whole line. iii. Copy first line of file course and paste it so that it becomes the last line iv. Search for a string Linux. v. Go to line number 10. vi. Scroll the full page backward. vii. Move to first word of line. viii. Delete trailing spaces in the current line. ix. Search for the pattern Unix and replace it with Linux. x. Delete previous character from current cursor position. Assignment 2.2.3: Use sed editor for the following: Create a file with five records with the name fsed1 and do the following using sed command: 1. Display first three lines. 2. Display the last line. 3. Display the third and fourth line. 4. Insert two more records and save the new file as newsed. 5. Delete the last two records from the file newsed. Assignment 3.1:

Write a shell script which accept a single argument and indicate whether this argument is digit, lowercase letter, uppercase letter or special character.

Assignment 3.2:

Write and execute a shell script to read marks in five subjects and find the percentage per Also give the grades as follows: i. If the per < 35 then GRADE = FAIL ii. If the 35 >= per < 45 then GRADE = THIRD iii. If the 45 >= per < 60 then GRADE = SECOND iv. If the 60 >= per < 75 then GRADE = FIRST v. If the per >= 75 then GRADE = DISTINCTION
Assignment 3.3: Create a c- shell script to print summation of following series: 1! + 2! + 3! + ----- + N!.

Shell Programmes
Shprog1 echo Enter the values of a,b,c read a b c scale = 6 echo you have entered echo $a $b $c echo their sum and product is x = `echo $a + $b + $c | bc l` y = `echo $a\*$b\*$c | bc l` echo $x $y

Shprog2 #THIS PROGRAM DEMONSTRATES THE USE OF if echo enter any number read n if test $n gt 0 then echo $n is positive else echo $n is negative fi Shprog4 #THIS IS PROGRAM FOR IFELSE WITHOUT TEST echo enter the basic salary read b scale = 2 if [$b lt 1500] then hra=`echo $b\*10/100|bc l` da=`echo $b\*90/100|bc l` else hra=500 da=`echo $b\*98/100|bc l` fi g=`echo $b+$hra+$da|bc l` echo gross salary = $g Shprog5 echo ENTER THE FILE NAME read fname if [ -f $fname] then echo THE FILE EXISTS else echo FILE WITH THIS NAME DOES NOT EXIST fi Shprog6 #PROGRAM FOR STRING COMPARISON echo ENTER THE TWO STRINGS read str1 str2 if [ $str1 = $str2 ] then echo THE STRINGS ARE IDENTICAL else echo STRINGS ARE NOT IDENTICAL fi Shprog7 #Program to check whether file has a permission to write

echo Enter the file name read fname if [ -w $fname ] then echo Type text to append. To stop press Ctrl D. cat >> $fname else echo The file has no write permission fi Shprog8 #Program to give grades echo Enter the marks in the five subjects scale=2 read m1 m2 m3 m4 m5 per=`expr $m1 + $m2 + $m3 + $m4 + $m5` per=`expr $per / 5` echo $per if [ $per lt 35 ] then echo GRADE = FAIL fi if [ $per ge 35 a $per lt 45 ] then echo GRADE = THIRD fi if [ $per ge 45 a $per lt 60 ] then echo GRADE = SECOND fi if [ $per ge 60 a $per lt 75 ] then echo GRADE = FIRST fi Shprog9 #Program to check whether the number is +ve or ve echo Enter the number read a if [ $a lt 0 ] then echo a is negative elif [ $a gt 0 ] then echo a is positive else echo a is neither +ve nor ve fi Shprog10 #Program to print the day of the week using casein

echo Enter the day number read num case $num in 4. echo Sunday;; 5. echo Monday;; 6. echo Tuesday;; 7. echo Wednesday;; 8. echo Thursday;; 9. Friday;; 10. Saturday;; *) echo Enter the number bet 1 to 7;; esac Shprog11 #Program to find the type of the character entered echo Enter any character read c case $c in [a-z]) echo Small Case letter;; [A-Z]) echo Capital letter;; [0-9]) echo digit;; ?) echo special symbol;; *) echo more than one character, reenter;; esac Shprog12 #Program to find the pattern echo Enter the word read str case $str in [aeiou]*) echo The word begins with a vowel;; [0-9]*) echo The word starts with a digit;; *[0-9]) echo The word ends with a digit;; ????) echo The word entered is 4 lettered word;; *) echo The word entered is either starts with a consonant or incorrect input;; esac Shprog13 #Program to find the type of the file echo Enter the name of the file read fname case $fname in *.c) echo its a c prog file;; *.out) echo it is an output file;; *) echo not sure;; esac Shprog14 #Menu driven program echo enter echo 1 to see the contents of /etc/passwd

echo 2 to see list of users echo 3 to see present working directory echo 4 exit echo enter your choice read n case $n in v. ls /etc;; vi. ls /home;; vii. pwd;; viii.exit;; *) echo enter the choice as 1,2,3or4;; esac Shprog15 #Program to print first n numbers and their sum:while loop i=1 sum=0 while [ $i le 10 ] do echo $i sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is= $sum echo end of prog15 Shprog16 #Program to call another file echo hello!! echo date is date echo call prog15 bash shprog15 echo end of the prog16 Shprog17 #Program to print first n numbers and their sum:dountil i=1 sum=0 until [ $i gt 10 ] do echo $i sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is= $sum Shprog18 #program: for loop sum=0 for i in 1 2 3 4 5 6 7 8 9 10 do sum=`expr $sum + $i`

echo $i i=`expr $i + 1` done echo The sum is= $sum Shprog19 #Program to display the directories and files echo The directories are echo for file in * do if [ -d $file ] then echo $file fi done echo The files are echo for file in * do if [ -f $file ] then echo $file fi done shprog20 #Command line argument program #Type the string which you want to read after the file name i=1 for word in $* do echo $word i=`expr $i + 1` done echo `expr $i 1`

Shprog21 function one() { echo "Hello How r u?" echo "-------------------" } echo "This is function call demo" one two echo "Function call over"

Shprog22 function two() { echo "***************" } Shprog23 ---------------------------------------------------# -----------------------------------------------------------# Program to check given number is perfect or not. # -----------------------------------------------------------clear i=1 sum=0 echo Enter number read n while [ $i -lt $n ] do k=`expr $n % $i` if test $k -eq 0 then sum=`expr $sum + $i` fi i=`expr $i + 1` done if [ $n -eq $sum ] then echo The given number is perfect number else echo The given number is not perfect fi Shprog24 -------------------------------------------# -------------------------------------------------------------# Program to check entered number is prime or not. # -------------------------------------------------------------clear echo "Enter the number" read num i=2 flag=0 while [ $i -lt $num ] do k=`expr $num % $i` if test $k -eq 0 then flag=1 fi i=`expr $i + 1` done if test $flag -eq 0

then echo "Number is prime" else echo "Number is not prime" fi Shprog25 # ---------------------------------------------------# Program to check number is even or odd. # ---------------------------------------------------echo "Enter the number" read no k=`expr $no % 2` if test $k -eq 0 then echo "Number is even" else echo "Number is odd" fi _________________________________________________________ Shprog26 --------------------------------------------------# Program to print even number series. # --------------------------------------------------no=0 echo "Enter limit" read limit echo "Even number series" while [ $no -le $limit ] do echo $no no=`expr $no + 2` done _________________________________________________________ Shprog27 --------------------------------------------------------------# Program for continuation of any file ( for execution ) # ---------------------------------------------------------------cls echo "Do U want to execute file ? (Y/N) " read ch while [ $ch = 'y' ] do echo Enter filename to execute :read filename cls cat $filename

echo "--------------------------------------------------" echo " " sh $filename sleep 8 cls echo "Do u want to continue ? (Y/N) " read ch done -------------------------------------------------------Shprog28 # ------------------------------------------------# Program to find factorial of number. # ------------------------------------------------fact=1 i=1 echo "Enter the number :" read n while [ $n -ge $i ] do fact=`expr $fact \* $i` i=`expr $i + 1` done echo "Factorial = $fact" Shprog29 -------------------------------------------------------# ---------------------------------------------------------------# Enter a filename and check # ---------------------------------------------------------------clear echo Enter filename : read file if [ -s $file ] then if [ -f $file ] then echo "--------------------------------------" cat $file echo "--------------------------------------" elif [ -d $file ] then ls -i $file fi else echo "This file is empty" fi -----------------------------------------------Shprog30 # -------------------------------------------------------

# Program to print summantion 1+2+3+ ---- +n # ------------------------------------------------------clear echo Enter number read n i=1 sum=0 while [ $i -le $n ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo "Summation = $sum" -------------------------------------------------Shprog31 echo "Print table" j=1 echo Input number read x for i in 1 2 3 4 do j=`expr $x \* $i` echo $j i=`expr $i + 1` done -------------------------------------------------Shprog32 # Write a shell script to find which user login first time. # x=`who|cut -c25-30 |sort|head -1` name=`who|grep $x|cut -f1 -d" "` echo The first login person is $name ----------------------------------------------------echo "The value of variable x is $x" x=20 echo "The value of the variable x is $x" ----------------------------------------------------Shprog33 while echo -n "Name : " do read name case "$name" in *[^a-zA-Z]*) echo "Can contain only alphabets and spaces" continue;; " ") echo "Name not entered"

continue;; *) echo "$name">>stu break;; esac done Shprog35 echo "For loop demonstration " echo " " for i in fly spinder frog do echo I know an old lady who swallowed a $i. echo Swallowed a $i\? echo Swallowed a $i! done ------------------------------------------------------Shprog36 echo "For loop demonstration " for i in 1 2 3; do echo $i; done echo "For loop demo " for i in see spot run do echo $i done echo "For loop demo (of output LINUX) " for i in L I N U X do echo Give me a $i! echo $i! done ------------------------------------------------------Shprog37 echo "For loop demo " for i in $* do echo I know $i done ------------------------------------------------------Shprog38 echo "For loop demo " clear

tab=1 i=1 a=10 echo "Input Number :" read x echo echo "Table of $x" for i in 1 2 3 4 5 6 7 8 9 10 do i=`expr $x \* $i` echo $i done ------------------------------------------------------Shprog39 echo "For loop demo " clear tab=1 i=1 echo "Input lower limit :" read x1 echo "Input upper limit :" read x2 echo j=$x1 while [ $j -le $x2 ] do echo "table of $j" for i in 1 2 3 4 5 6 7 8 9 10 do i=`expr $j \* $i` echo $i done j=`expr $j + 1` done ------------------------------------------------------Shprog40 echo "Grep option to search a content in a file " for i in `ls` do x=`grep do $i` echo $x done ------------------------------------------------------Shprog41

clear echo "enter first no :" read num echo "enter second no :" read num1 rem=`expr $num % $num1` while test $rem -gt 0 do num1=`expr $num` num=`expr $num1` done echo "gcd="$num1 -----------------------------------------------------Shprog42 # echo "Please logout within 5 min" >mesg n=`who | grep $1` if [ -z $n ] then echo "The user $1 is not present" else write $1 < mesg sleep 5 who |grep $1 >the if [ -s the ] then ps -u $1|cut -c1-5 >the2 x=`tail +2 the2` kill -9 $x fi fi -------------------------------------------------------Shprog43 # Write a shell script to accept a number & print its binary equivalent # echo Enter the number read no a=$no s=0 x=1 while(test $a -ne 0) do temp=`expr $a % 2` a=`expr $a / 2` y=`expr $temp "*" $x`

s=`expr $s + $y` x=`expr $x "*" 10` done echo Binary equivalent of decimal number $no is $s Shprog44 # ------------------------------------------------------------------------# Write a program to demonstration of command line arguments( positional # parameters ). # # $1,$2 The positional parameters # $* Complete set of positional parameters as a single string # $# Number of arguments specified in comand line # $0 Name of executed command # $? Exit status of last command # $! PID of last background job # $@ Same as $* except when enclosed in double quotes # ------------------------------------------------------------------------Shprog45 echo "Enter the pattern to search :" read pat echo "Enter the filename :" read fname echo OUTPUT echo ----------------grep "$pat" $fname && echo "Pattern found in file" echo "*******************************************************" grep "$pat" $fname || echo "Pattern not found" exit -------------------------------------------------------------Shprog46 # Write a shell script to create a file which stores the name of files and # against each name put either "Morning" ,"Evening" or "Afternoon" depending # upon time when file is created. # rm k3 ls -l > k1 cut -c48-50 k1 > k2 for i in `cat k2` do if(test $i -gt 0 -a $i -lt 12) then echo Morning >> k3 elif(test $i -gt 12 -a $i -lt 18) then echo Afternoon >> k3

else echo Evening >> k3 fi done ls > k4 paste k3 k4 > k5 cat k5 -------------------------------------------------------------Shprog 47 # Write a shell script to create a file which stores the name of files and # against each name put either "Morning" ,"Evening" or "Afternoon" depending # upon time when file is created. # rm mae ls -l > lnglst cut -c50-52 lnglst > timelst for i in `cat timelst` do if(test $i -gt 0 -a $i -lt 12) then echo Morning >> mae elif(test $i -gt 12 -a $i -lt 18) then echo Afternoon >> mae else echo Evening >> mae fi done ls > fnamelst paste fnamelst mae > mae_out cat mae_out --------------------------------------------------------------Shprog 48 IFS="-" n="[0-9][0-9]" y="[0-9][0-9][0-9][0-9]" month=(0 31 29 31 30 31 30 31 31 30 31 30 31) echo Enter date read val case "$val" in " ") echo "No value entered" ;; $n-$n-$y) set $val let rem=" $3 % 4 " if [ $2 -gt 12 -o $2 -lt 1 ]

then echo "illegal month" exit else case "$val" in 29-02-????) if [ $rem -gt 0 ] ; then echo "$3 is not a leap year" echo "Illegal date" exit fi;; *) if [ $1 -gt ${month[$2]} -o $1 -eq 0 ];then echo "illegal day" exit fi;; esac fi;; *) echo "Invalid date" ;; esac echo "$1-$2-$3 is valid date" -------------------------------------------------------------Shprog 49 # -------------------------------------------------------# Program to print n^y using while loop # -------------------------------------------------------echo "Enter the number : " read n echo "Enter power : " read y res=1 while [ $y -ge 1 ] do res=`expr $res \* $n` y=`expr $y - 1` done echo "Oytput : $res" -------------------------------------------------------------Shprog 50 month=(0 31 29 31 30 31 30 31 31 30 31 30 31) echo Enter month read mon case "$mon" in [jJ][aA][nN]*) echo "No.of days : ${month[1]} ";; [fF][eE][bB]*) echo enter year read year let rem="$year % 4" if [ $rem -eq 0 ] ; then

echo "No. of days : ${month[2]}" else echo "No. of days : 28 " fi;; [mM][aA][rR]*) echo "No. of days : ${month[3]} " esac -------------------------------------------------------------Shprog 51 # ----------------------------------------------------# Program to print prime number series. # ----------------------------------------------------clear echo Enter limit read limit j=1 flag=1 echo "Prime Number Series" while [ $j -le $limit ] do i=2 while [ $i -lt $j ] do temp=`expr $j % $i` if [ $temp -eq 0 ] then flag=0 break; else flag=1 fi i=`expr $i + 1` done if test $flag -eq 1 then echo $j fi j=`expr $j + 1` done -----------------------------------------------------------------Shprog 52 ans=Y while [ $ans = "Y" ] do echo " MENU" echo "1 . Query on product" echo "2 . Query on customer" echo "0 . Exit"

echo "enter ur choice : " read ch case "$ch" in 1) echo "Enter product number :" read pno grep $pno masterdata echo "Query on other product : " read ans if(test $ans = "Y") then continue fi;; 2) echo "Enter customer number :" read cno grep $cno transdata;; 0) exit;; *) echo "Invalid choice. Try again..." esac done ----------------------------------------------------Shprog 53 echi " " echo "Give a string : " read a echo "Give no. of times to be repeted : " read b n=$b while [ $n -gt 1 ] do echo $a n = `expr $n - 1` done -----------------------------------------------------Shprog 54 # ---------------------------------# Simple shell script. # ---------------------------------date cal `date "+%m %Y"` echo Calender for current month is shown above -----------------------------------------------------Student.dat 4101| harshali chaudhari | programmer | VB | 11/09/38 | 5690 2102| vaishali narkhede | Programmer | java| 09/12/37 | 7650 2103| poonam chaudhari | analyst | C | 02/03/01 | 5488

4104| supriya singh | Analyst | vb | 06/07/43 | 4300 6105| komal choudhari | director | java| 08/07/56 | 7600 2106| barun dasgupta | director | C | 06/04/45 | 8900 3107| tarun sengupta | analyst | VB | 06/05/34 | 7590 1108| yogini kulkarni | programmer | c++ | 03/12/35 | 6732 4109| priya singh | programer | VB | 02/12/34 | 6549 5110| leela dasgupta | dyrector | C | 03/22/34 | 8300 -----------------------------------------------------student1.dat 101| harshali chaudhari | programmer | VB | 11/09/38 | 5690 102| vaishali narkhede | Programmer | java| 09/12/37 | 7650 103| poonam chaudhari | analyst | C | 02/03/01 | 5488 106| barun dasgupta | director | C | 06/04/45 | 8900 107| tarun sengupta | analyst | VB | 06/05/34 | 7590 108| yogini kulkarni | programmer ----------------------------------------------------------student2.dat 101| harshali chaudhari | programmer | VB | 11/09/38 | 5690 103| poonam chaudhari | analyst | C | 02/03/01 | 5488 104| supriya singh | Analyst | vb | 06/07/43 | 4300 105| komal choudhari | director | java| 08/07/56 | 7600 106| barun dasgupta | director | C | 06/04/45 | 8900 107| tarun sengupta | analyst | VB | 06/05/34 | 7590 ----------------------------------------------------------transdata p1001:c101:pratik:10:25:20/05/2000 p1002:c103:lalit:20:55:2/3/2001 p1003:c104:mohan;15:15:10/3/2002 ----------------------------------------------------------masterdata p1001:soap:20:aaaaaa:s201:reema:mumbai p1002:oil:50:ygjjdd:s202:uma:nagpur p1003:pen:30:gdsdvsa:s203:pooja:nasik p1004:pencil:10:dhgds:s204:komal:pune -----------------------------------------------------------Shprog 54 # Write a shell script to find which user login first time. # x=`who|cut -c25-30 |sort|head -1` name=`who|grep $x|cut -f1 -d" "` echo The first login person is $name

c++

03/12/35

6732

-------------------------------------------------------Shprog 55 # ---------------------------------------------------------------# Program for continuation of any file ( for execution ) # ---------------------------------------------------------------cls echo "Do U want to execute file ? (Y/N) " read ch while [ $ch = 'y' ] do echo Enter filename to execute :read filename cls cat $filename echo "--------------------------------------------------" echo " " sh $filename sleep 8 cls echo "Do u want to continue ? (Y/N) " read ch done -------------------------------------------------------Write a shell script for expression evaluation bc $1|paste -d=$1 Shprog 56 -------------------------------------------------------# ------------------------------------------------# Program to find factorial of number. # ------------------------------------------------fact=1 i=1 echo "Enter the number :" read n while [ $n -ge $i ] do fact=`expr $fact \* $i` i=`expr $i + 1` done echo "Factorial = $fact" Shprog 57 -------------------------------------------------------# ---------------------------------------------------------------# Enter a filename and check # ---------------------------------------------------------------clear echo Enter filename : read file

if [ -s $file ] then if [ -f $file ] then echo "--------------------------------------" cat $file echo "--------------------------------------" elif [ -d $file ] then ls -i $file fi else echo "This file is empty" fi Shprog 58 -----------------------------------------------# ------------------------------------------------------# Program to print summantion 1+2+3+ ---- +n # ------------------------------------------------------clear echo Enter number read n i=1 sum=0 while [ $i -le $n ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo "Summation = $sum" Shprog 59 -------------------------------------------------echo "Print table" j=1 echo Input number read x for i in 1 2 3 4 do j=`expr $x \* $i` echo $j i=`expr $i + 1` done Shprog 60 ----------------------------------------------------while echo -n "Name : " do read name case "$name" in *[^a-zA-Z]*) echo "Can contain only alphabets and spaces"

continue;; " ") echo "Name not entered" continue;; *) echo "$name">>stu break;; esac done -------------------------------------------------------

You might also like