Module 2
Module 2
Module 2
The ls command is to obtain a list of all file names in the current directory.
ls with options:
Option Description
-x Multicolumnar Output
-F Marks executables with *, directories with / and symbolic link
with @
-a Shows all filenames beginning with a dot including . and ..
-R Recursive list
-r Sorts filenames in reverse order(ASCII collating sequence by
default)
-l Long listing in ASCII collating sequence seven attributes of a
file.
-d dirname Lists only dirname if dirname is a directory.
-t Sort filenames by last modification time.
-lt Sorts listing by last modification time.
-u Sorts filenames by last access time.
-lu Sorts by ASCII collating sequence but listing shows last access
time.
-lut As above but sorted by last access time.
-i Displays inode number.
ls look up the file‘s inode to fetch its attributes. It lists seven attributes of all files in the current
directory and they:
1. File type & Permission:
The first column shows the type and permissions associated with each file.
The first character in this column is mostly a -, which indicates that file is an
ordinary one.
2. Links:
The second column indicates the number of links associated with the file.
This is actually the number of filenames maintained by the system of that
file.
3. Ownership:
When you create a file, you automatically become its owner. The third column
shows kumar is the owner of all of these files. The owner has full authority to
tamper with a files contents and permissions—a privilege not available with
others except the root user.
4. Group Ownership:
The fourth column represents the group owner of the file. Every user is attached
to a group owner. Every member of that group can access the file depending
on the permission assigned.
5. File size:
File size in bytes is displayed. It is the number of character in the file rather
than the actual size occupied on disk.
Example:
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
2. Absolute mode
• This command is used to add/delete permission for all type of users(owner,
group or others)
• This command is also used to change all permissions in the command line.
• Syntax:
• Chmod octal_value filename
• This command takes 2 arguments:
1. Octal_value contains 3 octal digits to represent 3 types of users(owner,
group or others)
a. First digit is for the user
b. Second digit is for the group
c. Third digit is for others
• Each digit represents a permission as shown below:
4(100) – read only
2(010) – write only
1(001) – execute only
6(110) – read and write only
2.1Wild Cards
The meta characters that are used to construct the generalized pattern for matching
filenames belong to a category called wild-cards. The following table lists them:
Metacharacters * and ?
To list all files that begin with chap, use, $ls chap*
To list all files whose filenames are six character long and start with chap, use, $ls
chap??
Ex:
$ ls chap? // to list all 5 characters filenames beginning with “chap”
Chapx, chapy, chapz
$ ls chap?? // to list 6 characters filenames beginning with “chap”
Chap01, chap02, chap03
Character class
It comprises of a set of characters enclosed by the rectangular brackets’[‘ and ‘]’.
It matches a single character in the class.
Example:
Character class Match
[ijk] A single character either an I, j ork
[X-Z] A single character that is within the ASCII
range of the characters X and Z
Chap0[124] Chap01, chap02, chap04
[!ijk] A single character that is not n I, j or k
[!X-Z] A single character that is not within the
ASCII range of the characters x and z
ii)Standard output: The file representing output which is representing the display.
It represents 3 possible destinations:
a) The terminal, the default destination.
b) A file using the redirection symbols > and >>
c) As input to another program using a pipeline
By default, the shell directs standard output from a command to the screen.
Ex:
$cat sample.txt
Hello
World
$ wc sample.txt
2 2 10
The redirect output symbol (>) instructs the shell to redirect the output of a command to the specified file
instead of to a screen.
Ex:
$ wc sample.txt > temp.txt
$ cat temp.txt
2 2 10 // output of wc stored in temp.txt.
“>>” can be used to append to a existing file.
iii)Standard error: The file representing error messages that come from the command or shell. This file is
also connected to the display.
By default, the shell directs standard error from a command to the scree.
Ex: cat empty.txt
Cat: cannot open empty.txt
The redirect output instructs the shell to redirect the error message of a command to the specified file instead
of to the screen.
Ex: cat empty.txt >> errorfile.txt
$ cat errorfile.txt
Cat: cannot open empty text. // error message stored in errorfile.txt
Example:
$ grep -i “[Mm][Hh]” demo_file // matches all the words such as MH mH Mh mh
2.2.2.2 Asterisk(*):
The asterisk(*) refers to the immediately preceding character.
It indicates zero or more occurrences of the previous character.
G* -> nothing or g, gg, ggg, etc.
Lg* -> l or lg, lgg, lggg, etc.
Example:
$ grep “isaa*c” demo_file // matches isac Isaac or isaaac
2.2.2.3 Dot(.):
• A dot matches single character.
• Example:
$ grep “2…” demo_file // matches all 4 character words beginning with 2
• Regular expression “.*” -> signifies any number of characters or none
• Ex:
$ grep prog.c.* demo_file // matches all c and cpp extension filenames
Metacharacter/character Match
class
Ch+ One or more occurrences of character ch
Ch? Zero or one occurrence of character ch
Exp1|exp2 Exp1 or exp2
(x1|x2)x3 X1x3 or x2x3
• If current version of grep doesn’t support ERE, then use egrep but without the -E option.
• -E option treats pattern as ERE.
• Example:
$ grep -E “isaa*c” demp_file // matches isac Isaac
$ grep -E ‘vijaykumar | jayakumar’ demo_file //matches multiple patterns
$ grep -E ‘(vijay | jaya) kumar’ demo_file // matches multiple patterns.
2.2.4 grep:
• g/re/p means “globally search for a regular expression and print all lines containing it”.
• This command is used to search a file(s) for lines that have a certain pattern.
• This command scans for the pattern in a file
Syntax:
Grep pattern filename(s)
Example:
Grep “MH” student.lst // display lines containg “MH” from the file student.lst
▪ Patterns with and without quotes is possible.
▪ Quote is mandatory when pattern involves more than one word.
▪ Example:
Grep “My Document” student.lst // display lines containing “My Document” from student.lst.
▪ This command can be used with multiple filenames, where it displays the filenames along with the
output.
▪ Example:
Grep “MH” student.lst vtu.lst rank.lst
2.2.4.1 grep options:
Linux supports below listed options.
Option Significance
-i Ignores case for matching.
-v Doesn't display lines matching expression
-n Displays line numbers along with lines
-c Displays count of number of occurrences
-l Displays list of filenames only.
-e exp Matches multiple patterns
-f Takes patterns from file, one per line
filename
-E Treats patterns as an ERE
-F Matches multiple fixed strings
To understand the working of different options, let us consider we have following information in the file
student.lst
$ cat student.lst
4 | MH | 10 | IS | 111
4 | MH | 11 | CS | 401
4 | GW | 11 | CS | 402
4 | VV | 11 | CS | 403
Ignore case:
-I (ignore) option is used to search all lines containing a pattern regardless ofd uppercase and lowercase
distinction.
Example:
$ grep -I “MH” demo_file // matches all the words such as MH mH Mh mh
4 | MH | 10 | 111
4 | MH | 11 | 401
Deleting Lines:
-v option is used to print all lines that do not contain the specified pattern in a file.
Ex:
Grep -v ‘MH’ student.lst
4 | GW | 11 | CS | 402
4 | VV 11 | CS | 403
Displaying Line Number:
-n(line number is used to display line numbers containing the pattern.
Example:
Grep -n ‘MH’ STUDENT.LST
1
2
Counting Lines Containing the Pattern:
-c(line count) option is used to count number of lines containing the pattern.
Example:
Grep -c ‘MH’ student.lst
2
Displaying filenames:
-l(list filename) option is used to list out the files containing the pattern.
Example:
Grep -l ‘MH’ *.lst
Matching Multiple Pattern:
-e option is used to match multiple pattern in a file.
Example:
Grep -e ‘MH’ -e ‘VV’ student.lst
Taking Pattern from File:
-f option is used to place all matched pattern in a separate file, one pattern per line.
Chapter- 3: Shell Programming
3.1 Introduction
1. A shell is a program that acts as the interface between user & the linux system,
allowing user to enter commands for the operating system to execute. A linux shell is
both a command interpreter and a programming language.
2. Shells may be used interactively or non-interactively, in interactive mode, they
accept input typed from the keyboard. When executing non- interactively, shells
execute commands read from a file.
3. The most commonly used shells are SH(Bourne Shell) CSH(C Shell) and
KSH(Korn Shell). KSH is based on SH & so is BASH(Bourne again shell).
TCSH(Extended C Shell) is based on CSH.
8) SHELL
• It specifies the current shell being used by the users.
• Different types of shells are Bourne shell, C-shell and Korn shell
• This variable is set for the user by the system admin in /etc/passwd.
9) TERM
a. This indicates the terminal type that is used.
b. Every terminal has certain characteristics that are defined in a separate control file in the
terminfo directory.
c. If TERM is not set correctly, vi will not work and display will be faulty.
The read statement is the shell’s internal tool for making scripts interactive (i.e. taking input fromthe
user). It is used with one or more variables.
Inputs supplied with the standard input are read into these variables. For instance, the use of
statement like read name, causes the script to pause at that point to take input from the keyboard.
Whatever is entered by youwill be stored in the variable name.
Example: A shell script that uses read to take a search string and filename from the terminal.
#! /bin/sh
\c”read pname
# use echo –e in
bash
echo “Enter the file to be used:
\c”read fname
Running of the above script by specifying the inputs when the script pauses twice:
$ emp1.s
Enter the pattern to be searched :
directorEnter the file to be used:
emp.lst
Searching for pattern director from the file emp.lst
Shell scripts also accept arguments from the command line. Therefore, they can be run
noninteractively and be used with redirection and pipelines.
The arguments are assigned to special shell variables. Represented by $1, $2, etc; similar
to Ccommand arguments argv[0], argv[1], etc.
The following table lists the different shell parameters.
Shell parameter
Significance
$1, $2… Positional parameters representing command line arguments
Exit status is used to devise program logic that braches into different paths depending on
success or failure of a command.
3.6 Logical Operators for Conditional Execution
The shell provides two operators that aloe conditional execution, the && and ||.
Usage:
cmd1 && cmd2
cmd1 || cmd2
&& delimits two commands. cmd 2 executed only when cmd1 succeeds.
Example 1:
$ grep ‘director’ emp.lst && echo “pattern found
Output:
Pattern found
Example 2:
$ grep ‘clerk’ emp.lst || echo “Pattern Not Found”
Output: Pattern Not Found
Example 3:
$ grep “$1” $2 || exit2
Echo “Pattern found and job over”
3.6.1 String Comparison:
Test command is also used for testing strings.
Test can be used to compare strings with the following set of comparison operators as listed
below.
Test True if
s1=s2 String s1=s2
s1!=s2 String s1 is not equal to s2
-n stg String stg is not a null string
-z stg String stg is a null string
stg String stg is assigned and not null
s1= =s2 String s1=s2
#!/bin/sh
#emp1.sh checks user input for null values finally turns emp.sh developed
previously#
if [ $# -eq 0 ] ; then
echo “Enter the string to be searched
:\c”read pname
if [ -z “$pname” ] ; then
echo “You have not entered th e string”; exit
1fi
echo “Enter the filename to be used
:\c”read flname
if [ ! –n “$flname” ] ; then
echo “ You have not entered the flname” ; exit
2fi
emp.sh “$pname”
“$flname”else
emp.sh
$*fi
Output1:
$emp1.sh
Enter the string to be searched :[Enter]
You have not entered the string
Output2:
$emp1.sh
Enter the string to be searched :root
Enter the filename to be searched :/etc/passwd
Root:x:0:1:Super-user:/:/usr/bin/bash
When we run the script with arguments emp1.sh bypasses all the above activities and calls emp.shto
perform all validation checks
$ emp1.sh jai
9878|jai sharma|director|sales|12/03/56|70000
$emp1.sh “jai sharma” emp.lst You didn’t
Because $* treats jai and sharma are separate arguments. And $# makes a wrong argument count.
Solution is replace $* with “$@” (with quote” and then run the script.
Test is used to test various file attributes like its type(file, directory or symbolic links)or its
permission( read, write, Execute, SUID, ETC)
Example:
$ ls -l emp.lst
Example: filetest.sh
#! /bin/usr
Else
Output:
$ filetest.sh emp3.lst
$ filetest.sh emp.lst
Test True if
The if statement makes two way decisions based on the result of a condition. The following
forms of IF are available in the shell:
if command is If command is If command is
successful then successful then successful then
execute commands execute commands execute
else fi commands elif
execute commands command is
fi successful then
execute commands
else
execute commands
fi
Form 1 Form 2 Form 3
Example:
#!/bin/sh
# shell script to illustrate
#! /bin/usr
Ans =y
While[“$ans “=” “y”]
Do
Echo “Enter the code and description: \c” > /dev/tty
Read code description
Echo “$code $description” > newlist
Echo “Enter any more [Y/N]”
Read any
Case $any in
Y * | y* ) answer = y;;
N * | n* ) answer = n;;
*) answer = y;;
Esac
done
Input:
Enter the code and description : 03 analgestics
Enter any more [Y/N] :y
Enter the code and description : 04 antibiotics
Enter any more [Y/N] : [Enter]
Enter the code and description : 05 OTC drugs
Enter any more [Y/N] : n
Output:
$ cat newlist
03 | analgestics
04 | antibiotics
05 | OTC Drugs
Output:
Ch1 copied to ch1.bak
Ch2 copied to ch2.bak
3.9.1 Sources of list:
List from variables: Series of variables are evaluated by the shell before executing the loop.
Example:
$ for var in $PATH $HOME; do echo “$var”; done
Output:
/bin:/usr?bin;?home?local?bin;
/home/user1
List from command substitution: Command substitution is used for creating a list. This is used when
the list is large.
Example:
$ for var in ‘cat clist’
List from wildcards: Here, the shell interprets the wildcards as filenames.
Example: $ for in *.htm*.html; do
Sed ‘s/strong/STRONG/g
s/img src/ING SRC/’ $file $$
mv $$ file
done.
List form positional parameters:
Example: emp.sh
#! /bin/sh
For pattern in “$@”; do
Grep “$pattern” emp.lst || echo “pattern $pattern not found”
Done
Output:
$emp.sh 9876 “Rohit”
9876 Jai Sharma Director Productions
3.10 The case conditional
The case statement is the second conditional offered by the shell. It doesn’t have a parallel
either inC (Switch is similar) or perl.
The statement matches an expression for more than one alternative, and uses a compact
construct topermit multiway branching.
case also handles string tests, but in a more efficient manner than if.
Syntax:
Case expression in
Pattern 1) commands1 ;;
Pattern 2) commands2 ;;
Pattern 3) commands3 ;;
…
esac
Case first matches expression with pattern1. if the match succeeds, then it executes
commands1,which may be one or more commands.
If the match fails, then pattern2 is matched and so forth.
Each command list is terminated with a pair of semicolon and the entire construct is closed
withesac (reverse of case).
#! /bin/sh
#
Echo “ Menu\n
1. List of files\n 2. Processes of user\n 3. Today’s date
4. Users of system\n 5. Quit\n Enter your option: \c”
Read choice
Case “$choice” in
1) Ls -l;
2) Ps -f;
3) Date;;
4) Who;;
5) Exit;;
*) echo “Invalid option”
esac
Output:
$ menu.sh
Menu
1. List of files
2. Processes of user
3. Today’s date
4. Users of systemest a user response for both y and Y(or
5. Quit
Enter your option: 3
Mon oct 8 08:02:45 IST 2007
Matching multiple patterns:
Case can also specify the same action for more than one pattern. For instance to test a user response for both
y and Y (or n and N).
Example:
Echo “Do you wish to continue? [y/n]: \c”
Read ans
Case “$ans” in
Y | y);;
N | n) exit ;;
Esac
Wild cards: case uses them:
Case has a string-matching feature that uses wild-cards
It uses the filename matching metacharacters *, ? and character class( to match only strings and not files in
the current directory).
Example:
Case “$ans” in
[Yy] [eE]* );; // matches YES, Yes, yes,yEs, etc…
[Nn] [oO]) exit;; // matches no, NO, No, nO
➢ The set statement assigns positional parameters $1, $2 and so on, to its arguments. This is used
forpicking up individual fields from the output of a program.
➢ Shift transfers the contents of positional parameters to its immediate lower numbered one.
➢ This is done as many times as the statement is called. When called once, $2 becomes $1,
$3becomes S2 and so on.
Example:
$echo $1 $2 $3
Mon Oct 8
$ shift
$echo $1 $2 $3
$echo $1 $2 $3