Module 2
Module 2
Module 2
– For E.g :
Owner Group Others
[ R W E] [ R W E] [ R W E]
Binary 1 1 1 1 - 1 - - 1
Octal 7 5
1
• E.g
• before applying chmod
• Ls –I chap01
• -r w - - - - - - - 1 A B 100 12 may 2012 10.30 chap01
• When you log into the system you are given a default shell.
• When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command
aliases, and executes any commands specified in these files.
• The original shell was the Bourne shell, sh. Every Unix platform will
either have the Bourne shell, or a Bourne compatible shell
available.
• Numerous other shells are available.
Examples:
• $ls chap0[124]
• Matches chap01, chap02, chap04 and lists if found.
• $ ls chap[x-z]
• Matches chapx, chapy, chapz and lists if found.
• You can negate a character class to reverse a
matching criteria.
Example:
• Assuming file2 doesn’t exist, the following
command redirects the standard output to file
myOutput and the standard error to file myError.
• $ ls –l file1 file2 1>myOutput 2>myError
• To redirect both standard output and standard
error to a single file use:
• $ ls – l file1 file2 1>| myOutput 2>| myError
OR
• $ ls –l file1 file2 1> myOutput 2>&
Filters: Using both standard input and standard
output
• UNIX commands can be grouped into four
categories
• Example
• $ ls -a | more
• $ who | sort | lpr
When a command needs to be ignorant of its source
Example:
• $ echo Current date and time is `date`
• Observe the use of back quotes around date in the
previous command.
• Here the output of the command execution of date is
taken as argument of echo.
• The shell executes the enclosed command and
replaces the enclosed command line with the output
of the command.
• Similarly the following command displays the total
number of files in the working directory.
• $ echo “There are `ls | wc –l` files in the current
directory”
• Observe the use of double quotes around the
argument of echo. If single quotes are used, the
backquote is not interpreted by the shell if enclosed in
single quotes.
Shell variables
• Environmental variables are used to provide
information to the programs you use.
• You can have both global environment and local shell
variables.
Environment variable
⚫ Environment variables control the behaviour of
the system. They determine the environment in
which user work.
Ordinary Variable
⚫ Ordinary variables are local to a particular user’s
shell. These variables exist only for a short time
during the execution of a shell script.
⚫ They are local to the user’s shell environment
and are not available for the other scripts or
processes.
⚫ As these variables are defined and used by
specific users, they are also called user-defined
variables.
⚫ For example, one could set a variable called sum
⚫ Variablesare defined using an equal to (=) operator
without any spaces on either side of it.
⚫ The general format of variable declaration is :
variable=value.
⚫ The value of variables are stored in the ASCII
format.
Read can be used with one or more variables, ex: read pname flname
Readonly commands
[root@localhost ~]# function hello() {
• Variables and functions can be made echo "hello"; }
readonly. [root@localhost ~]# readonly -f hello
[root@localhost ~]# readonly a=10 [root@localhost ~]# function hello() {
[root@localhost ~]# echo $a echo "hello world"; }
10 bash: hello: readonly function
[root@localhost ~]# set a=20
[root@localhost ~]# echo $a • Can unset readonly function
10 [root@localhost ~]# unset hello
• Cannot unset a readonly variable, [root@localhost ~]# function hello() {
solution is to kill the shell echo "hello world"; }
[root@localhost ~]#
[root@localhost ~]# unset a
bash: unset: a: cannot unset: readonly
variable
Using Command line arguments
exit and exit status of command
• exit 0 when everything went fine
• exit 1 when something went wrong
$cat foo
cat: can’t open foo
• The shell offers $? and test that evaluates a commands exit
status.
$grep director emp.lst > /dev/null; echo $?
0 success
$grep manager emp.lst > /dev/null; echo $?
1 failure in finding pattern
$grep manager emp3.lst > /dev/null; echo $?
grep : can’t open emp3.lst failure in opening
file
2
The Logical Operators && and ||
[ $x –eq $y ]
▪ White spaces are required.
String comparison
File Tests
The case conditional
The case conditional
The while loop
The while loop
$emp5.sh
Enter the code and description: 03 analgesics
Enter any more (y/n)? n
for : looping with a list
for : looping with a list
#!/bin/sh
#emp6.sh -- using for loop with positional parameters