0% found this document useful (0 votes)
130 views30 pages

DTG

The document contains lecture notes on shell programming and file attributes from the UNIX and Shell Programming module of the Computer Science department at Global Academy of Technology. The syllabus covers topics like shell programming, variables, file attributes, filters and special files. The notes provide an overview of different shells like Bourne shell, C shell, Korn shell and Bash shell. It describes how to write and run shell scripts, and defines local, environment and read-only variables. Common environment variables like PATH, HOME, MAIL, PS1 are explained along with their usage. File attributes like inodes, hard links, soft links are also summarized.

Uploaded by

Ravindra Rao
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)
130 views30 pages

DTG

The document contains lecture notes on shell programming and file attributes from the UNIX and Shell Programming module of the Computer Science department at Global Academy of Technology. The syllabus covers topics like shell programming, variables, file attributes, filters and special files. The notes provide an overview of different shells like Bourne shell, C shell, Korn shell and Bash shell. It describes how to write and run shell scripts, and defines local, environment and read-only variables. Common environment variables like PATH, HOME, MAIL, PS1 are explained along with their usage. File attributes like inodes, hard links, soft links are also summarized.

Uploaded by

Ravindra Rao
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/ 30

“GLOBAL ACADEMY OF TECHNOLOGY”

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

17CS35 - UNIX and Shell Programming


MODULE 4: Lecture Notes
The Shell Programming and More File attributes

SYLLABUS:

Shell programming. Ordinary and environment variables. The .profile. Read and read only commands.
Command line arguments. exit and exit status of a command. Logical operators for conditional
execution. The test command and its shortcut. The if, while, for and case control statements. The set and
shift commands and handling positional parameters. The here ( << ) document and trap command.
Simple shell program examples. File inodes and the inode structure. File links – hard and soft links.
Filters. Head and tail commands. Cut and paste commands. The sort command and its usage with
different options. The umask and default file permissions. Two special files /dev/null and /dev/tty.
[Topics from chapter 11, 12, 14 of text book 1,chapter 17 from text book2]

Text Books: 1.Sumitabha Das., Unix Concepts and Applications., 4th Edition., Tata McGraw Hill
2.Behrouz A. Forouzan, Richard F. Gilberg : UNIX and Shell Programming- Cengage Learning
– India Edition. 2009.
Note: Refer chapters 8 and 4 from M. G Venkateshmurthy

Prepared by,
TANUSHREE K N, Assistant professor, Department of CSE

Tanushree K N III_sem_CSE/GAT/2018 Page 1


1. The Shell

 The UNIX shell is both an interpreter as well as a scripting language. An interactive shell turns non
interactive when it executes a script.
 Bourne Shell – This shell was developed by Steve Bourne. It is the original UNIX shell. It has strong
programming features, but it is a weak interpreter.
 C Shell – This shell was developed by Bill Joy. It has improved interpretive features, but it wasn‟t
suitable for programming.
 Korn Shell – This shell was developed by David Korn. It combines best features of the bourne and C
shells. Ithas features like aliases, command history. But it lacks some features of the C shell.
 Bash Shell – This was developed by GNU. It can be considered as a superset that combined the features
of Korn and C Shells. More importantly, it conforms to POSIX shell specification

2. UNIX Shell programming or shell script. What are the ways of


running a shell script
 A group of commands to be executed regularly should be stored in a file and the file is executed as a
shell script or shell program .OR A shell script is a text file that contains executable commands.
 user normally use .sh extension for shell scripting . Shell scripts are executed in a separate child
shell process
 Every script has three parts they are,
1. Interpreter Designator line
-It tells UNIX the path to the appropriate shell interpreter and denoted by pound sign #!
2. Comments
-Comments are identified with the pound sign #,using # we can only comment single line
3. Commands
-we can use any of the commands available in UNIX and they will not execute until we
execute the scripts.
-Shells use two tokens to separate commands: semicolon and newline

Tanushree K N III_sem_CSE/GAT/2018 Page 2


 Example:
#!/bin/sh
#script.sh
echo "today's date : `date`"
echo "my shell : $SHELL"

In the above example : #!/bin/sh is known as interpreter line. It denotes the pathname of the
shell to be used for running the script. Here it specifies Bourne shell and # denotes commented line
 Execution steps:
To run the script make it executable first and then invoke the script name(2 diff way)
1. $chmod +x script.sh
$./ script.sh
2. $ sh script.sh

NOTE:

3. Shell variables.
 A shell can be cons idered as a programming language as with any other language,
variables are defined and used with a shell.
 There are three t ypes of variables ,they are
1. Local variables or user defined variable
2. System variables or environment variable
3. read-onl y variables

Tanushree K N III_sem_CSE/GAT/2018 Page 3


4. Local variables.
 local variables in the shell are variables that are local to a particular user’s shell
These variables are exists onl y for a short time during the execution of a shell script
 To declare a local shell variable use the form variable=value (no spaces around =)
and its evaluation requires the $ as a prefix to the variable.
 shell variables names are constructed using onl y alphanumeric characters and the
underscore characters (_)
 Examples

1) #!/bin/sh output: sh script1.sh


#script1.sh 5
count=5
echo $count

2) $x=37 output: 37
$echo $x

3) $echo $xyz output: null string since xyz not initialized

Note :A variable can be removed with unset and protected from reassignment by readonly. Both are
shell internal commands.

5. Environment variables.
 System variables are set either during the boot sequence or immediatel y after logging
in.
 The working environment under which a user works depends entirel y upon the values
of these variables, these variables are known as environment variables
 These variables are similar to global variables .if required, values of these variable
scan be changed to have an environmen t to suit the user
 By convention these variables are written using UPPERCASE letter onl y.
 A list of all variables currentl y defined for user session can be viewed entering the
env command.(refer 195 page no from sumitabha das for env content)

Tanushree K N III_sem_CSE/GAT/2018 Page 4


 The set statement display all variables available in the current shell, but env command
displays onl y environment variables. Note than env is an external command and runs
in a child process.
 The environment variables are managed by the shell. As opposed to regular shel l
variables, environment variables are inherited by any program that user starts,
including another shell. New processes are assigned a copy of these variables, which
they can read, modify and pass on in turn to their own child processes.

The Common Environment Variables


 The following table shows some of the common environment variables

1. The command search path (PATH)


o The PATH variable instructs the shell about the route it should follow to locate any executable command.
o This variable holds a list of directories in a certain order in this list colons(:) separate different
directory.
o The current value of this variable can be seen using echo command as shown below.
$echo $PATH
O/P: /usr/local/sbin:/usr/sbin;/usr/bin:. // .(dot) represent current dir

o More directory can be added to the PATH using an assignment statement, new directory will also be

Tanushree K N III_sem_CSE/GAT/2018 Page 5


searched after all the directory.
$PATH=$PATH:/new/dir

1. Your home directory (HOME)


o When user logs in, UNIX normally places user in a directory named after user login name. This is called
the home directory or login directory.
o The home directory for a user is set by the system administrator while creating users (using useradd
command).
$echo $HOME
O/P: /usr/kumar

2. mailbox location and checking (MAIL and MAILCHECK)


o The incoming mails for a user are generally stored at /var/mail or /var/spool/mail and this location is
available in the environment variable MAIl.
o MAILCHECK determines how often the shell checks the file for arrival of new mail

4. The prompt strings (PS1, PS2)


o The prompt that user normally sees (the $ prompt) is the shell‟s primary prompt specified by PS1.
o PS2 specifies the secondary prompt (>).
o To change the prompt by assigning a new value to these environment variables.
$PS1=+ //change the primary promt tom +
+ //$ has changed to + , here you can execute any UNIX commands.
for ex: +cal

5 Shell used by the commands with shell escapes (SHELL)


o This environment variable specifies the login shell as well as the shell that interprets the command if
preceded with a shell escape.
$echo $SHELL
O/P: /bin/bash

6 Your username (LOGNAME)


o This variable shows your username , sometimes you may forget your username or login name ,using
this variable you can make sure which account you logged in to..
$echo $LOGNAME
Henry

Tanushree K N III_sem_CSE/GAT/2018 Page 6


7 Field Separator for command and arguments (IFS)
o IFS contains a string of characters that are used a word separator in the command line
o This string normally consists of the space , tab and newline characters. You can confirm the contents
of this variable by taking its octal dump
$echo “$IFS” | od –bc
0000000 040 011 012 012
\t \n \n

8 The terminal type (TERM)

o TERM indicates the terminal type that is used.


o Every terminal has certain characteristics that are defined in a separate control file in the termi info
directory (in/usr/lib or /usr/share/lib)
$echo $TERM
Vt100

9 Variables used in Bash and Korn

o The Bash and korn prompt can do much more than displaying such simple information as user name,
the name of machine and some indication about the present working directory. Some examples are
demonstrated next.
$ PS1=„[PWD] „
[/home/srm] cd progs
[/home/srm/progs] _

o Bash and Korn also support a history facility that treats a previous command as an event and
associates it with a number. This event number is represented as !.
$ PS1=”[!] “ $ PS1=”[! $PWD] “
[42] _ [42 /home/srm/progs] _

$ PS1=“\h “ // Host name of the machine


saturn>

Tanushree K N III_sem_CSE/GAT/2018 Page 7


6. The .profile OR a script run during starting
 Every user has a script of his or her own. This file is a shell script that will be present in the home
directory of the user.
 As this resides in the HOME directory , it gets executed as soon as the user logs on.
 The system admin provides each user with a profile that will be just sufficient to have a minimum
environment
 The user can customize the same according to his or her convenience. because this file is automatically
executed on login ,it is called the AUTOEXEC.BAT file of Unix.
 The contents of a typical .profile of a user is given below.
$cat .profile
Note: When logging into an interactive login shell, login will do the authentication, set the environment
and start shell.
 In the case of bash, the next step is reading the general profile from /etc, if that file exists. bashthen
looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and reads and executes commands
from the first one that exists and is readable. If none exists, /etc/bashrc is applied.
 When a login shell exits, bash reads and executes commands from the file, ~/.bash_logout, if it exists.
 The profile contains commands that are meant to be executed only once in a session. It can also be used
to customize the operating environment to suit user requirements. Every time user change the profile
file, user should either log out and log in again or can execute it by using a special command (called
dot).
$ . .profile

7. Read and read only commands


Read command
 The read command is used to give input to a shell program interactivel y.
 This command reads just one line and assigns this line to one or more shell variables.
Example1: script called readme.sh
$cat readname.sh output: $sh readname.sh
echo what is your name ? What is your name
read name harsha
echo the name is $name The name is harsha
when the shell comes across a read command ,it waits or pauses for t he values to be
input and carriage returns.
Tanushree K N III_sem_CSE/GAT/2018 Page 8
Example2: script called telno.sh(using \c :accept input in the same line)
$cat telno.sh output: sh telno.sh
echo “enter the name:\c” enter the name: murthy
read name enter the tel no: 081 -47326487
echo “enter the tel no: \c” $
read number
echo $name $number >> phone.lst

Read command with multiple arguments


 The read command can take multiple argumnets.in other words, values for more than
one variables can be input, using a single read co mmand.
 In an example, in which values to three variables a, b and c may be read in is given
here.
 There could be a possibilit y that number of values input are either less or more than
the number of read’s argumnets.in such situations values are assigned as follows.
1. If number of values input are less than the number of arguments, then the
arguments or variables to which values are not input will be initiated to null

2. If number of values are more than the number of arguments, the first n -1 values
are assigned to the first n -1arguments and all the remaining input values are

assigned to the nth arguments .

Read only function read-only variables


 Variables, the values of which can onl y be read but not be manipulated are called read
onl y variables.
 Example: script called readonl y.sh
$cat readonly output: sh readonly.sh
echo input a values for x input a value for x
read x the value of x is 4
echo the values of x is $x line 5:x readonl y variable so it does not
readonly x display its value
x=`expr $x + 1`
echo the values of x now is $x
Tanushree K N III_sem_CSE/GAT/2018 Page 9
8. Using Command line arguments or positional parameters
 Like unix command shell script also accepts arguments from command line. When arguments are
specified with a shell script they are assigned to certain variables- known as positional parameters.
 Arguments are passed from the command line into a shell program using the
positional parameters $1 through to $9.
 Each parameter corresponds to the position of the argument on the command line.
 The first argument is read by the shell into the parameter $1, the second argument into $2, and so on.
 After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.Some shells
doesn't support this\ method. In that case, to refer to parameters with numbers greater than 9, use
the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes
$2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

Example:
#!/bin/bash
# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
exit 0

output: root@localhost ~]# sh parameters.sh 47 9 34


first parameter is 47
Second parameter is 9
Third parameter is 34

Tanushree K N III_sem_CSE/GAT/2018 Page 10


Parameter Meaning
$* It stores the complete set of positional parameters as a single string
$# It is number of arguments specified
$0 holds the command name itself
$$ pid of the current shell
$1,$2.. positional parameters
$? exit status of last command
“$@” Each quoted string treated as separate argument.(recommended over
$*)
$! PID of the last background job

9. exit and exit status of a command ($? Variables)


 whenever a command ,that is , a program, is run it may either get executed successfully and yield a
result or it may not get executed successfully
 Whenever a command is successfully executed the program returns a 0 (zero), if a command is not
executed successfully a value other than zero will be returned. (ex 1 or 2) .
 Please note that these parameters or variables may only be referenced assignment to them is not allowed.
You can use $? to find out the exit status of command.
 $? always expands to the status of the most recently executed foreground command or pipeline.
 Example1, you run the command cal:
$ cal
Now to see exit status of cal command type following command:
$ echo $?
Output:0
//Zero means command executed successfully, if exit status returns non-zero value then your
command failed to execute.
 Example 2:
$cat sample.sh
Cat: sample.sh : no such file or dir
$echo $?

Tanushree K N III_sem_CSE/GAT/2018 Page 11


10. Logical operators for conditional execution. && and ||

 The shell provides two operators that aloe conditional execution, the && and || Usage:
 Syntax:
cmd1 && cmd2
cmd1 || cmd2
 && delimits two commands. cmd 2 executed only when cmd1 succeeds.
 The || operator plays an inverse role:the second command is executed only only when the first fails.
 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
 Example 3:
grep “$1” $2 || exit 2
echo “Pattern Found Job Over”

11. The test command and its shortcut .


 With if statement to evaluate expression test is required. Test uses certain operators to evaluate the
condition on its right and returns either true or false exit status.
 test works in three ways :

Compares two numbers 

Compares two strings or a single one for null value 

Checks file attributes. 

Tanushree K N III_sem_CSE/GAT/2018 Page 12


1. Numeric comparison
It is always begin with a – (hyphen), followed by a two character word and enclosed on either side by
whitespace
Example: $x=5; y=7; z=8;
test $x -eq $y

test $x –eq $y is equivalent as [ $x –eq $y ]

Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than

2. String comparison

Test True if
S1=S2 String s1 equal to 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

3. File attribute comparison:

-f file file exist and is a regular file

-r file file exist and is readable

-w file file exist and is writable

-x file file exist and is executable

-d file file exist and is a directory.

Tanushree K N III_sem_CSE/GAT/2018 Page 13


Example:
#!/bin/sh
#file.sh OUTPUT:$sh file.sh filename
if [ ! -e $1]; then
echo “file does not exist”
elif [ ! -r $1]; then
echo “file is not readable”
elif [ ! -w $1]; then
echo “file is not writable”
else
echo “file is both readable and writable”

12. The if control statements


 If statement makes two way decisions depending on fulfillment of certain condition.
Syntax:

Example:
#!/bin/sh o/p:?
if [ ! -e $1] ; then
echo “file does not exist”
elif [ ! -r $1] ; then
echo “file is not readable” else
echo “file is not readable and does not exist”
fi

Tanushree K N III_sem_CSE/GAT/2018 Page 14


13. While and for control statements
Loops are used to perform a set of instructions repeatedly.

Syntax (while loop): while condition is true


do
commands
done
Example: Displaying first 10 numbers
#!/bin/sh o/p:?
i=1
While[ $i –le 10]
do
echo $i
i= `expr $i +1 `
done

Syntax (for loop) : for variable in list


Do commands done

Example: for var in $PATH $HOME $MAIL do o/p:?


echo “$var”
done

note: Refer PPT for more examples

14. case control statements


It is the second conditional offered by shell. It matches an expression for more than one alternative ( multiway in
branching). It is used mainly for menu based design.
Syntax:
case expression
pattern1) commands1;;
pattern2) commands1;;
pattern3) commands1;;
pattern4) commands1;;
……………
Esac

Tanushree K N III_sem_CSE/GAT/2018 Page 15


Example: o/p:?

#!/bin/sh
echo "MENU
1. list of files 2. processes3. todays date 4. users of system 5. Quit enter your option"
read choice
case "$choice" in
1) ls –l ;;
2) ps –f ;;
3) date ;;
4) who ;;
5) exit ;;
*) echo "invalid option"
esac

15. The set and shift commands


 Set assigns its arguments to the positional parameters $1 $2 and so on.
Example:
$ set 9876 2345 6213
$echo "\$1 is $1, \$2 is $2, \$3 is $3"
Output: $1 is 9876, $2 is 2345, $3 is 6213
$echo "The $# arguments are $*"
output:The 3 arguments are 9876 2345 6213

 shift transfers the contents of a positional parameter to its immediate lower numbered one. This is done
as many times the statement is called.
Example:
$ set 9876 2345 6213
$ echo "$@"
output:9876 2345 6213
$ echo $1 $2 $3
output: 9876 2345 6213
$ shift
$ echo $1 $2 $3
output: 345 6213 (shifted by one) //$1 value is 2345 and $2 value is 6213

Tanushree K N III_sem_CSE/GAT/2018 Page 16


16. The here ( << ) document and trap command. Simple shell
program examples.

The here (<< )


 When the data that program reads is fixed and limited, the shell uses the << symbols to read data from
the same file containing the script. This is referred to as a here document. It is signifying the data is here
rather than in a separate file.

Example: vi emp.sh
#!/bin/sh
echo "enter the pattern to be searched : "
read pname
echo "enter the file to be used: "
read flname
echo "searching for $pname from file $flname"
grep "$pname" $flname

Execution steps: ./emp.sh << END


 director
 emp.lst
END

 The here document symbol (<<) is followed by two data lines and a delimiter (END). Shell treats every
line following the command and delimited by END as input to the command. The content of here
document is interpreted and processed by shell.

trap command
 Trap helps user to interrupt a program. The statement is usually placed at the beginning of a shell script
and uses two lists
Syntax: trap ‘command list’ signal list
 When script is sent any of the signals in signal_list, trap executes the commands in command_list.

Tanushree K N III_sem_CSE/GAT/2018 Page 17


The signal_list can contain the integer values or names of one or more signals.
Example:
$trap ‘rm *.c; echo “program interrupted”; exit’ HUP INT TERM.
 trap is a signal handler. It first removes all files expanded from *.c.
 Then echoes the message and finally terminates the script when the signals SIGHUP(1) SIGiINT(2) or
SIGTERM(15) are sent to the shell process running the script

17. File inodes and the inode structure or File Systems and inodes
 A file is an object on a computer that stores data, information, settings, or commands that are used
with a computer program.
 The hard disk is split into distinct partitions, with a separate file system in each partition. Every file
system has a directory structure headed by root.
n partitions = n file systems = n separate root directorie
 All attributes of a file except its name and contents are available in a table and this table is called
inode (index node), accessed by the inode number.(unique identification number)
 The inode structure contains the following attributes of a file:
 File type
 File permissions
 Number of links
 The UID of the owner
 The GID of the group owner
 File size in bytes
 Date and time of last modification
 Date and time of last access
 Date and time of last change of the inode
 An array of pointers that keep track of all disk blocks used by the file

 Please note that, neither the name of the file nor the inode number is stored in the inode.
To know inode number of a file:
$ls -i tulec05
o/p: 9059 tulec05
$ls -il tulec05 //- l for attributes and – I for inode number
o/p: 9059 -rw-r--r-- 1 kumar metal 51813 Jan 31 11:15 tulec05

Tanushree K N III_sem_CSE/GAT/2018 Page 18


Where, 9059 is the inode number and no other file can have the same inode number in the
same file system

18. File links – hard and soft links OR ln command


 The name of the file is not stored in the inode, because a file can have multiple filenames, when that
happens we say the file has more than one link.
 There are two different ways in which we can create links
1. HARD LINKS
2. SOFT LINKS

HARD LINKS
 Hard link acts like a mirror copy of the original file. These links share the same inodes. Changes made
to the original or hard linked file will reflect in the other. When you delete Hard Link nothing will
happen to the other file. Hard links can't cross file systems.
 The link count is displayed in the second column of the listing. This count is normally 1, but the
following files have two links,
-rwxr-xr-- 2 kumar metal 163 Jull 13 21:36 backup.sh
-rwxr-xr-- 2 kumar metal 163 Jul 13 21:36 restore.sh
 All attributes seem to be identical, but the files could still be copies. It’s the link count that seems to
suggest that the files are linked to each other. But this can only be confirmed by using the –i option to ls.
$ls -li backup.sh restore.sh
478274 -rwxr-xr-- 2 kumar metal163 jul 13 21:36 backup.sh
478274 -rwxr-xr-- 2 kumar metal163 jul 13 21:36 restore.sh

ln: Creating Hard Links

 A file is linked with the ln command which takes two filenames as arguments (cp command). The
command can create both a hard link and a soft link and has syntax similar to the one used by cp.
 The following command links emp.lst with employee:

Tanushree K N III_sem_CSE/GAT/2018 Page 19


$ln emp.lst employee
 The –i option to ls shows that they have the same inode number, meaning that they are actually one end
the same file:
$ls -li emp.lst employee
29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 emp.lst
29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 employee
The link count, which is normally one for unlinked files, is shown to be two. You can increase
the number of links by adding the third file name emp.dat as:
$ln employee emp.dat ; ls -l emp*
29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 emp.dat
29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 emp.lst
29518 -rwxr-xr-x 3 kumar metal 915 may 4 09:58 employee

Note: You can link multiple files, but then the destination filename must be a directory.
 A file is considered to be completely removed from the file system when its link count drops to
zero. ln returns an error when the destination file exists. Use the –f option to force the removal of the
existing link before creation of the new one
NOTE: Points to remember

1. Hard Links have same inodes number

2. ls -l command shows all the links with the link column showing the number of links.
3. Links have actual file contents
4. Removing any link, just reduces the link count but doesn't affect the other links.
5. You cannot create a Hard Link for a directory.
6. Even if the original file is removed, the link will still show you the contents of the file.

Tanushree K N III_sem_CSE/GAT/2018 Page 20


SOFT LINKS
 Soft Link is actual link to the original file. These Links will have a different Inodes value. Soft link
points to the original file so if original file is deleted then the soft link fails. If you delete the Soft Link,
nothing will happen to file. The reason for this is, the actual file or directory’s inode is different from the
"soft link" created file's inodes. Soft links can cross file systems.
 Unlike the hard linked, a symbolic link doesn’t have the file’s contents, but simply provides the
pathname of the file that actually has the contents.
 Example:
$ln -s note note.sym //-s for creating soft link
ls -li note note.sym
output: 9948 -rw-r--r-- 1 kumar group 80 feb 16 14:52 note
9952 lrwxrwxrwx 1 kumar group 4 feb16 15:07note.sym ->note
 Where, l indicate symbolic link file category. -> indicates note.sym contains the pathname for the
filename note. Size of symbolic link is only 4 bytes; it is the length of the pathname of note.
 It’s important that this time we indeed have two files, and they are not identical. Removing note.sym
won’t affect us much because we can easily recreate the link. But if we remove note, we would lose the
file containing the data. In that case, note.sym would point to a nonexistent file and become a dangling
symbolic link.

NOTE: points to remember


1. Soft Links have different inodes numbers

2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Soft Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but when the original file is removed, the link becomes
a 'dangling' link that points to nonexistent file.
5. A Soft Link can link to a directory

Tanushree K N III_sem_CSE/GAT/2018 Page 21


19. Filters (head, tail, cut, paste and sort)

 Filters are the commands which accept data from standard input manipulate it and write the results to
standard output.
 Filters are the central tools of the UNIX tool kit, and each filter performs a simple function. Some
commands use delimiter, pipe (|) or colon (:).
 Several UNIX commands are provided for text editing and shell programming. (emp.lst) - each line of
this file has six fields separated by five delimiters (separator).
 The details of an employee are stored in one single line. This text file designed in fixed format and
containing a personnel database. There are 15 lines, where each field is separated by the delimiter |.

NOTE; The Simple Database


$ cat emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
9876 | jai sharma | director | production | 12/03/50 | 7000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
2365 | barun sengupta | director | personnel | 11/05/47 | 7800
5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
4290 | jayant choudhury | executive | production | 07/09/50 | 6000
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000

20. Head and tail commands.

 head – displaying the beginning of the file


The command displays the top of the file. It displays the first 10 lines of the file, when used without an
option. -n to specify a line count
Example:
$head emp.lst // It displays the first 10 lines of the file
$head -n 3 emp.lst //will display the first three lines of the file.

Tanushree K N III_sem_CSE/GAT/2018 Page 22


 tail – displaying the end of a file
This command displays the end of the file. It displays the last 10 lines of the file, when used without an
option. -n to specify a line count
Example:
$tail emp.lst //It displays the last 10 lines of the file,
$tail -n 3 emp.lst // displays the last three lines of the file.

We can also address lines from the beginning of the file instead of the end. The +count option allows to
do that, where count represents the line number from where the selection should begin.
$tail +11 emp.lst // Will display 11th line onwards
 Different options for tail are:
1. Monitoring the file growth (-f)
2. Extracting bytes rather than lines (-c)
Use tail –f when we are running a program that continuously writes to a file, and we want
to see how the file is growing. We have to terminate this command with the interrupt key.

Note : for more examples refer 231 page no from text

21. Cut and paste commands.


cut – slitting a file vertically
 It is used for slitting the file vertically.
Example:
$ head -n 5 emp.lst | tee shortlist
will select the first five lines of emp.lst and saves it to shortlist.
 We can cut by using -c option with a list of column numbers, delimited by a comma (cutting
columns).
Example:
$cut -c 6-22,24-32 shortlist
$cut -c -3,6-22,28-34,55- shortlist
The expression 55- indicates column number 55 to end of line. Similarly, -3 is the same as 1-3.
 Most files don’t contain fixed length lines, so we have to cut fields rather than columns
(cutting fields).
-d for the field delimiter
Tanushree K N III_sem_CSE/GAT/2018 Page 23
-f for the field list
$cut -d \ | -f 2,3 shortlist | tee cutlist1
will display the second and third columns of shortlist and saves the output in cutlist1. here | is
escaped to prevent it as pipeline character
• To print the remaining fields, we have
$cut –d \ | -f 1,4- shortlist > cutlist2
paste – pasting files

 When we cut with cut, it can be pasted back with the paste command, vertically ratherb than
horizontally.
 We can view two files side by side by pasting them. In the previous topic, cut was used to create the
two files cutlist1 and cutlist2 containing two cut-out portions of the same file.
Example:
$paste cutlist1 cutlist2
$paste -d “|” cutlist1 cutlist2 // We can specify one or more delimiters with -d
 Where each field will be separated by the delimiter |. Even though paste uses at least two files for
concatenating lines, the data for one file can be supplied through the standard input.
Note: please refer ppt for examples

22. The sort command and its usage with different options.
sort: ordering a file
 Sorting is the ordering of data in ascending or descending sequence. The sort command orders a file and
by default, the entire line is sorted
Example: $ cat file.txt
abhishek
chitransh
satish
rajan
naveen
divyam
harsh

$sort file.txt
abhishek
chitransh
divyam
harsh
naveen
rajan

Tanushree K N III_sem_CSE/GAT/2018 Page 24


$sort shortlist
This default sorting sequence can be altered by using certain options. We can also sort one or
mor keys (fileds) or use a different ordering rule.

sort options: The important sort options are:


-tchar uses delimiter char to identify fields
-k n sorts on nth field
-k m,n starts sort on mth field and ends sort on nth field
-k m.n starts sort on nth column of mth field
-u removes repeated lines
-n sorts numerically
-r reverses sort order
-f folds lowercase to equivalent uppercase
-m list merges sorted files in list
-c checks if file is sorted
-o flname places output in file flname

Note: Sample database


$cat shortlist
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
9876 | jai sharma | director | production | 12/03/50 | 7000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
2365 | barun sengupta | director | personnel | 11/05/47 | 7800
5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
4290 | jayant choudhury | executive | production | 07/09/50 | 6000
2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000

Tanushree K N III_sem_CSE/GAT/2018 Page 25


Examples;
1. $sort –t“|” –k 2 shortlist
// sorts the second field (name)

2. $sort –t”|” –r –k 2 shortlist or sort –t”|” –k 2r shortlist


//sort order can be revered with this –r option.
3. $sort –t”|” –k 3,3 –k 2,2 shortlist
// sorting on secondary key is also possible as shown above.
4. $sort –t”|” –k 5.7,5.8 shortlist
//we can also specify a character position with in a field to be the beginning of sort as shown above
(sorting on columns).
5. $sort –n numfile
//when sort acts on numericals, strange things can happen. When we sort a file containing only
numbers, we get a curious result. This can be overridden by –n (numeric) option.
6. $cut –d “|” –f3 emp.lst | sort –u | tee desigx.lst
//Removing repeated lines can be possible using –u option as shown above. If we
cut out the designation filed from emp.lst, we can pipe it to sort to find out the unique
designations that occur in the file.
Other sort options are:
sort –o sortedlist –k 3 shortlist
sort –o shortlist shortlist
sort –c shortlist
sort –t “|” –c –k 2 shortlist
sort –m foo1 foo2 foo3

23. expr: Computation and String Handling


• The Broune shell uses expr command to perform computations. This command combinesthe following
two functions:

 Performs arithmetic operations on integers


 Manipulates strings
Computation: expr can perform the four basic arithmetic operations (+, -, *, /), as well as modulus (%)
functions.

Tanushree K N III_sem_CSE/GAT/2018 Page 26


Examples:
$ x=3 y=5
$ expr 3+5
O/P:8

$ expr $x-$y
O/P;-2

$ expr 3 \* 5 Note:\ is used to prevent the shell from interpreting * as metacharacter


O/P:15

$ expr $y/$x
O/P:1

$ expr 13%5
o/p:3

 expr is also used with command substitution to assign a variable.


Example1:
$ x=6 y=2 : z=`expr $x+$y`
$ echo $z
o/p:8
Example2:
$ x=5
$ x=`expr $x+1`
$ echo $x
o/p;6

String Handling:

 expr is also used to handle strings. For manipulating strings, expr uses two expressions separated by a
colon (:).
 The string to be worked upon is closed on the left of the colon band a regular expression is placed on its
right. Depending on the composition of the expression expr can perform the following three functions:
1. Determine the length of the string.
2. Extract the substring.
3. Locate the position of a character in a string.

Tanushree K N III_sem_CSE/GAT/2018 Page 27


1. Length of the string:
The regular expression.* is used to print the number of characters matching the pattern .
Example1:
$ expr “abcdefg” : ‘.*’
o/p: 7

Example2:
while echo “Enter your name: \c” ;do
read name
if [`expe “$name” :’.*’` -gt 20] ; then
echo “Name is very long”
else
break
fi
done

imp Note:
2. Extract the substring.
3. Locate the position of a character in a string.

Refer text sumithabadas the above concept

24. The umask and default file permissions.


• When you create a file and directories, the default permissions are rw-rw-rw-(octal
666) for regular file rwxrwxrwx(octal 777) for directories
• This default permission are transformed by subtracting the user mask from it to
remove one or more permissions
• Set user masjk using command called umask
$umask 022
$umask
o/p: 022
• This is an octal value which has to be subtracted from the system default to ob tain the
actual default.

Tanushree K N III_sem_CSE/GAT/2018 Page 28


• This becomes 644(666-022) for ordinary files and 755(777-022) for directory files).
• When you create a file on this system, it will have the permissions rw -r--r—(644 for
regular file) and rwxr -xr-x for directory.
• Example:
• Before umask
$cat > foo // create a file
$ls –l foo
o/p: the default permission for a regular file is 666 ,it means rw -rw-rw-
• Now set the umask
$umask 022
// not onl y 022 you can use any value ranging from 000 -666 for regular file and
000-777 for director y file
$umask
o/p:022
• After umask //666 -022=644 is the permission set for the newl y created files.
$cat > foo1
$ls -l foo1
o/p: rw-r—r—

Note: similarly it will apply to the directories

Tanushree K N III_sem_CSE/GAT/2018 Page 29


25. Two special files /dev/null and /dev/tty.
/dev/null:
• If user would like to execute a command but don’t like to see its contents on the screen, to redirect the
output to a file called /dev/null. It is a special file that can accept any stream without growing in size. It’s
size is always zero.
• It is a special file that can accept any stream without growing in size. Its size is always zero.\
Example: refer text (venkateshmurthy)

/dev/tty:
• This file indicates one’s terminal. In a shell script, some times user require to redirect the output of
some select statements explicitly to the terminal. In such cases redirect these explicitly to /dev/tty
inside the script.
• If you would like to execute a command but don’t like to see its contents on the screen, you may wish to
redirect the output of some select statements explicitly to the terminal. In such cases you can redirect
these explicitly to /dev/tty inside the script.

Example: refer text (venkateshmurthy)

-------------ALL THE BEST--------------

Tanushree K N III_sem_CSE/GAT/2018 Page 30

You might also like