2. File attributes and permissions
Basic File Attributes
• The UNIX file system allows the user to access other
files not belonging to them and without infringing on
security.
File Ownership
• When you create a file, you become its owner. Every
owner is attached to a group owner. Several users may
belong to a single group, but the privileges of the group
are set by the owner of the file and not by the group
members.
3. Cont..
When the system administrator creates a user account,
he has to assign these parameters to the user:
• The user-id (UID) – both its name and numeric
representation
• The group-id (GID) – both its name and numeric
representation.
• The file /etc/passwd maintains the UID and GID.
/etc/group contains the GID.
• The command “id” can be used to know your own UID
and GID.
4. Cont..
File Permissions
• UNIX follows a three-tiered file protection system that
determines a file’s access rights. It is displayed in the
following format:
• Filetype owner (rwx)
• groupowner (rwx)
• others (rwx)
• For Example:
-rwxr-xr-- 1 kumar metal 20500 may 10 19:21 chap02
r w x r – x r - -
owner/user group owner others
5. Cont..
• The first group has all three permissions. The file is readable,
writable and executable by the owner of the file.
• The second group has a hyphen in the middle slot, which
indicates the absence of write permission by the group owner of
the file.
• The third group has the write and executes bits absent. This set
of permissions is applicable to others.
• You can set different permissions for the three categories of
users – owner, group and others.
6. Changing File Permissions
Changing File Permissions
• A file or a directory is created with a default set of permissions,
which can be determined by umask.
• chmod command is used to set the permissions of one or more
files for all three categories of users. It can be run only by the
owner and the administrator.
• The command can be used in two ways:
- (i) In a relative manner by specifying the changes to the
current permissions
-(ii) In an absolute manner by specifying the final permissions
7. Cont..
Relative Permissions
• chmod only changes the permissions specified in the
command line and leaves the other permissions
unchanged. The syntax is:
chmod category operation permission filename(s)
• chmod takes an expression as its argument which
contains:
• user category (user, group, others)
• operation to be performed (assign or remove a
permission)
• type of permission (read, write, execute)
8. Cont..
Let us discuss some examples:
Initially,
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart
chmod u+x xstart
-rwxr--r-- 1 kumar metal 1906 sep 23:38 xstart
The command assigns (+) execute (x) permission to the user
(u), other permissions remain unchanged.
9. Cont..
chmod ugo+x xstart or
chmod a+x xstart or
chmod +x xstart
The above 3 commands assigns execute permissions to the
user group and others.
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
chmod accepts multiple file names in command line
chmod u+x note note1 note3
assign execute permission to to user of the files note1,note2
and note3.
10. Cont..
Let initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
chmod go-r xstart
Then, it becomes
-rwx--x--x 1 kumar metal 1906 sep 23:38 xstart
Absolute Permissions
Here, we need not to know the current file
permissions. We can set all nine permissions
explicitly. A string of three octal digits is used as an
expression.
11. Cont..
The permission can be represented by one octal digit
for each category. For each category, we add octal
digits.
If we represent the permissions of each category by
one octal digit, this is how the permission can be
represented:
• Read permission – 4 (octal 100)
• Write permission – 2 (octal 010)
• Execute permission – 1 (octal 001)
12. Cont..
We have three categories and three permissions for each
category, so three octal digits can describe a file’s permissions
completely.
The most significant digit represents user and the least one
represents others.
13. Cont..
chmod can use this three-digit string as the
expression.
Using relative permission, we have,
chmod a+rw xstart
Using absolute permission, we have,
chmod 666 xstart
chmod 644 xstart
chmod 761 xstart
will assign all permissions to the owner, read and
write permissions for the group and only execute
permission to the others.
14. Cont..
To give yourself and your goup members full acces, you
use:
chmod 770 participants
If you want to keep full access for yourself, but want to
keep other people from modifying the file, you can use:
chmod 755 participants
Use option -R to change the permission recursively as
shown below.
chmod -R 755 directory-name/
15. Cont..
777 signify all permissions for all categories, but still
we can prevent a file from being deleted.
000 signifies absence of all permissions for all
categories, but still we can delete a file. It is the
directory permissions that determine whether a file can
be deleted or not.
Only owner can change the file permissions. User can
not change other user’s file’s permissions. But the
system administrator can do anything.
16. The Security Implications
The Security Implications:
Let the default permission for the file xstart is
-rw-r--r--
chmod u-rw, go-r xstart or
chmod 000 xstart
----------
This is simply useless but still the user can delete this
file On the other hand,
chmod a+rwx xstart
chmod 777 xstart
-rwxrwxrwx
The UNIX system by default, never allows this situation
as you can never have a secure system.
Hence, directory permissions also play a very vital role
here
17. Directory Permissions
Directory Permissions
It is possible that a file cannot be accessed even
though it has read permission, and can be removed
even when it is write protected. The default
permissions of a directory are,
rwxr-xr-x (755)
A directory must never be writable by group and others
Example:
mkdir c_progs
ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
18. Cont..
If a directory has write permission for group and others
also, be assured that every user can remove every file
in the directory.
As a rule, you must not make directories universally
writable unless you have definite reasons to do so.
19. Changing File Ownership
Changing File Ownership
Usually, on BSD and AT&T systems, there are two
commands meant to change the ownership of a file or
directory.
Let kumar be the owner and metal be the group owner.
If Sharma copies a file of kumar, then sharma will
become its owner and he can manipulate the attributes
chown changing file owner and chgrp changing group
owner.
On BSD, only system administrator can use chown.On
other systems, only the owner can change both.
20. Cont..
Changing ownership requires superuser permission, so use su
command
ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note
chown sharma note; ls -l note
-rwxr----x 1 sharma metal 347 may 10 20:30 note
Once ownership of the file has been given away to sharma, the
user file permissions that previously applied to Kumar now apply
to sharma.
Thus, Kumar can no longer edit note since there is no write
privilege for group and others.
He can not get back the ownership either. But he can copy the file
to his own directory, in which case he becomes the owner of the
copy.
21. Cont..
Chgrp command changes the file’s group owner. No superuser
permission is required.
ls –l dept.lst
-rw-r--r-- 1 kumar metal 139 jun 8 16:43 dept.lst
chgrp dba dept.lst; ls –l dept.lst
-rw-r--r-- 1 kumar dba 139 jun 8 16:43 dept.lst
22. The shells interpretive cycle
The shell sits between you and the operating system, acting as a
command interpreter.
It reads your terminal input and translates the commands into
actions taken by the system. 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.
23. Cont..
Numerous other shells are available. Some of the more well
known of these may be on your Unix system:
the Korn shell(/bin/ksh), by David Korn
C shell(/bin/csh), by Bill Joy
the Bourne Again Shell Bash(/bin/bash)
the T-C shell Tcsh (/bin/tcsh)
the extended C shell Cshe (/bin/cshe)
Even though the shell appears not to be doing anything
meaningful when there is no activity at the terminal, it swings into
action the moment you key in something.
When you log in you are in one of the five shells
24. Cont..
The system administrator determines which shell you
start in by an entry in the password file (/etc/passwd).
Even though your start up shell is determined by the
system administrator, you can always switch to another
shell.
The following example shows how to move to other
shells:
$ bash # Move to Bash shell
$ ksh # Move to Korn shell
$ csh # Move to C shell
25. Cont..
UNIX contains a system variable, SHELL that
identifies the path to your login shell. You can check it
with the command as follows:
$ echo $SHELL
/bin/ksh
The correct command to end the session at the login
shell is logout, but the exit command also terminates
the session
26. Cont..
The following activities are typically performed by the
shell in its interpretive cycle:
The shell issues the prompt and waits for you to enter
a command.
After a command is entered, the shell scans the
command line for metacharacters and expands
abbreviations (like the * in rm *) to recreate a
simplified command line.
It then passes on the command line to the kernel for
execution.
The shell waits for the command to complete and
normally can’t do any work while the command is
running.
27. Cont..
After the command execution is complete, the prompt
reappears and the shell returns to its waiting role to
start the next cycle. You are free to enter another
command.
28. Wild cards(Pattern matching) and file name
generation
A pattern is framed using ordinary characters and a
metacharacter (like *) using well-defined rules.
The pattern can then be used as an argument to the
command, and the shell will expand it suitably before
the command is executed.
The metacharacters that are used to construct the
generalized pattern for matching filenames belong to
a category called wild-cards.
The following table lists them:
30. Cont..
The * and ?
•Match any number (or none) of the single characters that
immediately precede it except whitespace. The preceding
character can also be a regular expression.
•For example, since
. (dot) means any character,
* means "match any number of any character."
? will match zero or one character except whitespace.
Examples:
To list all files that begins with chap, use
$ ls chap*
To list all files whose filenames start with chap, use
$ ls chap??
31. Cont..
The * and ?
•Match any number (or none) of the single characters that
immediately precede it except whitespace. The preceding
character can also be a regular expression.
•For example, since
. (dot) means any character,
* means "match any number of any character."
? will match zero or one character except whitespace.
Examples:
To list all files that begins with chap, use
$ ls chap*
To list all files whose filenames start with chap, use
$ ls chap??
32. Cont..
Matching the Dot(.)
.(dot)
Match any single character except newline.
Both * and ? operate with some restrictions. For example, the *
doesn’t match all files beginning with a . (dot) or the / of a
pathname.
If you wish to list all hidden filenames in your directory having
at least three characters after the dot, the dot must be matched
explicitly.
$ ls .???*
However, if the filename contains a dot anywhere but at the
beginning, it need not be matched explicitly.
Similarly, these characters don’t match the / in a pathname. So,
you cannot use
$ cd /usr?local to change to /usr/local.
33. Cont..
The character class
You can frame more restrictive patterns with the character class.
The character class comprises a set of characters enclosed by the
rectangular brackets, [ and ], but it matches a single character in
the class.
The pattern [abd] is character class, and it matches a single
character – an a,b or d.
Examples:
$ls chap0[124] - Matches chap01, chap02, chap04 and lists if
found.
You can specify range inside the class with a –(hyphen).
Examples:
$ls chap0[1-4] - Matches chap01, chap02,chap03 and chap04
and lists if found.
$ ls chap[x-z] - Matches chapx, chapy, chapz and lists if
found.
34. Cont..
You can negate a character class to reverse matching criteria
using !( exclamation mark).
For example,
To match all filenames with a single-character extension but not
the .c or .o files, use
*.[!co]
- To match all filenames that doesn’t begin with an alphabetic
character, use
[!a-zA-Z]*
Matching totally dissimilar patterns
This feature is not available in the Bourne shell. To copy all the C
and Java source programs from another directory, we can use *.
{c,java}
$ cp $HOME/prog_sources/*.{c,java} .
35. Cont..
To copy three directories (project, html and scripts) to the
current directory, we can use
$ cp /home/kumar/{project,html,scripts}/* .
The *, ? lose their meaning when used inside the class, and are
matched literally.
Examples:
[*,?] Either * or ?
Removing the special meanings of wild cards (Escaping
and quoting)
The shell uses some special characters to match filenames. If
the file names themselves contains any of these special
characters, we need a mechanism to use this file names.
For example we have a file called chap*. If I tried to remove
that file by using rm chap*, this will delete all files starting with
chap. So UNIX provides two mechanisms to use a special
character as normal one.
36. Cont..
Escaping: Provide a (backslash) before the wild card to remove
its meaning.
Quoting: Enclosing the wild cards or even the entire pattern,
within quotes.
Escaping
Escaping is providing a (backslash) before the wild-card to
remove (escape) its special meaning.
For instance, if we have a file whose filename is chap*
(Remember a file in UNIX can be names with virtually any
character except the / and null), to remove the file, it is
dangerous to give command as rm chap*, as it will remove all
files beginning with chap.
Hence to suppress the special meaning of *, use the command
rm chap*
37. Cont..
Examples:
To list the contents of the file chap0[1-3], use
$ cat chap0[1-3]
Escaping the space: A filename can contain a whitespace
character also. Hence to remove a file named My Document.doc,
which has a space embedded, use:
$ rm My Document.doc
Escaping the itself: Use another before it.
$echo
Escaping the new line character: Use before pressing Enter key.
38. Cont..
Quoting:
Quoting is enclosing the wild-card, or even the entire pattern,
within quotes. Anything within these quotes (barring a few
exceptions) are left alone by the shell and not interpreted.
When a command argument is enclosed in quotes, the meanings
of all enclosed special characters are turned off.
Single quote: All special characters between these quotes lose
their special meaning.
$ rm ‘chap*’ Removes file chap*
Double quote
Most special characters between these quotes lose their special
meaning with these exceptions −
$ for parameter substitution
`Backquotes for command substitution
$ to enable literal dollar signs
` to enable literal backquotes
" to enable embedded double quotes
39. Cont..
to enable embedded backslashes
All other characters are literal (not special)
$ rm “My Document.doc” Removes file My Document.doc
Echo “”
Back quote: Anything in between back quotes would be treated
as a command and would be executed.
DATE=`date`
echo "Current Date: $DATE“
Upon execution, you will receive the following result −
Current Date: Thu Jul 2 05:28:45 MST 2009
40. Three standard files and redirection
These files are streams of characters which many commands see
as input and output.
When a user logs in, the shell makes available three files
representing three streams. Each stream is associated with a
default device:
Standard input: The file (stream) representing input, connected
to the keyboard.
Standard output: The file (stream) representing output,
connected to the display.
Standard error: The file (stream) representing error messages
that emanate from the command or shell, connected to the
display.
41. Cont..
REDIRECTION:
It is the process by which we specify that a file is to be used in
place of one of the standard files. With input files, we call it input
redirection; with output files, we call it as output redirection; and
with error file, we call it as error redirection.
Standard input
The standard input can represent three input sources:
The keyboard, the default source.
A file using redirection with the < symbol.
Another program using a pipeline.
Eg: If you want to use wc command without arguments, wc
obtains its arguments from the keyboard. To mark the end of
input press Ctrl+d
$ wc
Hello students
This is cse department
[Ctrl+d]
42. Cont..
2 6 37
If you want a file to be the input for a command that normally
wouldn't accept a file as an option or if you want command
ignorant to the source of its input, you can redirect input using
the "<" (less-than symbol) operator.
Eg: $ wc <sample.txt
On seeing the <, the shell opens the disk file sample.txt for
reading
It unplugs the standard input file from its default source and
assigns to sample.txt
Wc reads from standard input which has earlier be reassigned
by the shell to sample.txt
Here wc is unaware what is the destination of its input file.
43. Cont..
Taking input both from file and standard input – symbol is used to
indicate the sequence of taking input.
44. Cont..
Standard Output
All commands displaying output on the terminal actually write to
the standard output file as a stream of characters, and not
directly to the terminal as such.
Any command that uses standard output is ignorant about the
destination of its output. The standard output can represent three
possible destinations:
•The terminal, the default destination.
•A file using the redirection symbols > and >>.
•As input to another program using a pipeline.
Eg:
$ wc sample.txt > newfile
$ cat newfile
3 14 71 sample.txt
45. Cont..
Standard Output
All commands displaying output on the terminal actually write to
the standard output file as a stream of characters, and not
directly to the terminal as such.
Any command that uses standard output is ignorant about the
destination of its output. The standard output can represent three
possible destinations:
•The terminal, the default destination.
•A file using the redirection symbols > and >>.
•As input to another program using a pipeline.
Eg:
$ wc sample.txt > newfile
$ cat newfile
3 14 71 sample.txt
46. Cont..
> is used to redirect the output into different location. If the
output file doesn’t exist it will createit. If it exists the shell
overwrite it. >> is used to append the file.
(ls –x *.c ; echo ; cat *.c) > c_progs_all.txt
Standard Error
A file is opened by referring to its pathname, but subsequent
read and write operations identify the file by a unique number
called a file descriptor.
The kernel maintains a table of file descriptors for every process
running in the system. The first three slots are generally allocated
to the three standard streams as,
0 – Standard input
1 – Standard output
2 – Standard error
These descriptors are implicitly prefixed to the redirection
symbols.
47. Cont..
Examples:
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>& 1
When you enter an incorrect command or try to open nonexisting
file, certain error messages show up on the screen. This is the
standard error stream whose default destination is the terminal.
Eg:
Cat foo
Cat: cannot open foo
Cat foo > errorfile
48. Cont..
Filters: Using both standard input and standard output
UNIX commands can be grouped into four categories viz.,
1.Directory-oriented commands like mkdir, rmdir and cd, and
basic file handling commands like cp, mv and rm use neither
standard input nor standard output.
2. Commands like ls, pwd, who etc. don’t read standard input
but they write to standard output.
3. Commands like lp that read standard input but don’t write to
standard output.
4. Commands like cat, wc, cmp etc. that use both standard
input and standard output.
Commands in the fourth category are called filters. Note that
filters can also read directly from files whose names are
provided as arguments.
49. Cont..
Example: To perform arithmetic calculations that are specified as
expressions in input file calc.txt and redirect the output to a file
result.txt, use
$ bc < calc.txt > result.txt
50. Connecting commands: Pipe
With piping, the output of a command can be used as input
(piped) to a subsequent command.
A pipe operator receives its input from standard output and
sends it to the next command through standard input. This means
that the left command must be able to send data to standard
output and the right command must be able to receive data from
standard input.
The token for a pipe is the vertical bar (|).
$ command1 | command2
Output from command1 is piped into input for command2.
This is equivalent to, but more efficient than:
$ command1 > temp
$ command2 < temp
$ rm temp
Main advantage of using pipe is that they doesn’t create
temporary files.
51. Cont..
Examples
$ ls -al | more
$ who | sort | lpr we can use any number of
commands with pipe.
We can do that by feeding the concatenated output stream of all
the .c files to wc –c as its input:
$ cat *.c | wc –c
52. Cont..
Creating a tee
tee is an external command that handles a character stream by
duplicating its input. It saves one copy in a file and writes the
other to standard output.
It is primarily used in conjunction with pipes and filters. It is also
a filter and hence can be placed anywhere in a pipeline.
Example: The following command sequence uses tee to display
the output of who and saves this output in a file as well.
$ who | tee users.lst
$ls -1 *.txt | wc -l | tee count.txt
53. Cont..
Command substitution
The shell enables the connecting of two commands in yet another
way.
While a pipe enables a command to obtain its standard input from
the standard output of another command, the shell enables one
or more command arguments to be obtained from the standard
output of another command. This feature is called command
substitution.
Example:
$ echo Current date and time is `date`
Observe the use of backquotes around date in the above
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.
54. Cont..
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
you use single quotes, the backquote is not interpreted by the
shell if enclosed in single quotes.
grep – searching for a pattern
It scans the file / input for a pattern and displays lines containing
the pattern, the line numbers or filenames where the pattern
occurs.
It’s a command from a special family in UNIX for handling search
requirements. The syntax is:
grep options pattern filename(s)
eg: grep “sales” emp.lst
55. Cont..
The above example will display lines containing sales from the file
emp.lst.
Patterns with and without quotes is possible. It’s generally safe to
quote the pattern. Quote is mandatory when pattern involves
more than one word
When grep is used with multiple filenames, it displays the
filenames along with the output.
grep “director” emp1.lst emp2.lst
grep is one of the most important UNIX commands, and we must
know the options that POSIX requires grep to support. Linux
supports all of these options.
-I ignores case for matching
-v doesn’t display lines matching expression
-n displays line numbers along with lines
56. Cont..
-c displays count of number of occurrences
-l displays list of filenames only
-e exp specifies expression with this option
-x matches pattern with entire line
-f file takes patterns from file, one per line
-E treats pattern as an extended RE
-F matches multiple fixed strings
Examples:
grep -i ‘agarwal’ emp.lst
grep –n ‘marketing’ emp.lst
grep –c ‘director’ emp.lst
57. egrep
Extended Regular Expression (ERE) and grep
If current version of grep doesn’t support ERE, then use egrep but
without the –E option. -E option treats pattern as an ERE.
+ matches one or more occurrences of the previous
character
? Matches zero or one occurrence of the previous character
b+ matches b, bb, bbb, etc.
b? matches either a single instance of b or nothing
These characters restrict the scope of match as compared to the *
grep –E “[aA]gg?arwal” emp.lst
The ERE set
ch+ matches one or more occurrences of character ch
ch? Matches zero or one occurrence of character ch
exp1|exp2 matches exp1 or exp2
(x1|x2)x3 matches x1x3 or x2x3
58. Cont..
Matching multiple patterns (|, ( and ))
grep –E ‘sengupta|dasgupta’ emp.lst
We can locate both without using –e option twice, or
grep –E ‘(sen|das)gupta’ emp.lst
59. Basic Regular Expressions (BRE)
Basic Regular Expressions (BRE) – An Introduction
It is tedious to specify each pattern separately with the -e option.
grep uses an expression of a different type to match a group of
similar patterns.
If an expression uses meta characters, it is termed a regular
expression. Some of the characters used by regular expression
are also meaningful to the shell.
BRE character subset
The basic regular expression character subset uses an elaborate
meta character set, overshadowing the shell’s wild-cards, and can
perform amazing matches. * Zero or more occurrences
g* nothing or g, gg, ggg, etc.
. A single character
.* nothing or any number of characters
[pqr] a single character p, q or r
[c1-c2] a single character within the ASCII range represented by
c1 and c2
60. Cont..
The character class
grep supports basic regular expressions (BRE) by default and
extended regular expressions (ERE) with the –E option.
A regular expression allows a group of characters enclosed
within a pair of [ ], in which the match is performed for a single
character in the group.
grep “[aA]g[ar][ar]wal” emp.lst
A single pattern has matched two similar strings. The pattern
[a-zA-Z0-9] matches a single alphanumeric character.
61. Cont..
The *
The asterisk refers to the immediately preceding character. *
indicates zero or more occurrences of the previous character.
g* nothing or g, gg, ggg, etc.
grep “[aA]gg*[ar][ar]wal” emp.lst
Notice that we don’t require to use –e option three times to get
the same output!!!!!
The dot
A dot matches a single character. The shell uses ? Character to
indicate that.
.* signifies any number of characters or none
grep “j.*saxena” emp.lst
62. Cont..
Specifying Pattern Locations (^ and $)
Most of the regular expression characters are used for matching
patterns, but there are two that can match a pattern at the
beginning or end of a line.
^ for matching at the beginning of a line
$ for matching at the end of a line
grep “^2” emp.lst
Selects lines where emp_id starting with 2
grep “7…$” emp.lst
Selects lines where emp_salary ranges between 7000 to 7999
grep “^[^2]” emp.lst
Selects lines where emp_id doesn’t start with 2
63. SHELL PROGRAMMING
ORDINARY AND ENVIRONMENT VARIABLES.
Environment variable
Environment variables control the behavior 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 to the desired
value.
64. Cont..
Variables are 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.
Evaluating a shell variable:
Variables are evaluated by prefixing the variable name with a $.
When the shell reads a command line, all words that are preceded
by a $ are identified and evaluated as variables unless otherwise
the $ is despecialized.
Ex: $x=50
$echo $x
$echo $a
$
65. THE .PROFILE FILE
THE .PROFILE FILE
This file is a shell script that will be present in the home directory
of every user.
As this file resides in the HOME directory, it gets executed as soon
as the user logs in.
The .profile file is specific for every individual user and is
responsible for user environment.
The system administrator provides each user with a profile that
will sufficient to have a minimum working environment, the user
can then edit and customize the same according to their
convenience.
This file is automatically executed on login; it is called the
AUTOEXE.BAT file of unix. In this file if required suitable values
are assigned to the environment variables.
66. Cont..
The user can customize the operating environment to suit user
requirements by manipulating system variables also, adding and
modifying statements in the .profile file.
The profile file acts like a universal file for all users and is
responsible for the general environment
67. Shell Scripts
Shell Scripts
When groups of command have to be executed regularly, they
should be stored in a file, and the file itself executed as a shell
script or a shell program by the user.
A shell program runs in interpretive mode. It is not complied with
a separate executable file as with a C program but each
statement is loaded into memory when it is to be executed.
Hence shell scripts run slower than the programs written in high-
level language. .sh is used as an extension for shell scripts.
However the use of extension is not mandatory. Shell scripts are
executed in a separate child shell process which may or may not
be same as the login shell.
68. Cont..
Example: script.sh
#! /bin/sh
# script.sh: Sample Shell Script
echo “Welcome to Shell Programming”
echo “Today’s date : `date`”
echo “This months calendar:”
cal `date “+%m 20%y”` #This month’s calendar.
echo “My Shell :$ SHELL”
The # character indicates the comments in the shell script and all
the characters that follow the # symbol are ignored by the shell.
However, this does not apply to the first line which beings with
#.
This because, it is an interpreter line which always begins with #!
followed by the pathname of the shell to be used for running the
script. In the above example the first line indicates that we are
using a Bourne Shell.
69. Cont..
To run the script we need to first make it executable. This is
achieved by using the chmod command as shown below:
$ chmod +x script.sh
Then invoke the script name as:
$ script.sh
Once this is done, we can see the following output :
Welcome to Shell Programming
Today’s date: Mon Oct 8 08:02:45 IST 2007
This month’s calendar:
October 2007
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
My Shell: /bin/Sh
70. Cont..
Read: Making scripts interactive
The read statement is the shell’s internal tool for making scripts
interactive (i.e. taking input from the 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 you will 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
# emp1.sh: Interactive version, uses read to accept two inputs
71. Cont..
#
echo “Enter the pattern to be searched: c” # No newline
read pname
echo “Enter the file to be used: c” # use echo –e in bash
read fname
echo “Searching for pattern $pname from the file $fname”
grep $pname $fname
echo “Selected records shown above”
Running of the above script by specifying the inputs when the
script pauses twice:
$ emp1.sh
Enter the pattern to be searched : director
Enter the file to be used: emp.lst
Searching for pattern director from the file emp.lst
9876 Jai Sharma Director Productions
2356 Rohit Director Sales
72. Cont..
Using Command Line Arguments
Shell scripts also accept arguments from the command line.
The arguments are assigned to special shell variables.
Represented by $1, $2, etc; similar to C command arguments
argv[0], argv[1], etc.
Within the command script, the passed parameters are
accessible using ‘positional parameters’. These range from $0 to
$9, where $0 refers to the name of the command itself, and $1 to
$9 are the first through to the ninth parameter, depending on
how many parameters were actually passed.
Example:
$ sh hello how to do you do
Here $0 would be assigned sh
$1 would be assigned hello
$2 would be assigned how
And so on …
73. Cont..
set
This command can be used to set the values of the positional
parameters on the command line.
Example:
$ set how do you do
$ echo $1 $2
how do
Here, “how” was assigned to $1 and “do” was assigned to $2 and
so on.
shift
This command is used to shift the position of the positional
parameters. i.e. $2 will be shifted to $1 all the way to the tenth
parameter being shifted to $9.
74. Cont..
Example:
$ set hello good morning how do you do welcome to Unix tutorial.
Here, ‘hello’ is assigned to $1, ‘good’ to $2 and so on to ‘to’ being
assigned to $9. Now the shift command can be used to shift the
parameters ‘N’ places.
Example:
$ shift 2
$ echo $1
Now $1 will be ‘morning’ and so on to $8 being ‘unix’ and $9
being ‘tutorial’.
The following table lists the different shell parameters.
Shell parameter Significance
$1, $2… Positional parameters representing
command line arguments
$ # No. of arguments specified in
command line
75. Cont..
$ 0 Name of the executed command
$ * Complete set of positional parameters
as a single string
“$ @” Each quoted string treated as
separate argument
$ ? Exit status of last command
$$ Pid of the current shell
$! PID of the last background job.
exit and Exit Status of Command
To terminate a program exit is used. Nonzero value indicates an
error condition.
Example 1:
$ cat foo
Cat: can’t open foo
Returns nonzero exit status. The shell variable $? Stores this
status.
Example 2:
grep director emp.lst > /dev/null:echo $?
0
76. Cont..
The logical Operators && and ||
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.
Example1:
$ grep ‘director’ emp.lst && echo “Pattern found”
Output:
9876 Jai Sharma Director Productions
2356 Rohit Director Sales
Pattern found
Example 2:
$ grep ‘clerk’ emp.lst || echo “Pattern not found”
Output:
Pattern not found
77. Cont..
The if Conditional
The if statement makes two way decisions based on the result of
a condition. The following forms
of if are available in the shell:
78. Cont..
If the command succeeds, the statements within if are executed
or else statements in else block are executed (if else present).
Example:
#! /bin/sh
if grep “^$1” /etc/passwd 2>/dev/null
then
echo “Pattern Found”
else
echo “Pattern Not Found”
fi
Output1:
$ emp3.sh ftp
ftp: *.325:15:FTP User:/Users1/home/ftp:/bin/true
Pattern Found
79. Cont..
While: Looping
To carry out a set of instruction repeatedly shell offers three
features namely while, until and for.
Syntax:
while condition is true
do
Commands
Done
The commands enclosed by do and done are executed repeatedly
as long as condition is true.
Example:
#! /bin/usr
ans=y
while [“$ans”=”y”]
do
echo “Enter the code and description : c” > /dev/tty
80. Cont..
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
81. Cont..
Output:
$ cat newlist
03 | analgestics
04 | antibiotics
05 | OTC drugs
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal
Table: Operators
Operators always begin with a – (Hyphen) followed by a two word
character word and enclosed on either side by whitespace.
82. Cont..
Numeric comparison in the shell is confined to integer values
only, decimal values are simply truncated.
Ex:
$x=5;y=7;z=7.2
1. $test $x –eq $y; echo $?
1 Not equal
2. $test $x –lt $y; echo $?
0 True
3. $test $z –gt $y; echo $?
1 7.2 is not greater than 7
83. Cont..
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
84. Cont..
Example:
#!/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 the string”; exit 1
fi
echo “Enter the filename to be used :c”
read flname
if [ –n “$flname” ] ; then
echo “ You have not entered the flname” ; exit 2
fi
emp.sh “$pname” “$flname”
else
emp.sh $*
fi
85. Cont..
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
86. Cont..
The case Conditional
The case statement is the second conditional offered by the
shell.
It doesn’t have a parallel either in C (Switch is similar) or perl.
The statement matches an expression for more than one
alternative, and uses a compact construct to permit multiway
branching.
case also handles string tests, but in a more efficient manner
than if.
Syntax:
case expression in
Pattern1) commands1 ;;
Pattern2) commands2 ;;
Pattern3) commands3 ;;
…
esac
87. Cont..
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 with esac (reverse of case).
The Here Document (<<)
The shell uses the << symbol to read data from the same file
containing the script. This is referred to as a here document,
signifying that the data is here rather than in aspirate file.
Any command using standard input can also take input from a
here document.
88. Cont..
This rather peculiar metacharacter (<<) allows us to provide
complex, multi-line input to a command. Perhaps its most
common usage is in a shell script used with the cat command to
display a lengthy description to the user.
$ cat << STOP
> Today, we hope that you learn a great deal about Unix.
>We are using Linux as our Unix system.
> There are many versions of Unix systems, including:
>UNIX, Solaris, BSD, AIX, HP-UX and Linux.
>References to UNIX, as it used to be spelled, are considered
> references to the UNIX from AT&T Bell Labs. Unix is a
> generic term refering to all Unix like systems.
>STOP
89. Cont..
Output:
Today, we hope that you learn a great deal about Unix. We are
using Linux as our Unix system. There are many versions of Unix
systems, including: UNIX, Solaris, BSD, AIX, HP-UX and Linux.
References to UNIX, as it used to be spelled, are considered
references to the UNIX from AT&T Bell Labs. Unix is a generic
term refering to all Unix like systems.
trap: interrupting a Program
Normally, the shell scripts terminate whenever the interrupt key
is pressed. It is not a good programming practice because a lot of
temporary files will be stored on disk.
The trap statement lets you do the things you want to do when
a script receives a signal. The trap statement is normally placed
at the beginning of the shell script and uses two lists:
trap ‘command_list’ signal_list
90. Cont..
When a script is sent any of the signals in signal_list, trap
executes the commands in command_list.
The signal list can contain the integer values or names (without
SIG prefix) of one or more signals – the ones used with the kill
command.
Example: To remove all temporary files named after the PID
number of the shell:
trap ‘rm $$* ; echo “Program Interrupted” ; exit’ HUP INT TERM
trap is a signal handler.
It first removes all files expanded from $$*, echoes a message
and finally terminates the script when signals SIGHUP (1), SIGINT
(2) or SIGTERM(15) are sent to the shell process running the
script.
91. Cont..
A script can also be made to ignore the signals by using a null
command list.
Example:
trap ‘’ 1 2 15