Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2
- Day 1
- passwd, who, tty, script, clear and tput, uname, date, cal, bc, man, pwd, mkdir, cd, rmdir
- Lab Assignments:
- 1. Perform the following
- a) List the names of the users logged in and their total count without displaying any further details. b) Find out your terminal‟s device name.
- c) Display current date in the form dd/mm/yyyy.
- d) Find out your machine‟s name and the version of the operating system.
- e) Clear the screen and place the cursor at row 12, column 25. f) Find the decimal equivalent of 1101001.
- g) Find out the users who are idling. h) Use man to get help
- i) Ensure that bc displays the results of all divisions using three decimal places.
- Solutions:
- 1)a) $ who -q b) $ tty
- c) $ date +% d/%m/%Y
- d) $ uname -nr e) $ clear
- $ tput cup 12 25
- f) $ bc ibase=2
- g) $ who -Hu h) $ man tty
- I) $ bc
- Scale=3
- 3
- Day 2
- cp, rm, mv, cat, file, ls, umask, chmod, touch, ln, find
- Lab Assignments:
- 1. Perform the following:
- a) Create a directory structure in your home directory (mca, two subdirectories cprogs and projects
- under mca) while being in your home directory. b) Change to the directory projects.
- c) Create a file called biodata and store your name, age, sex, and address in it.
- d) Make a copy of the file biodata into another file text within the directory cprogs. e) Move the file text from cprogs to projects.
- f) Combine the contents of the file biodata and text into another file datatext. g) Rename the file text to newtext.
- h) Change the permissions of the file newtext to rw-rw-rw-. i) List all filenames starting with „a‟ or „b‟ or „m‟.
- j) List all filenames that end with a digit.
- k) List all files in the current directory whose second character is a digit.
- l) Use command(s) to create a directory in your home directory called KeepOut whose contents can be read only by you.
- Solution:
- 1. a) $ mkdir mca mca/cprogs mca/projects b) $ cd /mca/projects
- c) $ cat > /mca/projects/biodata
- ( write some datas)
- d) $ cp /mca/projects/biodata /mca/cprogs/text e) $ mv /mca/cprogs/text /mca/projects/text
- f) $ cat /mca/projects/biodata /mca/cprogs/text >> /mca/projects/datatext g) $ mv /mca/projects/text /mca/projects/newtext
- h) $ chmod 666 /mca/projects/newtext
- I) $ ls [abm] * J) $ ls * [0-9]
- K) $ ls ? [0-9]*
- L) $ mkdir keepout ; chmod 400 keepout
- 4
- Day 3
- wc, od, cmp, diff, comm., tee, head, tail, cut, paste, sort, tr, uniq
- Lab Assignments:
- 1. Perform the following:
- a) List all files beginning with character „a‟ on the screen and also store them in a file called file1.
- b) Sort the output of who and display on screen along with total number of users. The same output except the number of users should be stored in a file file1.
- c) Double space a file
- d) Select lines 5 to 10 of a file
- e) Find the user name and group id from the file /etc/passwd using the cut command. f) Extract the names of the users from /etc/passwd after ignoring the first 10 entries.
- g) Sort the file /etc/passwd on GUID (primary) and UID (secondary) so that the users with the same
- GUID are placed together. User with a lower UID should be placed higher in the list. h) List from /etc/passwd the UID and the user having the highest UID.
- i) Device a sequence which lists the five largest files in the current directory.
- j) Remove duplicate lines from a file.
- k) Count the frequency of occurrences of words in a file.
- l) Find "long" listing of the smallest 5 files in the /etc directory whose name contains the string ".conf", sorted by increasing file size.
- m) What would you type at the command line to get a sorted list, with no duplicates, of all the users
- logged into the local network?
- n) What would you type at the command line to find all files in your home directory that are more than a week old and end with .bak?
- What would you type at the command line to find out how many total lines are contained in all the
- files ending in .c in the current directory, printing only the total number of lines?
- Solution:
- 1. a) $ ls [a] * | tee file1
- b) $ who -q | sort ; who -Hu | cat >> file1 c) $ pr -d file1
- d) $ head -10 file1 | tail -n -5
- e) $ cut -d “:” -f 1,4 /etc/passwd
- f) $ cut -d “:” -f 1 /etc/passwd | tail -n +11 g) $ cut -d “:” -f 3,4 /etc/passwd | sort -n
- h) $ sort -t “:” -r -n -k 3 /etc/passwd | cut -d “:” -f 1,3 | head -1 i) $ ls -lS | head -6
- j) $ uniq file1
- k) $ sort file1 | uniq -c
- 5
- l) $ ls -lSr /etc/*.conf | head -5 m) $ who | uniq |sort
- n) $ find -mtime +7 -name “*.bak” -ls o) $ wc -l *.c
- 6
- Day 4
- ps, kill, grep, egrep, fgrep
- Lab Assignments:
- 1. Perform the following
- a) Find out the PID of your login shell.
- b) Remove the header line from the ps output.
- c) List all processes that you are currently running on your machine, sorted by the command name in alphabetical order (i.e. a process running acroread should be listed before a process running zwgc). The
- output should consist only of the processes you are running and nothing else (i.e. if you are running 6 processes, the output should only have 6 lines).
- d) Display the files in the current directory that contain the string MCA HITK in either upper- or lowercase.
- e) Store in a variable the number of lines containing the word MCA in the files mca1, mca2 and mca3.
- f) If you did not have the wc command, how would you use grep to count the number of users currently using the system?
- g) Remove blank lines from a file using grep.
- h) List the ordinary files in your current directory that are not writable by the owner.
- i) Locate lines ending and beginning with a dot and containing anything between them. j) Locate lines that are less than 100 characters in length.
- Solution:
- 1. (a to c is discarded)
- d) $ grep -il „MCAHITK‟ *
- e) $ var=`grep MCA mca[1-3] | wc -l`
- $ echo $var
- f) who | grep -c “.*”
- g) $ grep -v “^$” aa1
- h) $ ls -l | grep -v „^..w‟
- i) $ grep „^\..*\.$‟ mca4
- j) $ grep „^.\{0,99\}$‟ * file1
- 7
- Day 5
- Lab Assignments:
- 1. Write a shell script to find out whether an integer input through the keyboard is an odd number or an even number.
- 2. Write a shell script to find out whether any year input through the keyboard is a leap year or not. If no argument is supplied the current year should be assumed.
- 3. Write a shell script to find the maximum of three numbers provided as command line arguments.
- 4. Write a shell script to check whether a given number is prime or not.
- Solution: (LAB)
- 1) #! /bin/sh
- # Purpose: To find whether an integer taken as input is an odd or even number.
- # USAGE: sh oddeven.sh (No arguments required)
- echo “Enter an integer:”
- read n
- if test `expr $n % 2` -eq 0 then
- echo “$n is a even no.”
- else
- echo “$n is a odd no.”
- fi
- # end of script
- 2) #! /bin/sh
- # Purpose: To find out whether a year taken as input is leap year or not.
- # USAGE: sh leapyear.sh year
- If test $# -eq 0 then
- 8
- set `date`
- var=$6
- else
- var=$1
- fi
- if test `expr $var % 4` -eq 0 -a `expr $var % 100` -ne 0 -o `expr $var % 400` -eq 0`
- then
- echo “$var is a leap year”
- else
- echo “$var is not a leap year”
- fi
- # end of script
- 3) #! /bin/sh
- # Purpose: To find out maximum of three numbers taken as command line argument.
- # USAGE: sh maxthreeno.sh number1 number2 number3
- If test $# -ne 3 then
- echo “please give three numbers”
- exit fi
- if test $# -eq 3 then
- max=$1
- if test $2 -gt $max then
- max=$2
- 9
- fi
- if test $3 -gt $max then
- max=$3
- fi
- fi
- echo “max number=$max”
- #end of script
- 4) #! /bin/sh
- # Purpose: To check whether a integer is prime or not taken as input.
- # USAGE: sh primecheck.sh(No arguments required)
- echo “Enter a number:”
- read n i=1
- k=0
- while test $i -le $n do
- if test `expr $n % $i ` -eq 0 then
- k=`expr $k + 1`
- fi
- i=`expr $i + 1`
- done
- if test $k -eq 2 then
- echo “$n is prime”
- else
- 10
- echo “$n is not prime”
- fi
- # en d of script
- Home assignments:
- 1. Write a shell script to find the factorial value of any integer entered through the keyboard.
- 2. Write a shell script to generate all combinations of 1, 2 and 3.
- 3. Write a shell script to print all prime numbers in a given range.
- 4. Write a shell script to calculate the sum of digits of any number entered through keyboard.
- 5. Rajesh‟s basic salary (BASIC) is input through the keyboard. His dearness allowance (DA) is 52% of BASIC. House rent allowance (HRA) is 15% of BASIC. Contributory provident fund is 12% of (BASIC + DA). Write a shell script to calculate his gross salary and take home salary using the following formula:
- Gross salary = BASIC + DA + HRA
- Take home salary = Gross salary - (BASIC + DA) * 0.12
- Solution:(HOME)
- 1) #! /bin/sh
- #Purpose: To find out the factorial of a given integer.
- # USAGE: sh factorial.sh (No arguments required)
- echo “Enter an integer:”
- read n f=1
- i=1
- while test $i -le $n do
- f=`expr $f \* $i`
- i=`expr $i + 1`
- done
- echo “Factorial value: $f”
- # end of script
- 11
- 2) #! /bin/sh
- #Purpose: To generate all combinations of 1,2 and 3.
- # USAGE: sh combin123.sh (no arguments required)
- echo “Respective combination of 1,2,3 are:”
- i=1
- while test $i -le 3 do
- j=1
- while test $j -le 3 do
- k=1
- while test $k -le 3 do
- s=`expr $i \* 100 + $j \* 10 + $k`
- echo $s
- k=`expr $k + 1`
- done
- j=`expr $j + 1`
- done
- i=`expr $i + 1`
- done
- #end of script
- 3) #! /bin/sh
- # Purpose: To print all prime numbers in a given range.
- # sh primerange.sh (No arguments required)
- echo “Enter the range:”
- 12
- read n i=1
- while test $i -le $n do
- b=1 c=0
- while test $b -le $i do
- if test `expr $i % $b` -eq 0 then
- c=`expr $c + 1`
- fi
- b=`expr $b + 1`
- done
- if test $c -eq 2 then
- echo “$i is a prime number”
- fi
- i=`expr $i + 1`
- done
- #end of script
- 4) #! /bin/sh
- # Purpose: To find out the sum of digits given as input.
- # USAGE: sh sumdigits.sh (No arguments required)
- echo “Enter a number :”
- 13
- read n sum=0
- while test $n -gt 0 do
- r=`expr $n % 10`
- sum=`expr $sum + $r`
- n=`expr $n / 10`
- done
- echo “Sum of digits:$sum”
- # end of script
- 5) #! /bin/sh
- #Purpose: To find out the Basic salary of an employee.
- # USAGE: sh basicsalary.sh (no arguments required)
- echo “Enter rajesh `s basic salary:”
- read basic
- da=`expr $basic \* 52 / 100` hra=`expr $basic \* 15 / 100` v=`expr $basic + $da` cpf=`expr $v \* 12 / 100` gs=`expr $basic + $da + $hra` hs=`expr $gs - $cpf`
- echo “Gross salary:$gs”
- echo “Home salary:$hs”
- #end of script
- 4
- Day 6
- Lab Assignments:
- 1. Write a shell program that takes a number from user and prints the reverse of the number.
- 2. Write a shell script to determine whether two numbers input through keyboard are prime to each other.
- 3. Write a shell script to find whether a number is divisible by 11.
- 4. Write a shell script that produces a shell calculator to perform the following operations:
- 1. Addition
- 2. Subtraction
- 3. Multiplication
- 4. Division
- Solution: (LAB)
- 1) #! /bin/sh
- # Purpose: To find out the reverse of a given number.
- # USAGE: sh reverseno.sh (no arguments required)
- echo “ Enter a number:”
- read n sum=0
- while test $n -gt 0 do
- r=`expr $n % 10`
- sum=`expr $sum \* 10 + $r`
- n=`expr $n / 10`
- done
- echo “Reverse of a number:$sum”
- #end of script
- 2) #! /bin/sh
- 15
- #Purpose: To determine whether two numbers taken as input are prime to each other.
- # USAGE: sh primeeachother.sh (NO arguments required)
- echo “Enter first number:”
- read a
- echo “Enter second number:”
- read b
- if test $b -gt $a then
- max=$b min=$a
- else
- max=$a min=$b
- fi
- r=`expr $max % $min`
- while test $r -ne 0 do
- max=$min min=$r
- r=`expr $max % $min`
- done
- if test $min -eq 1 then
- echo “$a and $b are prime to each other”
- else
- echo “$a and $b are not prime to each other”
- fi
- #end of script
- 16
- 3) #! /bin/sh
- #Purpose: To check whether the given number is divisible by 11.
- # USAGE: sh div11.sh (no arguments required)
- echo “Enter a number:”
- read n p=0
- c=0 k=0
- while test $n -gt 0 do
- r=`expr $n % 10` c=`expr $c + 1` n=`expr $n / 10`
- done i=1
- while test $i -le $c do
- r=`expr $n % 10`
- if test `expr $i % 2` -ne 0 then
- p=`expr $p + 1`
- else
- k=`expr $k + 1`
- fi
- n=`expr $n / 10`
- 17
- i=`expr $i + 1`
- done
- if test $k -gt $p then
- l=`expr $k - $p`
- else
- l=`expr $p - $k`
- fi
- if test `expr $l % 11` -eq 0 then
- echo “Divisible by 11”
- else
- echo “Not divisible by 11”
- fi
- #end of script
- 4) #! /bin/sh
- #Purpose: A program to generate a calculator which performs basic arithmetical operations.
- # USAGE: sh calculator.sh operand operator operand
- If test $# -ne 3 then
- echo “Please give three arguments as operand op operand”
- exit fi
- case $2 in
- +) var=`expr $1 + $ 3`;;
- -)var=`expr $1 - $3`;;
- X)var=`expr $1 \* $3`;; #(PLEASE GIVE X INSTEAD OF * DURING MULTIPLICATION)
- 18
- /)var=`expr $1 / $3`;;
- *) echo “Invalid operator”;;
- esac
- echo “The required value:$var”
- #end of script
- Home assignments:
- 1. Write a shell script to print the following pattern for any number of lines:
- *
- * * *
- * * * * *
- * * * * * * *
- * * * * * * * * *
- 2. Write a shell script to test whether a given string is palindrome or not.
- 3. Write a shell script which counts the number of consonants and vowels in a given sentence.
- 4. Write a shell script to display the list of users as well as the number of users connected to the
- system.
- Solution: (HOME)
- 1) #! /bin/sh
- #Purpose: To generate a pattern with stars.
- # USAGE: sh pattern.sh (no argments required)
- echo “Enter the number of rows:”
- read r i=1
- while test $i -le $r
- 19
- do
- k=`expr $r - $i` while test $k -gt 0 do
- echo -n “2blanks”
- k=`expr $k - 1`
- done j=1
- while test $j -le `expr 2 \* i - 1`
- do
- echo -n “*blank”
- j=`expr $j + 1`
- done echo
- i=`expr $i + 1`
- done
- #end of script
- 2) #! /bin/sh
- #Purpose:To check whether the given string is palindrome or not.
- # USAGE: sh palindromestring.sh (no arguments required)
- echo “Enter a string:”
- read n len=${#n}
- v=`expr $len / 2 + 1`
- i=1
- while test $i -lt $v do
- 20
- k1=`echo $n| cut -c $i` k2=`echo $n|cut -c $len` if test “$k1” != “$k2” then
- echo “Not palindrome”
- exit fi
- len=`expr $len - 1`
- i=`expr $i + 1`
- done
- echo “Palindrome “
- #end of script
- 3) #! /bin/sh
- #Purpose: To count number of vowels and consonants in a sentence.
- # USAGE: sh vowelcons.sh (No arguments required)
- echo “Enter a sentence:”
- read line len=${#line} vowel=0
- x=0 i=1
- while test $i -le $len do
- k=`echo $line|cut -c $i | grep -ic [aeiou]` k1=`echo $line|cut -c $i|grep -ic [^a-z]` vowel=`expr $vowel + $k`
- x=`expr $x + $k1`
- 21
- i=`expr $i + 1`
- done
- echo “No of vowel:$vowel” con=`expr $len - $vowel - $x` echo “No of consonants:$con”
- #end of script
- 4) #! /bin/sh
- #Purpose: To count number of users who are currently log ged in the system.
- # USAGE: sh user.sh (NO arguments required)
- echo “List of users connected:`who -q|head -1`”
- echo “No of users:`who -q | tail -n -1 | cut -c 9`” #end of script
- 22
- Day 7
- Lab Assignments:
- 1. Write a shell script that displays a list of all files in the current directory to which you have read, write and execute permissions.
- 2. Write a shell script that lists files by modification time when called with lm and by access time when
- called with la. By default, the script should show the listing of all files in the current directory.
- 3. Write a shell script to display the files created or updated within fourteen days from the current date.
- 4. Develop a shell script which displays all files with all attributes those have been created or modified in the month of November.
- Solution: (LAB)
- 1) #! /bin/sh
- #Purpose: To display list of files in current directory to which owner have read,write and execute permission.
- # USAGE: sh rwxperm.sh (no arguments required)
- for var in *
- do
- if test -r $var -a -w $var -a -x $var -a ! -d $var then
- ls $var fi
- done
- #end of script
- 2) #! /bin/sh
- # Purpose: To list the files according to modification or access time depending on the arguments in command line.
- # USAGE: sh filelist .sh lm(for modification time) or la (last access time)
- case $1 in lm) ls -lt ;; la) ls -lut;;
- *) ls -l;;
- 23
- esac
- #end of script
- 3) #! /bin/sh
- # Purpose: To list the files created or updated within fourteen days from current date.
- # USAGE: sh filecreat14.sh (no arguments required)
- find -atime -14 -mtime -14 | sort -u
- #end of script
- 4) #! /bin/sh
- #Purpose: To list those files created or updated in month of November.
- # USAGE: sh createNOV.sh (no arguments required)
- for var in *
- do
- set -- `ls -l $var`
- if test “$6” = “Nov”
- then
- ls -l $var fi
- done
- #end of script
- Home assignments:
- 1. Write a shell script to print last twenty commands issued by the user. The user name is supplied as a command line argument to the script (use bash-history file).
- 2. Write a shell program, which displays the message “welcome” and prints the date when you login to
- your system.
- 24
- 3. Accept a string from the terminal and echo a suitable message if it doesn‟t have at least ten
- characters.
- 4. Write a shell script which receives either the LOGNAME or the UID supplied at the command prompt and finds out at how many terminals this user has logged in.
- 5. Write a shell script, which gets executed the moment a user logs in. It should display the message “GOOD MORNING” or “GOOD AFTERNOON” or “GOOD EVENING” depending upon the time at which the user logs in.
- Solution: (HOME)
- 1) #! /bin/sh
- #Purpose: To print last 20 commands issued by the user.
- # USAGE: sh 20comnd.sh username if test $# -ne 1
- then
- echo “Enter your login name only”
- exit fi
- if test “$1” = “$LOGNAME”
- then
- cat $HOME/.bash_history | tail -20 else
- echo “Wrong username”
- fi
- #end of script
- 2) #! /bin/sh
- #Purpose: To print welcome and date when user log in to the system.
- # USAGE: sh welcome.sh (no arguments required)
- 25
- # To be written in /.bashrc file
- echo “Welcome”
- echo `date`
- #end of script
- 3) #! /bin/sh
- #Purpose: To take a string and print a msg if the string have or not have atleast 10 characters.
- # USAGE: sh string10char.sh string
- If test $# -ne 1 then
- echo “Enter a string.”
- Exit fi
- st=$1 v=${#st}
- if test $v -lt 10 then
- echo “String doesnot have atleast 10 characters.”
- else
- echo “String have atleast 10 characters”
- fi
- #end of script
- 4) #! /bin/sh
- 26
- #Purpose: To print number of terminals opened by the user.
- # USAGE: sh terminal.sh loginname if test $# -ne 1
- then
- echo “Enter your login name only”
- exit fi
- if test “$1” = “$LOGNAME”
- then
- v=`ls /dev/pts | wc -w`
- echo “Number of terminals=$v”
- else
- echo “Wrong login name”
- fi
- #end of script
- 5) #! /bin/sh
- #Purpose:To print suitable msg depending on the time of the day when the user will log in.
- # USAGE: sh message.sh (no arguments required)
- # to be written in /.bashrc file var=`date +%H`
- if test $var -lt 12 then
- echo “Good morning”
- elif test $var -lt 16 then
- 27
- echo “Good afternoon”
- elif test $var -lt 20 then
- echo “Good evening”
- else
- echo “Good night fi
- #end of script
- 28
- Day 8
- Lab Assignments:
- 1. Write a shell script, which reports names and sizes of all files in a directory (directory should be supplied as an argument to the shell script) whose size exceeds 100 bytes. The filenames should be printed in decreasing order of their sizes. The total number of such files should also be reported.
- 2. Write a shell script that shows the names of all the non-directory files in the current directory and calculates the sum of the size of them.
- 3. Write a shell script to list the name of files under the current directory that starts with a vowel.
- 4. Write a shell script which receives two filenames as arguments and checks whether the two file‟s
- contents are same or not. If they are same then the second file should be deleted.
- Solution: (LAB)
- 1) #! /bin/sh
- #Purpose: To print the name of all files in a directory whose size exceeds 100 bytes along with total number of such files.
- # USAGE: sh file100byte.sh directoryname
- If test $# -ne 1 then
- fi
- cd $1
- echo “Please give a directory name and try again”
- exit
- find -size +100b | sort –nr
- echo “Total number of such files:”
- find -size +100b | grep -c “.*”
- 29
- #end of script
- 2) #! /bin/sh
- #Purpose: To print all non-directory files in current directory alongwith the total size of those files.
- #USAGE: sh sizenondir.sh (No arguments required)
- sum=0
- for var in *
- do
- if test ! -d $var
- then
- fi done
- ls $var
- set -- `ls -l $var`
- let sum=sum+$5
- echo “Sum of sizes of all non directory files under current directory is:$sum”
- #end of script
- 3) #! /bin/sh
- #Purpose: To list the files starting with a vowel.
- # USAGE : sh filevowel.sh (no arguments required)
- echo “Required files are:”
- ls | grep “^[aeiou]”
- #end of script
- 4) #! /bin/sh
- 30
- #Purpose: To compare two files and delete the second file if they are equal.
- # USAGE: sh filecomparedel.sh file1 file2 if test $# -ne 2
- then
- echo “Please give two filenames.”
- exit fi
- cmp -s $1 $2
- if test $? -eq 0 then
- echo “$1 and $2 are same”
- rm $2
- else
- echo “$1 and $2 are not same”
- fi
- #end of script
- Home assignments:
- 1. Write a shell script to check if a given file (filename supplied as command line argument) is a regular file or not and find the total number of words, characters and lines in it.
- 2. Write a shell script which reads a directory name and compares the current directory with it (which has more files and how many more files).
- 3. Write a shell script to check whether the given file is a blank file or not. If not found blank then display the contents of the file.
- 4. Write a shell script to concatenate two files and count the number of characters, number of words and number of lines in the resultant file.
- 5. Write a shell script that accepts two directory names, say mca1 and mca2 as arguments and deletes
- those files in mca2 which have identical named files in mca1.
- Solution: (HOME)
- 1) #! /bin/sh
- 31
- #Purpose: To check whether the given file is regular or not and find out total no. of words, total no. of characters and total no. of lines in it.
- # USAGE: sh regularfile.sh filename if test $# -ne 1
- then
- echo “Please give a filename”
- exit fi
- if test -d $1
- then
- echo “It is a directory ,please give a file name”
- exit fi
- if test -f $1
- . then
- echo “It is a regular file”
- echo “Total Words:`wc -w $1 | cut -d “ “ -f 1`”
- echo “Total characters:`wc -c $1 | cut -d “ “ -f 1`”
- echo “Total lines:`wc -l $1 | cut -d “ “ -f 1`”
- else
- echo “It is not a regular file”
- fi
- #end of script
- 2) #! /bin/sh
- #Purpose: To check two directories and compare how many files between them.
- #USAGE: sh dircheck.sh (No arguments required)
- echo -n “Enter a directory name:”
- read dirc x=`pwd`
- 32
- v1=`pwd | ls |wc -w`
- echo “Files in `pwd` =$v1”
- cd $dirc y=`pwd`
- v2=`ls |wc -w`
- echo “Files in `pwd`=$v2”
- if test $v1 -eq $v2 then
- echo “Both directory have same no of files”
- fi
- if test $v1 -gt $v2
- then
- echo “Current directory $x contains `expr $v1 - $v2` more files than given
- directory $y”
- else
- echo “Given directory $y contains `expr $v2 - $v1` more files than current
- directory $x”
- fi
- #end of script
- 3) #! /bin/sh
- #Purpose: To check whether the given file is blank or not.
- # USAGE: sh blankfilecheck.sh filename if test $# -ne 1
- then
- echo “Enter a filename and try again”
- exit fi
- if test -d $1 then
- echo “It is a directory,please enter a filename”
- 33
- exit fi
- set -- `ls -l $1` if test $5 -eq 0 then
- echo “$1 is a blank file”
- else
- echo “$1 is not a blank file”
- echo “Contents of the file:”
- cat $1 fi
- #end of script
- 4) #! /bin/sh
- #Purpose: To concatenate two files and find the no. of words,char and lines of resultant file.
- # USAGE: sh concatfileinform.sh file1 file2 if test $# -ne 2
- then
- echo “Enter two file names only”
- exit fi
- echo -n “Enter the name of resultant file where these two files are to be concatenated:”
- read rfile
- cat $1 $2 > rfile
- echo “Contents of resultant file:”
- cat $rfile
- echo “Resultant file have:”
- echo “Number of words:`wc -w $rfile| cut -d “ “ -f 1`”
- 34
- echo “Number of characters:`wc -c $rfile| cut -d “ “ -f 1`”
- echo “Number of lines:`wc -l $rfile| cut -d “ “ -f 1`”
- #end of script
- 5) #! /bin/sh
- #Purpose: To check two directories and delete files of second directory which is identical to the files of first directory.
- #USAGE : sh identicalfiledel.sh directory1 directory2 if test $# -ne 2
- then
- echo “Enter two directory names only”
- exit fi
- if test -d $1 -a -d $2 then
- echo “$1 and $2 are directories”
- else
- echo “$1 and $2 are not directories”
- exit fi
- echo “Files in directory $1=`ls $HOME/$1`” echo “Files in directory $2=`ls $HOME/$2`” f=0
- cd $1
- for var1 in *
- do
- cd $HOME/$2
- 35
- for var2 in *
- do
- cmp -s “$var1” “$var2”
- if test $? -eq 0 then
- f=1
- echo “$var2 is present in $1 and $2 with identical names”
- rm $var2
- echo “$var2 is removed from $2”
- fi done
- done
- if test $f -eq 0 then
- echo “There are no identical named files in $1 and $2”
- else
- echo “Present files in $2 are:”
- ls $HOME/$2 fi
- #End of script
- 36
- Day 9
- Lab Assignments:
- 1. A file called list consists of several words. Write a shell script which will receive a list of filenames, the first of which would be list. The shell script should report all occurrences of each word in list in the rest of the files supplied as arguments.
- 2. Write a shell script which deletes all lines containing the word UNIX in the files supplied as arguments to this shell script.
- 3. Write a shell script which would receive a log name during execution, obtain information about it from password file and display this information on the screen in easily understandable format.
- 4. Write a shell script, which will receive either the filename or the filename with its full path during execution. This script should print information about the file as given by ls –l command and display it in
- an informative manner.
- Solution: (LAB)
- 1) #! /bin/sh
- #Purpose: To consider a file and compare all occurrences of words of that file with the files supplied as arguments.
- #USAGE: sh listoccurence.sh list file1 file2 file3………file n
- If test $# -eq 0 then
- echo “Please give filenames”
- exit fi
- l=$1 shift
- for var1 in $*
- do
- for var2 in `cat $l`
- do
- echo “Occurrence of $var2 in $var1 is :`grep -c $var2 $var1`”
- done done
- 37
- #end of script
- 2) #! /bin/sh
- #Purpose: To delete the lines containing the word UNIX in the file supplied.
- #USAGE: sh unixdelete.sh filename(s) If test $# -eq 0
- then
- echo “Please give filenames”
- exit fi
- for var in $*
- do
- grep -v “UNIX” $var >ff cp ff $var
- done
- #end of script
- 3) #! /bin/sh
- #Purpose: To print the user information in a informative manner.
- #USAGE: sh userinform.sh loginname if test $# -ne 1
- then
- echo “Enter your login name only”
- exit fi
- grep -w “$1” “/etc/passwd”>file1
- 38
- IFS=:
- If test $? -eq 0 then
- set -- `grep -w “$1” “/etc/passwd”`
- echo “Loginame:$1” echo “Password:$2” echo “UID:$3”
- echo “GUID:$4”
- echo “Home:$5”
- echo “Login shell:$7”
- else
- echo “User doesnot exist”
- fi
- #end of script
- 4) #! /bin/sh
- #Purpose: To display all the attributes of file in a informative manner.
- # USAGE: sh fileinform.sh filename if test $# -ne 1
- then
- echo “Enter a file name”
- exit fi
- set -- `ls - l $1`
- echo “File permission:$1”
- echo “Links:$2”
- echo “Username:$3”
- 39
- echo “Groupuser name:$4”
- echo “Size:$5” echo “Month:$6” echo “Date:$7”
- echo “Time:$8” #end of script
- Home assignments:
- 1. Write a shell script that takes a list of names and displays all information in the password file, where login names are the members of the list.
- 2. Write a shell script that periodically count the number of users logged into the system. Send the number of minutes at which to check as parameter.
- 3. Write a shell script to count the number of words of different length present in a given text.
- 4. Write a shell script to count the frequency of different words used in a file.
- A shell script receives even number of filenames. Suppose four filenames are supplied then the first file should get copied into the second file, the third file should get copied into the fourth file, and so on. If odd number of filenames is supplied then no copying should take place and an error message should be displayed.
- solution: (HOME)
- 1) #! /bin/sh
- #Purpose: To take list of login names and print there attributes in informative manner from password file.
- #USAGE: sh usersinform.sh loginname(s)
- if test $# -eq 0 then
- echo “Enter your login names please”
- exit
- 40
- fi
- for var in $*
- do
- grep -w “$var” “/etc/passwd” > file1
- IFS=:
- If test $? -eq 0 then
- set -- `grep -w “$var” “/etc/passwd”`
- echo “information of $var is:”
- echo “Loginame:$1” echo “Password:$2” echo “UID:$3”
- echo “GUID:$4”
- echo “Home:$5”
- echo “Login shell:$7”
- echo else
- echo “User doesnot exist”
- fi done
- #end of script
- 2) #! /bin/sh
- #Purpose: To generate a script which will print no. of users currently logged in according to the time supplied.
- #USAGE: sh usersattime.sh time(in terms of second)
- if test $# -ne 1 then
- echo "please give time in minute in terms of seconds" exit
- 41
- fi
- while true do
- c=`who|grep -c ".*"`
- echo "number of users=$c"
- done
- sleep $1
- #end of script
- 3) #! /bin/sh
- #Purpose: To print the occurrence of lenghth of different word in a text.
- #USAGE: sh wordlengthoccur.sh (no arguments required)
- echo "Enter a text:" read n
- echo $n|tr -cd "[\040a-zA-Z]" > ff for var in `cat ff`
- do
- echo ${#var} >> f1 done
- echo “Number of words of different lengths are:”
- cat f1|sort|uniq –c rm f1
- # end of script
- 4) #! /bin/sh
- #Purpose: To print the frequency of different words in a file.
- #USAGE: sh freqword.sh filename
- 42
- if test $# -ne 1
- then
- fi
- echo "please give file name" exit
- echo “Frequency of different words in the file $1:”
- tr ' ' '\n' < $1|tr -cd "[\na-zA-Z]" |sort| uniq –c
- #end of script
- 5) #! /bin/sh
- #Purpose: To concatenate the first file to second,third to fourth and so on,if the total number of arguments is even or else do nothing.
- #USAGE: sh evnoddfiles.sh filename(s)
- if test $# -eq 0 then
- echo "give file names" exit
- fi
- v=`expr $# % 2`
- k=$#
- c=0
- if test $v -eq 0 then
- for var in $*
- do
- c=`expr $c + 2`
- l=$1 m=$2
- cp $l $m shift 2
- if test $c -eq $k
- 43
- done
- then fi
- exit 1
- else
- fi
- echo "No copy should take place as odd number of files are supplied"
- #end of script
- 44
- Day 10
- Lab Assignments:
- 1. Devise a menu-driven shell program that accepts values from 1 to 4 and performs action depending upon the number keyed in:
- 1. List of users currently logged in
- 2. Present date
- 3. Present working directory
- 4. Quit
- solution: (LAB)
- 1) #! /bin/sh
- #USAGE: sh menu.sh (no arguments required)
- while true do
- echo “1. List of users currently logged in.”
- echo “2. Present date”
- echo “3. Present working directory”
- echo “ 4. Quit”
- echo -n “Enter your choice:”
- read ch case $ch in
- 1) who;;
- 2) date +%d-%B-%Y;;
- 3) pwd;;
- 4) exit;;
- *) echo “Invalid choice.”;;
- esac
- #end of script
- 45
- Home assignments:
- 1. Write a shell script to make a password based menu-driven program, which will give a maximum of three chances to enter the password. If the given password is correct then the program will show the
- 1. Number of users currently logged in.
- 2. Calendar of current month.
- 3. Date in the format: dd / mm / yyyy.
- 4. Quit
- The menu should be placed approximately in the centre of the screen and should be displayed in bold.
- solution: (HOME)
- 1) #! /bin/sh
- #USAGE: sh menupasswd.sh (no arguments required)
- c=0 f=0
- while test $f -eq 0 do
- echo "Enter ur login name:" read n
- if test $n != $LOGNAME
- then
- f=0
- c=`expr $c + 1`
- if test $c -eq 3 then
- else fi
- done
- fi f=1
- echo "3 chances over" exit 1
- if test $f -eq 1
- then
- echo "Login name valid" while true
- 46
- do
- echo "1.List of users currently logged in" echo "2.calendar of current month" echo "3.Date in format:dd/mm/yyyy"
- echo "4.Exit"
- echo "Enter ur choice:" read ch
- case $ch in
- 1) who -q|tail -1;;
- 2) cal;;
- 3) date +%d/%m/%Y;;
- 4) exit;;
- *) echo "invalid entry";;
- done
- esac
- fi
- #end of script
- 47
- Day 11
- Lab Assignments:
- 1. sed assignments
- a) Delete last line of a file.
- b) Delete from third to the last line of a file.
- c) Append the line “LET US STOP” after the last line.
- d) Print first five lines of a file.
- e) Remove all lines containing the word „BCA‟ from a file.
- f) Append the third line after the last line in a file.
- g) Replace all occurrences of „two‟ with „three‟ in a file. h) Cut and paste the third line after the last line in a file. i) Double-space a file.
- j) Insert a blank line above every line that matches "MCA".
- 2. awk assignments
- a) List all regular files in the current directory whose name starts with „a‟.
- b) Display the current date and month in the given format:
- c) Date: 22/11/2007 d) Month: Nov
- e) List all files in the current directory whose size is greater than 200 bytes, and also give the sum of the
- sizes of all those files.
- f) List the name of files under the current directory that starts with a vowel. g) Check whether a given string is palindrome or not.
- h) List all users currently logged into the system.
- i) List all files in the current directory that has more than one link to it. j) Double-space a file.
- k) Double-space a file that already has blank lines in it. Output file should contain no more than one
- blank line between lines of text.
- l) Number each line of file, but only print numbers if line is not blank.
- Solutions: (LAB)
- Sed assignments:
- 1) a) $ sed „$d‟ file
- b) $ sed „3,$d, file
- c) $ sed „$a\LET US STOP\‟ file
- d) $ sed „5q‟ file
- e) $ sed -n „/BCA/!p‟ file
- f) $ sed -n „p‟ file ; sed -n „3p‟ file
- 48
- g) $ sed „s/two/three/‟ file
- h) $ sed -n „3!p‟ file ; sed -n „3p‟ file
- i) $ sed „‟i\ \‟ file
- j) $ sed „/MCA/i\ \‟ file
- Awk assignments:
- 2) a) $ ls | awk „/^a/{print}‟ file
- b,c,d) $ date +”%d %m %Y %h”|awk -F “ “ „{print “Date:” $1”/”$2”/”$3 ; print
- “Month:” $4}‟
- e) $ ls -l | awk -F “ “ „BEGIN{printf “List of files:\n”} ; $5>200{s=s+$5;printf
- “%s\n”,$9}; END{print “Sum of sizes:” s}‟
- f) $ ls|awk „/^[aeiou]/{print}‟
- g) #!/bin/sh
- #Program: palin.sh
- #Purpose: To prove whether a string is palindrome or not using awk command
- #usage: sh palin.sh
- echo –n ”Enter any string:”
- read a junk
- echo “$a” | awk „
- {
- p=$1
- stat=”Palindrome” l=length(p) loop=int(l/2)
- for(i=1;i<=loop;i++)
- {
- first=substr(p,i,1)
- last=substr(p,l-i+1,1)
- if(first != last)
- stat=”Not palindrome”
- }
- print stat
- }‟
- h) $ who |awk -F “ “ „{print $1}‟
- 49
- i) $ ls -l |awk -F “ “ „$2>1{print $9}‟
- j) $ awk „1; {print “”}‟ file
- k) $ awk „NF{print $0 “\n”}‟ file
- l) $ awk „NF{$0=++a “:” $0} ; {print}‟ file
- Home assignments:
- 1. sed assignments
- a) Number each line of file, but only print numbers if line is not blank. b) Count lines (emulate "wc -l").
- c) If a line ends with a backslash, append the next line to it.
- d) Add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.).
- e) Print the line immediately after the line containing “MCA”, but not the line containing “MCA”.
- f) Print line number 5.
- g) Beginning at line 3, print every 7th line.
- 2. awk assignments
- a) Count lines (emulate "wc -l").
- b) Print the sums of the fields of every line.
- c) Add all fields in all lines and print the sum.
- d) Print every line after replacing each field with its absolute value. e) Print the total number of fields ("words") in all lines.
- f) Print the total number of lines that contain "MCA".
- g) Print the largest first field and the line that contains it (Intended for finding the longest string in field).
- h) Print the number of fields in each line, followed by the line. i) Sort the login names of all users and print it.
- j) Print only lines of less than 65 characters. k) Print line number 12.
- Solution(HOME):-
- Sed assignment:
- 1)a) $ sed „/./=‟ file| sed „/./N ; s/\n/ /‟
- b) $ sed -n „$=‟ file
- c) $ sed -e :a -e „/\\$/N ; s/\\\n// ; ta‟ file
- 50
- d) $ sed „n;n;n;n;G;‟ file
- e) $ sed -n „/MCA/{n;p;}‟ file
- f) $ sed -n „5p‟ file
- g) sed -n „3,${p;n;n;n;n;n;n;}‟ file
- awk assignment:
- 2)a) $ awk „END{print NR}‟ file
- b) $ awk „{s=0; for(i=1 ; i<=NF ; i=i+1) s=s+i ; print s}‟ file
- c) $ awk „{for(i=1 ; i<=NF ; i=i+1) s= s+i ; END{print s}‟ file
- d) $ awk „{for(i=1 ; i<=NF ; i=i+1) print “ “ i ; printf “\n%s\n”, $0}‟ file
- e) $ awk „{total=total+NF} ; END{print total}‟ file
- f) $ awk „/MCA/{n++} ; END {print n}‟ file
- g) $ awk „$1>max1{max=$1 ; max2=$0} ; END{print max1,max2}‟ file
- h) $ awk „{print NF “:” $0}‟ file
- i) $ awk -F “:” „{print $1 | “sort”}‟ /etc/passwd
- j) $ awk „length<65‟ file
- k) $ awk „NR==12‟ file
- 51
- Day 12
- Lab Assignments:
- 1. A text file contains information in the following format in each line of the text: Date Product Code Quantity Indicator (In/Out)
- ------ ----------------- ----------- ---------------------
- | | | |
- | | | |
- | | | |
- Write a shell script to calculate the cumulative stock of any particular product (either input from keyboard or command line argument) at any date.
- 2. A file contains marks of examination in the following format:
- Name Subject1 Subject2 Subject3 Subject4 Subject5
- ------- ---------- ---------- ----------- ----------- -----------
- . . . . . .
- . . . . . .
- . . . . . .
- Write a shell script to arrange the records according to the total marks of all subjects in the descending order.
- Solution : (LAB)
- 1) stock database:(stocklist.txt)
- 52
- Date Productcode Quantity indicator(in/out)
- 07/11/09 001 25 In
- 07/11/09 002 30 Out
- 07/11/09 001 50 In
- 11/11/09 003 60 In
- 12/11/09 002 70 Out
- 12/11/09 002 75 In
- 11/11/09 003 80 Out
- Shell script:
- #!/bin/sh
- #Program: stock.sh
- #Purpose: To calculate the cumilitive stock of any particular product at any date.
- #Usage: sh stock.sh stocklist.txt if test $# -ne 1
- then
- echo “Enter the database file as input in command line.”
- exit fi
- awk 'BEGIN { printf "\nEnter date:" getline c <"/dev/tty"
- printf "\nEnter code:"
- getline c1 <"/dev/tty"
- printf "Totalstock\n"
- }
- $1==c && $2==c1{x=x+$3} END{printf "%d\n",x} ' $1
- #end of script
- 2) student database: (studentinfo.txt)
- Name Sub1 Sub2 sub3 sub4 sub5
- ajay 56 58 69 70 75
- bimal 66 67 70 85 98
- ajoy 87 88 89 90 92
- 53
- raj 55 54 45 50 57
- komol 78 77 71 82 83
- sam 91 92 93 55 50
- shell script:
- #! /bin/sh
- #Program: student.sh
- #Purpose: To arrange the records according to the total marks of all subjects in descending order.
- #Usage: sh student.sh studentinfo.txt if test $# -ne 1
- then
- echo “Enter the database file as input in command line.”
- exit fi
- awk ' BEGIN {printf "NAME Total marks\n"} NR>1{
- x=0
- for(i=2;i<=NF;i++)
- x=x+$i
- printf "\n%s\t%d",$1,x|"sort -nr -k 2"
- } ' $1
- #end of script
- Home Assignments:
- 1. Write a shell function size( ) which lists only the total size of the files(filenames supplied as arguments).
- 2. Write a shell script for renaming each file in the directory such that it will have the current shell
- PID as an extension. The shell script should ensure that the directories do not get renamed.
- 3. Write a shell script to sort a list of elements using bubble sort technique.
Add Comment
Please, Sign In to add comment