0% found this document useful (0 votes)
5 views32 pages

Module - 2 - Unix System Programming (BCS515C)

The document outlines the curriculum for the Unix System Programming module at R. L. Jalappa Institute of Technology, focusing on file attributes, permissions, and shell programming. It details the structure of file permissions in UNIX, including commands for changing permissions and ownership. Additionally, it describes the shell's role as an interpreter between the user and the operating system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views32 pages

Module - 2 - Unix System Programming (BCS515C)

The document outlines the curriculum for the Unix System Programming module at R. L. Jalappa Institute of Technology, focusing on file attributes, permissions, and shell programming. It details the structure of file permissions in UNIX, including commands for changing permissions and ownership. Additionally, it describes the shell's role as an interpreter between the user and the operating system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Sri Devaraj Urs Educational Trust (R.

)
R. L. JALAPPA INSTITUTE OF TECHNOLOGY
(Approved by AICTE, New Delhi, Affiliated to VTU, Belagavi & Accredited by NAAC “A” Grade)
Kodigehalli, Doddaballapur- 561 203
Department of CS&E (Artificial Intelligence & Machine Learning)

Subject Code: BCS515C


Subject Name: Unix System Programming
Module Number: 02
Name of the Module: File attributes and permissions, The shells interpretive cycle,
Connecting commands and Shell programming
Scheme: 2022
Prepared by: Dr. Manjunatha B N
Associate Professor & HoD
Institute Vision
To be a premier Institution by imparting quality Technical education, Professional Training and Research.

Institute Mission
M1: To provide an outstanding Teaching, Learning and Research environment through Innovative Practices
in Quality Education.
M2: Develop Leaders with high level of Professionalism to have career in the Industry, Zeal for Higher
Education, focus on Entrepreneurial and Societal activities.

Department Vision
To empower the students with knowledge and skills to develop the competency in the field of Artificial
Intelligence and Machine Learning.

Department Mission
M1: To craft the students with Novel and Intellectual skills to capability in the field of Artificial Intelligence
and Machine Learning.
M2: To train the students to have Professional career in the field of AI and ML and zeal for Higher Studies
and Research.
PROGRAMME SPECIFIC OUTCOMES (PSOs)
PSO1: Students will have the ability to understand analyse and demonstrate the knowledge of Human
cognition, Artificial Intelligence, Machine Learning in terms of real world problems to meet the challenges
of future.
PSO2: Students will have the knowledge of software, Hardware, Algorithms, Modelling Networking and
Application Development.
PSO3: Students will have the ability to develop computational knowledge using Innovative tools and
techniques to solve problems in the areas related to Machine learning and Artificial Intelligence.

PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)


PEO1: Graduates will have Prospective careers in the field of AI and ML.
PEO2: Graduates will have good Leadership Qualities, Self Learning abilities and zeal for higher studies
and Research.
PEO3: Graduates will follow Ethical Practices and exhibit high level of professionalism by participating
and addressing Technical, Business and Environmental challenges.
Module-2 Unix System Programming

Module 2

File attributes and permissions and knowing them.


ls - l : Listing file Attributes
 ls command is used to obtain a list of all filenames in the current directory.
 ls looks up the file’s inode to fetch its attributes.
ls –l to lists seven attributes of all files in the current directory and they are:
1. File type and Permissions
2. Links
3. Ownership
4. Group ownership
5. File size
6. Last Modification date and time
7. File name
Example:
$ ls –l
total 72
- rw - r- - r- - 1 kumar metal 19514 may 10 13:45 chap01
- rw - r- - r- - 1 kumar metal 4174 may 10 15:01 chap02
- rw - rw - rw - 1 kumar metal 84 feb 12 12:30 dept.lst
- rw - r- - r- - 1 kumar metal 9156 mar 12 1999 genie.sh
d rwx r - x r - x 2 kumar metal 512 may 9 10:31 helpdir
d rwx r - x r - x 2 kumar metal 512 may 9 09:57 progs

File type and Permissions:The first column shows the type and permissions
associated with each file. 3 types of file
Ordinary file( - )
Directory file (d)
Device file(a,b or c)
 3 types of permission
r -> read
w -> write
x -> execute

Links :The second column indicates the number of links associated with the file.
Ownership: When you create a file, you automatically become its owner.
Group Ownership: Every user is attached to a group owner
File size: File size in bytes
Last modification time and date:If you change only the permissions or ownership of
the file, the modification time remains unchanged
File name:Displays file name.

Example: $ ls –l
total 72
- rw - r- - r- - 1 kumar metal 19514 may 10 13:45 chap01
- rw - r- - r- - 1 kumar metal 4174 may 10 15:01 chap02
- rw - rw - rw - 1 kumar metal 84 feb 12 12:30 dept.lst

Dept., of CS&E(AI & ML), RLJIT Page 1 Dr. Manjunatha B N


Module-2 Unix System Programming

- rw - r- - r- - 1 kumar metal 9156 mar 12 1999 genie.sh


d rwx r - x r - x 2 kumar metal 512 may 9 10:31 helpdir
d rwx r - x r - x 2 kumar metal 512 may 9 09:57 progs

The –d option: Listing Directory Attributes


ls to list the attributes of a directory, rather than its contents, you need to use the
–d(directory) option
Example:

$ ls –ld helpdir progs

drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir


drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

Note: ls -d will not list all subdirectories in the current directory.

1. File ownership
 When you create a file, you become its owner (third column)
 Group owner of the file (fourth column)
 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

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 and /etc/group contains the UID and GID

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)
Example:
- rwx r-x r-- 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x r--
owner/user group owner others
Each group here represents a category and contains three slots, representing the read,
write and execute permissions of the file
3 types of categories
Owner(user): (rwx)
 It contains all three permissions(rwx).
 The file is readable, writable and executable by the owner of the file.

Dept., of CS&E(AI & ML), RLJIT Page 2 Dr. Manjunatha B N


Module-2 Unix System Programming

group :(r – x)
 Group has a hyphen in the middle slot, which indicates the absence of
write permission by the group owner of the file.
Other: (r - -)
 It has the write and execute 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.

chmod : Changing File Permissions


 A file or a directory is created with a default set of permissions, which can be
determined by umask.
 Let us assume that the file permission for the created file is - r w - r - - r - -.
 Using chmod command, we can change the file permissions and allow the owner
to execute his file.

The command can be used in two ways:


1. In a relative manner by specifying the changes to the current permissions
2. In an absolute manner by specifying the final permissions

1. Relative Permissions
chmod only changes the permissions specified in the command line and the other
permissions unchanged.
syntax
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)
Abbreviations used by chmod

Category operation permission


u -> user + assign r – read
g - >group - remove w – write
o - >others = absolute x – execute
a - >all (ugo)
Examples:

Initially,
-rw-r--r-- 1 kumar metal 1906 sep 23:38 priya
$ chmod u+x priya ; ls –l priya

-r w x r - - r - - 1 kumar metal 1906 sep 23:38 priya


The command assigns (+) execute (x) permission to the user (u), other permissions
remain unchanged.

Dept., of CS&E(AI & ML), RLJIT Page 3 Dr. Manjunatha B N


Module-2 Unix System Programming

Change permission to all group

$ chmod ugo+x priya or


$ chmod a+x priya or
$ chmod +x priya; ls -l priya

-r w x r - x r - x 1 kumar metal 1906 sep 23:38 priya

chmod accepts multiple file names in command line

$ chmod u+x note note1 note3

Permissions are removed with the - operator


Let initially,
-r w x r - x r - x 1 kumar metal 1906 sep 23:38 xstart
$ chmod go-r xstart ; ls -l xstart
Then, it becomes
-r w x - - x - - x 1 kumar metal 1906 sep 23:38 xstart

chmod accepts multiple expressions in a command line

$ chmod a-x , go+r xstart ; ls –l xstart


-r w – r - - r - - 1 kumar metal 1906 sep 23:38 xstart
chmod accepts more than one permission in a command line

$ chmod o+wx xstart ; ls -l xstart


-r w – r - - r w x 1 kumar metal 1906 sep 23:38 xstart

2 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.
 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)

Octal Permissions Significance


0 --- no permissions
1 --x execute only
2 -w- write only
3 -wx write and execute
4 r-- read only
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute

Dept., of CS&E(AI & ML), RLJIT Page 4 Dr. Manjunatha B N


Module-2 Unix System Programming

 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. chmod
can use this three-digit string as the expression.
Example:
Using absolute permission, we have,

chmod 666 xstart ; ls –l xstart


-r w - r w - r w - 1 kumar metal 1906 may 10 20:30 xstart

Note: The 6 indicates read and write permissin(4+2).

To restore the original permission to the file


chmod 644 xstart ; ls -l xstart
-r w - r - - r - - 1 kumar metal 1906 may 10 20:30 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.
using chmod Recursively(-R)
chmod -R a+x shell_scripts
This makes all the files and subdirectories found in the shell_scripts directory, executable
by all users.

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,


r w x r - x r - x (755)
A directory must never be writable by group and others
Example:
$ mkdir c_progs
$ ls –ld c_progs
d r w x r - x r - x 2 kumar metal 512 may 9 09:57 c_progs

 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.

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.

Dept., of CS&E(AI & ML), RLJIT Page 5 Dr. Manjunatha B N


Module-2 Unix System Programming

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
chown: changing file owner
Changing ownership requires superuser permission, so use su command
$ ls -l note
-r w x r - - - - x 1 kumar metal 347 may 10 20:30 note
chown sharma note; ls -l note
-r w x r - - - - 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 cannot 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.

chgrp:changing group owner


This command changes the file’s group owner. No superuser permission is required.
Example:
$ ls –l dept.lst
-r w- r - - r - - 1 kumar metal 139 jun 8 16:43 dept.lst
$ chgrp dba dept.lst; ls –l dept.lst
-r w - r - - r - - 1 kumar dba 139 jun 8 16:43 dept.lst

The shells interpretive cycle


 The shell sits between user and the operating system, acting as a command interpreter.
 The following activities are typically performed by the shell in its interpretive.
1. The shell issues the prompt and waits for you to enter a command.
2. 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.
3. It then passes on the command line to the kernel for execution.
4. The shell waits for the command to complete and normally can’t do any work
while the command is running.
5. After command execution is complete, the prompt reappears and the shell
returns to its waiting role to start the next cycle. You are now free to enter
another command.

Dept., of CS&E(AI & ML), RLJIT Page 6 Dr. Manjunatha B N


Module-2 Unix System Programming

Pattern Maching-Wild cards


 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 (something like the joker that can
match any card).
 The Shell’s Wild-cards

The * and ?
The metacharacter * is one of the characters of the shell’s wild-card set. It matches any number
of characters (including none).
Example: To list all files begin with chap, use
$ ls chap*
chap chap01 chap02 chap03 chap04 chap15 chap17 chapx chapy chapz
The next wild-card is the ? which matches a single character.
Example: To list all files whose filenames are five character long and start with chap, use
$ ls chap?
chapx chapy chapz
Example: To list all files whose filenames are six character long and start with chap, use

$ ls chap??
chap01 chap02 chap03 chap04 chap15 chap17
Note: There are two things that the * and ? Can’t match.
They don’t match a filename beginning with a dot, but they can match any number of embedded
dots.
They don’t match the / in a pathname.
Hence both a dot (.) and / must be matched explicitly.
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- a, b or d.

Dept., of CS&E(AI & ML), RLJIT Page 7 Dr. Manjunatha B N


Module-2 Unix System Programming

Example:
$ ls chap0[124] matches chap01, chap02 and chap04
$ ls chap[1-4] matches chap1, chap2, chap3 and chap4
$ ls chap[x-z] matches chapx, chapy and chapz

You can reverse the matching criteria by using ! As the first character in the class.
$ ls *.[!co] matches all filenames with a single-character extension but not .c
and .o (Not in C Shell)

$ ls [!a-zA-Z]* matches all filenames that don’t begin with an alphabetic


character (Not in C Shell)

To summarize

Escaping and Quoting


Escaping: Providing a \ (backslash) before the wild-card to remove its special meaning.
Example:
$ rm chap\* Doesn’t remove chap1,chap2

Quoting: Enclosing the wild-card, or even the entire pattern, within quotes. When a command
argument is enclosed in quotes, the meanings of all enclosed special characters are turned off.

Example:
$ rm ‘chap*’ Removes file chap*

Dept., of CS&E(AI & ML), RLJIT Page 8 Dr. Manjunatha B N


Module-2 Unix System Programming

Redirection: The three standard files


 In the context of redirection, the terminal is a generic name that represents the screen
or keyboard.
 We see the command output and error messages on the terminal (display) and provide
command input through the terminal (keyboard).
 The shell associates three files with the terminal-two for the display and one for the
keyboard.
 These files are actually streams of characters which many commands see as input and
output.
 There are three standard streams:
1. Standard input – The file (stream) representing input, connected to keyboard.
2. Standard output – The file (stream) representing output, connected to display.
3. Standard error – The file (stream) representing error messages that emanate from the
command or shell, connected to display.
Three Special Files
Each of these three special files are represented by a number called, a file descriptor. A file is
opened by using its pathname, but subsequent read and write operations identify the file by this
file descriptor.
The kernel maintains a table of the file descriptors. The first three slots are allocated to the
three standard streams.
0 – Standard Input
1 – Standard Output
2 – Standard Error
Standard Input
When a command is used without arguments, it reads the file representing the standard input.
It can represent three input sources viz.,
 The keyboard, the default source
 A file using redirection with the < symbol
 Another program using a pipeline
Example:
wc (without any arguments)
wc < sample.txt
ls | wc
Note: When a command takes input from multiple sources (e.g., a file and standard input) the
– symbol must be used to indicate the sequence of taking input.
cat file1 – file2 First from file1, then standard input, and then from file2.

Standard Output
All commands displaying output on the terminal actually write to the standard output file as a
stream of characters. There are three possible destinations of this stream:
 The terminal, the default destination
 A file using the redirection symbols > and >>
 As input to another program using a pipeline

Dept., of CS&E(AI & ML), RLJIT Page 9 Dr. Manjunatha B N


Module-2 Unix System Programming

Example:
wc sample.txt
wc sample.txt > outputFile
who | wc –l
cat *.c > all_C_progs.txt

Standard Error
When you enter an incorrect command, or try to open nonexistent file, certain diagnostic
messages show up on the screen. This is the standard error stream.
For example, trying cat on a nonexistent file produces the error stream.
We can redirect this stream to a file. But, standard error cannot be redirected in the same way
standard output can be (with > or >>). To capture the standard error and redirect to a file we
have to use 2> symbols.
Example: cat file1 2> errorfile
cat file2 2>> errorfile
Filters: Using both standard input and standard output
UNIX commands can be grouped into 4 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, bc etc. that use both standard input and standard output.
The commands in fourth category are called filters. When a program/command performs
operations on input and writes the result to the standard output, it is called a filter.
Example: $ bc < expressions.txt > results.txt

Connecting commands: Pipe


Standard input and standard output constitute two separate streams that can be individually
manipulated by the shell.
With piping, the output of a command can be used as input (piped) to a subsequent command.
$ command1 | command 2
Output from command1 is piped into input for command2.
Without pipes,
$ who > file1
$ wc –l <file1
With pipes,
$ who | wc –l

Splitting the output: tee


tee handles a character stream by duplicating its input. It saves one copy in a file and writes the
other to standard output.

Dept., of CS&E(AI & ML), RLJIT Page 10 Dr. Manjunatha B N


Module-2 Unix System Programming

The following command sequence uses tee to display the output of who and saves this output
in a file as well:
$ who | tee user.txt
romeo pts/2 sep 7 08:41 (pc123.heavens.com)
juliet pts/3 sep 7 08:41 (pc123.heavens.com)
Sumit pts/2 sep 7 08:41 (mercury.heavens.com)
user.txt also contains this output.

Command substitution
Any command surrounded by backquotes is executed by the shell, which then replaces the
standard output of the command into the command line.
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`
Current date and time is Mon Oct 30 09:35:16 IST 2017
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.
$ echo “There are `ls | wc –l` files in the current directory”
There are 47 files in the current directory
The backquote is not interpreted by the shell if enclosed in single quotes.
$ echo ‘There are `ls | wc –l` files in the current directory’
There are `ls | wc –l` files in the current directory
Command substitution has interesting application possibilities in shell scripts. It speeds up
work by letting you combine a number of instructions in one.
Korn Shell, Bash Shell
POSIX recommends the use of the form $(command) rather than the old `command` for
command substitution. The Korn shell and Bash shell offer both forms.
$ echo The date today is `date` OR
$ echo The date today is $(date)

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.
Syntax:- grep options pattern filename(s)
Example:
grep “sales” emp.lst
 Patterns with and without quotes is possible
 Its generally safe to quote the pattern

Dept., of CS&E(AI & ML), RLJIT Page 11 Dr. Manjunatha B N


Module-2 Unix System Programming

 Quote is mandatory when pattren involves more than one word


 It returns the prompt in case the pattren can’t be located
grep president emp.lst
 When grep is used with multiple filenames, it displays the filenames along with the
output
grep “director” emp1.lst emp2.lst
Where it shows filename followed by the contents
grep options
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 specifies expression with this option
-x matches pattern with entire line
-f file takes pattrens from file, one per line
-E treats pattren as an extended RE
-F matches multiple fixed strings

BASIC REGULAR EXPRESSIONS (BRE)


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
* 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
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. When we use range,
make sure that the character on the left of the hyphen has a lower ASCII value than the
one on the right
THE *

Dept., of CS&E(AI & ML), RLJIT Page 12 Dr. Manjunatha B N


Module-2 Unix System Programming

* 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
.* signifies any number of characters or none
Specifying Pattern Locations of anchoring characters (^ 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
EXTENDED RE (ERE)
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
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

Shell variables
 Environmental variables are used to provide information to the programs you
use. You can have both global environment and local shell variables.
 Global environment variables are set by your login shell and new programs and
shells inherit the environment of their parent shell.
 Local shell variables are used only by that shell and are not passed on to other
processes. A child process cannot pass a variable back to its parent process.
Some global environment variables are,

Dept., of CS&E(AI & ML), RLJIT Page 13 Dr. Manjunatha B N


Module-2 Unix System Programming

HOME Path to your home directory

HOST The hostname of your system

LOGNAME The name you login with

PATH Paths to be searched for commands

SHELL The login shell you’re using

Local Shell Variables

 A variable assignment is of the form variable=value, but its evaluation requires


the $ as prefix to the variable name.
count=5
echo $count
total=$count You can assign value of one variable to another variable
echo $total
Note: There should not be any space around =. i.e. if we say x =5 then the shell interprets x as
command running with the =5 as argument!

 All shell variables are of string type.


 All shell variables are initialized to null strings by default.
i.e. x= [Enter] will assign null to x.

C Shell

C Shell uses the set statement to set variables.

set count = 1 OR set count=1

Evaluation is done in normal manner. i.e. prefix variable with $.

Effects of Quoting and Escaping

To assign a multiword string to a variable, quoting is preferred than escaping.

message=You\ didn\’t\ enter\ the\ filename

message=“You didn’t enter the filename”

echo The average salary is \$1000

Dept., of CS&E(AI & ML), RLJIT Page 14 Dr. Manjunatha B N


Module-2 Unix System Programming

echo ‘The average salary is $1000’

Note: $ is evaluated by the shell when it is double-quoted.

Shell Scripts/Shell Programs

 Group of commands have to be executed regularly, they should be stored in a file and
file itself executed as a shell script or a shell program by the user.
 A shell program runs in interpretive mode.
 Shell scripts are executed in a separate child shell process which may or may not be
same as the login shell.hell Scripts/Shell Programs

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”`

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.

Dept., of CS&E(AI & ML), RLJIT Page 15 Dr. Manjunatha B N


Module-2 Unix System Programming

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:

$ sh script.sh

The .profile

 the .profile lets you to customize your operating system environment to suit your
requirements.
 This environment remains in effect throughout the login session.
 Every time you make changes to this file.. You should either logout and login again,or
use special command to execute file:
$ .profile

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

echo “Enter the pattern to be searched: \c”

read pname

echo “Enter the file to be used: \c”

Dept., of CS&E(AI & ML), RLJIT Page 16 Dr. Manjunatha B N


Module-2 Unix System Programming

read fname

echo “Searching for pattern $pname from the file $fname”

grep “$pname” $fname

echo “Selected records shown above”

Output

$ sh 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 JaiSharma Director Productions

2356 Rohit Director Sales

Selected records shown above

Using Command Line Arguments

 Shell scripts also accept arguments from the command line. Therefore they can be run
non interactively and be used with redirection and pipelines.
 The arguments are assigned to special shell variables. Represented by $1, $2, etc
 Similar to C command arguments argv[0], argv[1], etc. The following table lists the
different shell parameters.
$# Number of arguments specified in command line

$0 Name of the executed command

$* Complete set of positional parameters as a single string

Dept., of CS&E(AI & ML), RLJIT Page 17 Dr. Manjunatha B N


Module-2 Unix System Programming

exit and EXIT status of the command

C program and shell scripts have a lot in common, and one of them is that they both use the
same command to terminate a program.
It has exit in shell and exit( ) in C.
There are two very common exit values
exit 0 successful

exit 1 failure

Example:

$ cat foo
cat: can’t open foo
Returns nonzero value to the calling program
The shell offers a variable $? And a command (test) that evaluates the exit status.

The logical Operators && and ||

The shell provides two operators that allow conditional execution, the && and ||.
Usage:
cmd1 && cmd2
cmd1 || cmd2
&& delimits two commands. cmd 2 executed only when cmd1 succeeds.
Example1:

Dept., of CS&E(AI & ML), RLJIT Page 18 Dr. Manjunatha B N


Module-2 Unix System Programming

$ grep ‘director’ emp.lst && echo “Pattern found”


Output:
9876 JaiSharma Director Productions
2356 Rohit Director Sales
Pattern found

|| this indicates inverse role , the second command executed only first command fails
Example 2:
$ grep ‘clerk’ emp.lst || echo “Pattern not found”
Output:
Pattern not found

Using test and [ ] to Evaluate Expressions


 Test statement is used to handle the true or false value returned by expressions, and it
is not possible with if statement.
 Test uses certain operators to evaluate the condition on its right and returns either a
true or false exit status, which is then used by if for making decisions.
 Test works in three ways
 Compare two numbers
 Compares two strings or a single one for a null value
 Checks files attributes
 Numeric Comparison
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
 Operators always begin with a – (Hyphen) followed by a two word character word
and enclosed on either side by white space.

Dept., of CS&E(AI & ML), RLJIT Page 19 Dr. Manjunatha B N


Module-2 Unix System Programming

 Numeric comparison in the shell is confined to integer values only, decimal values are
simply truncated
Example:
$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
4. $test $z –eq $y ; echo $?
0 7.2 is equal to 7

Shorthand for test


[ and ] can be used instead of test. The following two forms are equivalent
test $x –eq $y
and
[ $x –eq $y ]

The if Conditional
The if statement makes two way decision depending on the fulfillment of a certain condition.
Form 1:

if command is successful
then
execute commands
else
execute commands
fi

Dept., of CS&E(AI & ML), RLJIT Page 20 Dr. Manjunatha B N


Module-2 Unix System Programming

Form 2:
if command is successful
then
execute commands
fi
Form 3:
if command is successful
then
execute commands
elif commands is successful
then . . .
else . . .
fi
Example:
#! /bin/sh
if grep “^$1” /etc/passwd 2>/dev/null
then
echo “Pattern Found”
else
echo “Pattern Not Found”
fi

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.

Dept., of CS&E(AI & ML), RLJIT Page 21 Dr. Manjunatha B N


Module-2 Unix System Programming

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
fi
echo “Enter the filename to be used :\c”
read flname
if [ ! –n “$flname” ] ; then
echo “ You have not entered the flname” ; exit
fi
grep “$pname” $flname
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

Dept., of CS&E(AI & ML), RLJIT Page 22 Dr. Manjunatha B N


Module-2 Unix System Programming

File Tests
 test can be used to test various file attributes like its type (file, directory or symbolic
links)
 its permission (read, write. Execute, SUID, etc).
 the following table depicts file-related tests with test

Example:
$ ls –l emp.lst
-rw-rw-rw- 1 kumar group 870 jun 8 15:52 emp.lst

$ [-f emp.lst] ; echo $?


0 Ordinary file

$ [-x emp.lst] ; echo $?


1 Not an executable.
$ [! -w emp.lst] || echo “False that file not writeable”
False that file is not writable
for: Looping with a List
Shells for loop differs in structure from the ones used in other programming language.
The loop body also uses the do and done, but additional parameters here are variable and list.
Syntax:
for variable in list
do

Dept., of CS&E(AI & ML), RLJIT Page 23 Dr. Manjunatha B N


Module-2 Unix System Programming

Commands
done
Example:
for file in ch1 ch2
do
cp $file ${file}.bak
echo $file copied to $file.bak
done
Output:
ch1 copied to ch1.bak
ch2 copied to ch2.bak
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 list is large.
Example:
$ for var in `cat clist`
List from wildcards: Here the shell interprets the wildcards as filenames.
Example:
for file in *.htm *.html ; do
sed ‘s/strong/STRONG/g
s/img src/IMG SRC/g’ $file > $$
mv $$ $file
done
List from positional parameters:

Dept., of CS&E(AI & ML), RLJIT Page 24 Dr. Manjunatha B N


Module-2 Unix System Programming

Example: emp.sh
#! /bin/sh
for pattern in “$@”; do
grep “$pattern” emp.lst || echo “Pattern $pattern not found”
done

While: looping
It repeatadly performs a set of instructions until the control command returns a true exit status
Syntax:
while condtion is true
do
commands
done
Example:
#! /bin/bash
#shows use of while
#program to accept code and description in one line
answer=y
while [ "$answer" = "y" ]
do
echo "enter the code and description" > /dev/tty
read code description
echo "$code|$description" >> newlist #append this line to newlist
echo "enter anymore (y/n)?" >/dev/tty
read anymore
case $anymore in
y*|Y*) answer=y;; #also accept yes,YES
n*|N*)answer=n;; #also accept no|NO
*)answer=y;; #any other reply mean y
esac
done

The case Conditional

Dept., of CS&E(AI & ML), RLJIT Page 25 Dr. Manjunatha B N


Module-2 Unix System Programming

 Conditional statement offered by the shell


 The statement matches an expression for more than one alternative, and uses a
compact construct to permit multiway branching.
 Also handles string tests, but in a more efficient manner than if.

Syntax:
case expression in
Pattern1) commands1 ;;
Pattern2) commands2 ;;
Pattern3) 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 with esac (reverse os case).
Example: Write a shell program to create a menu which displays i) List of files
ii) Process of user iii) Today’s Date iv) users of system v) Quit to UNIX
#! /bin/sh
#
echo “Menu\n1. List of files\n2. Processes of user\n3. Today’s Date
4. Users of system\n5.Quit\nEnter 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

Dept., of CS&E(AI & ML), RLJIT Page 26 Dr. Manjunatha B N


Module-2 Unix System Programming

Menu
1. List of files
2. Processes of user
3. Today’s Date
4. Users of system
5. Quit
Enter your option: 3
Mon Nov 6 09:02:45 IST 2017
set and shift: Manipulating the Positional Parameters
The set statement assigns positional parameters $1, $2 and so on, to its arguments. This is
used for picking up individual fields from the output of a program.
Example 1: $ set 9876 2345 6213
This assigns the value 9876 to the positional parameters $1, 2345 to $2 and 6213 to $3
Shift: Shifting Arguments Left
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, $3 becomes
S2 and so on
Example 1:
$ echo “$@” $@ and $* are interchangeable
Mon Oct 8 08:02:45 IST 2007
$ echo $1 $2 $3
Mon Oct 8
$ shift
$ echo $1 $2 $3
Oct 8 08:02:45

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 separate file.
Using the Here Document with Interactive Programs:
Eg:
$ empl.sh « END
> director

Dept., of CS&E(AI & ML), RLJIT Page 27 Dr. Manjunatha B N


Module-2 Unix System Programming

> emp.lst
> END

Output:
Enter the pattern to be searched: Enter the file to be used: Searching for director from file
emp.lst
9876 Jai sharma Director Productions
2356 Rohit Director Sales
Selected records shown above.

trap: interrupting a Program


 shell scripts terminate - interrupt key is pressed.
 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.
 trap placed at the beginning of the shell script uses two lists:
trap ‘command_list’ signal_list
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.
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.
A script can also be made to ignore the signals by using a null command list.
Example: trap ‘’ 1 2 15

Dept., of CS&E(AI & ML), RLJIT Page 28 Dr. Manjunatha B N


Module-2 Unix System Programming

Assignment Questions
1. Explain the seven field output of ls –l command. Or Explain briefly the file
attributes listed using ls –l command. Or Which command is used for listing file
attributes? Explain significance of each field of the output.
2. What are different ways of setting file permissions? Or Explain how to use chmod
command to set the permissions in a relative and absolute manner with an example.
3. A file’s current permissions are – r w - r- - r- - specify the chmod expression required
to change them for the following:
i) rwxrwxrwx
ii) r - - r - x r - -
iii) - - - - - - - - -
iv) - - - r - - r - -
Using both the relative and absolute methods of assigning permissions.
4. What are directory permissions?
5. Explain Shell’s interpretive life cycle.
6. What are wild cards? Explain the shells wild cards, with examples.
7. Discuss the three standard files supported by UNIX. Or explain the standard input,
standard output and standard error with respect to UNIX operating system.
8. Explain the following
i) Filters ii) pipe iii) tee iv) Command substitution
9. Explain grep command with its options.
10. Explain with example Basic Regular Expressions (BRE).
11. Explain the anchoring characters.
12. What are environment variables? Explain any four. OR
Explain the following environment variables with examples:
i) SHELL ii) LOGNAME iii) PATH iv) HOME
13. What is the exit status of a command and where is it stores?
14. Explain the different ways of using test statements, with examples or Explain the use
of test and [ ] to evaluate an expression in shell.
15. What is shell script? Explain the following statements with syntax and examples.
i) if ii) case iii) while
16. Explain the shell features of “while” and “for” with syntax.
17. Explain the shell’s for loop giving the possible sources of the list.

Dept., of CS&E(AI & ML), RLJIT Page 29 Dr. Manjunatha B N


Module-2 Unix System Programming

18. Briefly explain set and shift commands in UNIX to manipulate positional
parameters with example.
19. Discuss use of trap statement for interrupting a program in UNIX.
20. What is shell programming? Write a shell program to create a menu which displays,
i) List of files ii) Current date iii) Process status
iv) Current user of the system and v) Quit to UNIX

Dept., of CS&E(AI & ML), RLJIT Page 30 Dr. Manjunatha B N

You might also like