17CS35 Module 4 - Shell Programming Files Inode and Filters - VTUPulse
17CS35 Module 4 - Shell Programming Files Inode and Filters - VTUPulse
UNIX Shell Programming 15CS35 Shell Programming Files inode and Filters
1. SHELL SCRIPT
When a group of commands have to be executed regularly, they should be stored in a file, and the
file itself executed as a shell script or shell program.
We use .sh extension for all shell scripts.
Every Shell script begins with special interpreter line like - #!/bin/sh or #!/bin/ksh or #!/bin/bash
This interpreter line specify which shell (Bourne/Korn/Bash) user prefer to execute shell script,
and it may or may not be same as user login shell.
Example:
#!/bin/sh
#Sample Shell Script – sample.sh
Example:
#!/bin/sh
#Shell Script to demonstrate command line arguments - sample.sh
echo "The Script Name is: $0"
echo "Number of arguments specified is: $#"
echo "The arguments are: $*"
echo "First Argument is: $1"
echo "Second Argument is: $2"
echo "Third Argument is: $3"
echo "Fourth Argument is: $4"
echo "The arguments are: $@"
Execution & Output:
$ sh sample.sh welcome to hit nidasoshi [Enter]
The Script Name is: sample.sh
Number of arguments specified is: 4
The arguments are: welcome to hit nidasoshi
First Argument is: welcome
Second Argument is: to
Third Argument is: hit
Fourth Argument is: nidasoshi
The arguments are: welcome to hit nidasoshi
Examples:
$ grep director emp.lst >/dev/null; echo $?
0 #Success
$ grep director emp.lst >/dev/null; echo $?
1 #Failure – in finding pattern
6. THE if CONDITIONAL
The if statement makes two-way decisions depending on the fulfillment of a certain condition.
In the shell, the statement uses the following forms-
Example:
#!/bin/sh
#Shell script to illustrate if conditional
if grep 'director' emp.lst >/dev/null
then
echo “Pattern found in File!”
else
echo “Pattern not-found in File!”
fi
Numeric Comparison
Numerical Comparison operators used by test:
Operator Meaning
-eq Equal to
The numerical comparison operators used by test always begins with a - (hyphen), followed by a
two-letter string, and enclosed on either side by whitespace.
Examples:
$ x=5, y=7, z=7.2
$ test $x -eq $y; echo $?
1 # Not Equal
$ test $y -eq $z
0 #True- 7.2 is equal to 7
The last example proves that numeric comparison is restricted to integers only.
The [ ] is used as shorthand for test.
Hence, above example may be re-written as-
test $x -eq $y or [ $x -eq $y ] #Both are equivalent
String Comparison
test can be used to compare strings with yet another set of operators.
The below table shows string tests used by test-
Test True if
s1 = s2 String s1 = s2
Example:
#!/bin/sh
#Shell script to illustrate string comparison using test – strcmp.sh
if [ $# -eq 0 ]; then
echo "Enter Your Name: \c"; read name;
if [ -z $name ]; then
echo "You have not entered your name! "; exit 1;
else
echo "Your Name From Input line is : $name "; exit 1;
fi
else
echo "Your Name From Command line is : $1 " ;
fi
$ sh strcmp.sh
Enter Your Name: Smith [Enter]
Your Name From Input line is : Smith
$ sh strcmp.sh
Enter Your Name: [Enter]
You have not entered your name!
case expression in
pattern1) command1 ;;
pattern2) command2 ;;
pattern3) command3 ;;
…........
esac
Example:
#!/bin/sh
#Shell script to illustrate CASE conditional – menu.sh
echo "\t MENU\n 1. List of files\n 2. Today's Date\n 3. Users of System\n 4. Quit\n";
echo "Enter your option: \c";
read choice
case "$choice" in
1) ls -l ;;
2) date ;;
3) who ;;
4) exit ;;
*) echo "Invalid Option!"
esac
Examples:
$ expr 3 + 5
8
$ x=8 y=4
$ expr $x + $y
12
$ expr $x - $y
4
$ expr $x \* $y #Asterisk has to be escaped to prevent from metacharacter
32
$ expr $x / $y
2
$ expr $x % $y
0
expr is often used with command substitution to assign a variable.
For example, you can set a variable z to the sum of two numbers:
$ x=6 y=2; z=expr `$x + $y`
$ echo $z
8
String Handling
For manipulating strings, expr uses two expressions separated by a colon.
The string to be worked upon is placed on the left of the :, and RE is placed on its right.
Depending on the composition of the expression, expr can perform three important string
functions:
Determine the length of the string
Extract a substring
Locate the position of a character in a string
Examples:
$ expr “abcdefghijkl” : '.*'
12 #Length of the string = number of characters
$ stg=2013
$ expr “$stg” : '..\(..\)'
13 #Extracts last two characters – as substring
$stg=abcdefgh
$ expr “$stg” : '[^d]*d'
4 #Locate position of character d in stg
Example:
#!/bin/sh
#Shell script to illustrate while loop – while.sh
answer=y;
while [ "$answer" = "y" ]
do
echo "Enter Branch Code and Name: \c"
read code name #Read both together
echo "$code|$name" >> newlist #Append a line to newlist
echo "Enter anymore (y/n)? \c"
read anymore
case $anymore in
y*|Y*) answer=y ;; #Also accepts yes, YES etc.
n*|N*) answer=n ;; #Also accepts no, NO etc.
*) answer=n ;; #Any other reply means no
esac
done
Execution & Output:
$ chmod 777 while.sh
$ sh while.sh
Enter Branch Code and Name: CS COMPUTER [Enter]
Enter anymore (y/n)? y [Enter]
Enter Branch Code and Name: EC ELECTRONICS [Enter]
Enter anymore (y/n)? n [Enter]
$ cat newlist
CS|COMPUTER
EC|ELECTRONICS
NOTE:
The shell also offers an until statement which operates with a reverse logic used in while.
With until, the loop body is executed as long as the condition remains false.
Example:
#!/bin/sh
#Shell script to illustrate use of for loop – forloop.sh
for file in chap1 chap2 chap3 chap4
do
cp $file ${file}.bak
echo “$file copied to $file.bak”
done
Execution & Output:
$ chmod 777 forloop.sh
$ sh forloop.sh
chap1 copied to chap1.bak
chap2 copied to chap2.bak
chap3 copied to chap3.bak
chap4 copied to chap4.bak
This is a text file designed in fixed format and containing a personnel database.
There are 15 lines in the file, where each line has six fields separated from one another by the
delimiter |.
The details of an employee are stored in one line.
A person is identified by emp_id, name, designation, department, date of birth and salary.
The below example shows how to cut the second and third fields from file shortlist
example:
$ cut -d \| -f 2,3 shortlist
a. k. shukla |g. m.
jai sharma |director
sumit chakrobarty |d. g. m.
barun sengupta |director
n. k. gupta |chairman
17. paste: PASTING FILES
What you cut with cut can be pasted back with the paste command – but vertically rather than
horizontally.
You can view two files side by side by pasting them.
Example:
$ cut -d \| -f 2,3 shortlist | tee cutlist1
a. k. shukla |g. m.
jai sharma |director
sumit chakrobarty |d. g. m.
barun sengupta |director
n. k. gupta |chairman
$ cut -d \| -f 5,6 shortlist | tee cutlist2
12/12/52|6000
12/03/50|7000
19/04/43|6000
11/05/47|7800
30/08/56|5400
Option Description
-n Sorts numerically
-f Case-insensitive sort
Observe that neither the name of the file nor the inode number is stored in the inode. It's the
directory that stores the inode number along with the filename. When you use a command with a
filename as argument, the kernel first locates the inode number of the file from the directory and
then reads the inode to fetch data relevant to the file.
Every file system has a separate portion set aside for storing inodes, where they are laid out in a
contiguous manner. This area is accessible only to the kernel. The inode number is actually the
position of the inode in this area. The kernel can locate inode number of any file using simple
arithmetic. Since a UNIX machine usually comprises multiple file systems, you can conclude that
the inode number for a file is unique in a single file system.
The ls command can be used with -i(inode) option to know the inode number of any file present
in the UNIX file system.
Example:
$ ls -il test.c
13109062 -rwxrwxrwx 1 chandrakant chandrakant 639 Dec 10 13:18 test.c
The file test.c has the inode number 13109062. This inode number is unique in this file system.
Here, you can identify symbolic link by the character l seen in the second (permissions) field of ls
-l output.
The pointer notation ->emp.lst suggest that employee contains the pathname for the filename
emp.lst.
It's emp.lst, not employee, that actually contains the data.
When you use cat employee, you don't actually open the symbolic link, employee, but the file the
link points to.
Examples:
$ mkdir sample #Create new directory, sample
$ ls -l -d sample #Check directory attributes by ls -l -d option
drwxrwxr-x 2 chandrakant chandrakant 4096 Jan 26 19:51 sample
Here,
first character in permissions field is d which shows file is of directory type.
Read Permission
Read permission for a directory means that the list of filenames stored in that directory is
accessible.
Since ls reads the directory to display filenames, if a directory's read permission is
removed, ls won't work.
Consider removing the read permission first from the directory sample.
Example:
$ ls -ld sample #Before removing read permission of sample
drwxrwxr-x 2 chandrakant chandrakant 4096 Jan 26 19:51 sample
$ chmod -r sample #Removes read permission of sample
$ ls -ld sample #After removing read permission of sample
sample: Permission denied
Write Permission
Be aware that you can't write to a directory file; only kernel can do that.
Write permission for a directory implies that you are permitted to create or remove files
in it.
To try that out, restore the read permission and remove the write permission from the
directory sample.
Example:
$ chmod 555 sample #Restore read permission $ Removes write permission
$ ls -ld sample
dr-xr-xr-x 2 chandrakant chandrakant 4096 Jan 26 19:51 sample
$ cp emp.lst sample
cp: cannot create regular file 'sample/emp.lst' : Permission denied
The directory sample doesn't have write permission; you can't create copy or delete a file in it.
Execute Permission
Executing a directory just doesn't make any sense, so what does its execute privilege
mean?
It only means that a user can “pass through” the directory in searching for subdirectories.
When you use a pathname with any command – cat /home/chandrakant/sample/emp.lst
you need to have execute permission for each of the directories involved in the complete
pathname.
The directory home contains entry for chandrakant, and the directory chandrakant
contains entry for sample, and so forth.
If a single directory in this pathname doesn't have execute permission, then it can't be
searched for the name of the next directory.
That's why the execute privilege of a directory is often referred to as the search
permission.
Example:
$ chmod 666 sample #Restore write permission & Removes execute permission
$ ls -ld sample
drw-rw-rw- 2 chandrakant chandrakant 4096 Jan 26 19:51 sample
$ cd sample
cd: sample: Permission denied
The touch command changes these times, and is used in the following manner:
touch options expression filename(s)
when touch is used without option or expression, both times are set to the current times.
The -m and -a options change the modification and access times, respectively.
The expression consists of an eight-digit number using the format MMDDhhmm (month, day,
hour and minute).
Examples:
$ touch 03161430 emp.lst #Change both modification and access time
$ ls -l emp.lst
-rw-rw-r-- 1 chandrakant chandrakant 80 Mar 16 14:30 emp.lst
$ touch -m 03161430 emp.lst #Change modification time
$ ls -l emp.lst
-rw-rw-r-- 1 chandrakant chandrakant 80 Mar 16 14:30 emp.lst