The UNIX File System Contains Several Different Types of Files
The UNIX File System Contains Several Different Types of Files
The UNIX File System Contains Several Different Types of Files
UNIX system has thousands of files. If you write a program, you add one more file to the system. When
you compile it you add some more. Files grow rapidly, and if they are not organized properly, you will
find it difficult to locate them. So UNIX has a file system (UFS) to manage or organizes its own files in
directory.
1. UNIX FILES AND BASIC FILE TYPES/CATEGORIES
FILES SUPPORTED BY UNIX FILE SYSTEM
File is a collection of records. So, files are divided into three categories
a. Ordinary file
b. Directory file
c. Device file
The UNIX file system contains several different types of files:
a.Ordinary Files or regular files
It contains only data as a stream of characters.
An ordinary files itself divided into 2 types
Text file: contains only printable characters, and you can often view the contents and make sense
out of them. All C and Java files are example of text file. A text file contains lines of characters
where every line is terminated with the newline character, also known as linefeed (LF) when you
press Enter while inserting text, the LF character is appended to every line. You won’t see this
character normally, but there is command (od) which can make it visible.
Binary file: it contains both printable and unprintable characters that cover the entire ASCII
range(0 to 255).most UNIX commands are example of binary files.
b. Directory files
i. Contains no data, but keeps some details of the files and subdirectories that it contains.
ii. A directory file contains an entry for every file and sub directory that it houses. Each entry has
two components
• The filename
• A unique Identification number for the file or directory( called the inode number)
iii. A directory contains the filename but not the contents of file.
iv. When you create or remove a file the kernel automatically updates its corresponding
directory by adding or removing the enter i.e inode number associated with that file.
c. Device files
i. Used to represent a real physical device such as a printer, tape drive or terminal, used for
Input/Ouput (I/O) operations
ii. Unix considers any device attached to the system to be a file - including your terminal:
iii. By default, a command treats your terminal as the standard input file (stdin) from which to
read its input
iv. Your terminal is also treated as the standard output file (stdout) to which a command's output
is sent.
v. stdin and stdout will be discussed in more detail later
vi. Two types of I/O: character and block
vii. Usually only found under directories named /dev
The implicit feature of every UNIX file system is that there is a top which serves as reference point for
all files.This top is called root & is represented by a /(front slash). Root is actually a directory. The root
Padmaja.k, Asst Prof, Dept of Page 31
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
directory has a number of sub directories under it. These sub directories in turn have more sub directories
and others files under them.
For instance bin and usr are two directories directly under root, while a second bin and kumar are sub
directories under usr.
Every file apart from root must have a parent. Thus the home directory is the parent of kumar , while /
is the parent of home and grandparent of kumar. If you create a file login.sql under the kumar directory
,then kumar will be the parent of this file.
The first group contains the files that are made available during system installation
• /bin and /usr/bin: these are the directories where all the commonly used UNIX commands are
found.
• /sbin and /usr/sbin: If there’s a command that you can’t execute but the system administrator
can execute, it would be probably in one of these directories.
• /etc: this directory contains the configuration files of the system. You can change a very important
aspect of system functioning by editing a text file in this directory. Your login name and password
are stored in files /etc/passwd and etc/shadow
• /dev: This directory contains all device files. These files don’t occupy space on disk.there could
be more sub directories like pts, dsk and rdsk in this directory
• /lib and /usr/lib: Contains shared library files and sometimes other kernel-related files.
• /usr and /include: contains the standard header files used by C programs. The statement
#include<stdio.h> used in most C programs referes to the file stdio.h in this directory.
• /usr/share/man: this is where the man pages are stored. There are separate subdirectories
here(like man1,man2 etc) that contains the pages for each section. For instance, the man page of
ls can be found in /usr/share/man/man1
User also work with their own files, they write programs, send and receive mail and also create temporary
files. These files are available in the second group shown below
• /tmp: the directory where users are allowed to create temporary files. These files are wiped
away regularly by the system
• /var: The variable part of the file system. Contains all your print jobs and your outgoing and
incoming mail.
• /home:On many systems users are housed here.Kumar would have his home directory in
/home/kumar
5. PATH VARIABLE:
• The PATH environment variable is a colon-delimited list of directories that your shell searches
through when you enter a command.
• Program files (executables) are kept in many different places on the Unix system. Your path
tells the Unix shell where to look on the system when you request a particular program.
• To find out what your path is, at the Unix shell prompt echo $PATH
• Your path will look something like the following.
/usr2/username/bin:/usr/local/bin:/usr/bin:.
You will see your username in place of username. Using the above example path, if you enter the ls
command, your shell will look for the appropriate executable file in the following order: first, it would
look through the directory /usr2/username/bin, then /usr/local/bin, then /usr/bin, and finally the local
directory, indicated by the . (a period).
If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the
prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.
Ex 6.2.2:When cd used without arguments: cd when used without arguments reverts to home directory
Padmaja.k, Asst Prof, Dept of Page 33
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
$pwd
/home/kumar/gmit
$cd
cd without argument will change directory from gmit to its home directory Kumar
$pwd
/home/kumar
Ex 6.2.3: If your present working directory is /home/Kumar and you need to switch to /bin directory
directly, use absolute pathname i.e /bin wd cd command
$pwd
/home/kumar
$cd /bin
$pwd
/bin
Ex 6.3.2: To create three directories at a time, named patch, dbs, doc, pass directory names as argu-
ments.
$mkdir patch dbs doc
Error is due to the fact that the parent directory named gmit is not created before creating sub directo-
ries cse and ise.
Ex 6.3.5: $mkdir test
mkdir: Failed to make directory “test”; Permission denied.
This can happen due to:
a. The directory named test may already exist
b. There may be an ordinary file by the same name in the current directory.
c. The permissions set for the current directory do not permit the creation of files and directories by the
user.
Command Function
cd Returns you to your login directory
cd ~ Also returns you to your login directory
cd / Takes you to the entire system’s root directory
cd /root Takes you to the home directory of the root or superuser,account
created at installation, you must be root user to access this directory.
cd /home Takes you to the home directory where user login directories are
usually stored
cd .. Moves you up one directory
cd ~otheruser Takes you to the otheruser’s login directory
cd /dir/subdirfoo Regardless of which directory you are in, the absolute path takes you
directly to subdirfoo, a subdirectory of dir.
Ex .6.6.1: Assume the current directory is /home/kumar/progs/data/text, using cd .. will move one level
up
$pwd
/home/kumar/progs/data/text
$ cd ..
$pwd
/home/kumar/progs/data
Ex 6.6.3: My present location is /etc/samba and now I want to change directory to /etc.
Using relative path: $ cd ..
Using absolute path: $cd /etc
Ex 6.6.4: My present location is /var/ftp/ and I want to change the location to /var/log
Using relative path: cd ../log
Using absolute path: cd /var/log
$ls
Output:08_packets.html
calendar
dept.lst
emp.lst
helpdir
uskdsk06
ls options:
• Output in multiple columns(-x):
$ls -x
08_packets.html calendar dept.lst emp.lst
helpdir progs usdsk07 usdsk07
If we specify two directories named helpdir and progs , the contents of the directory i,e filenames are
listed out.
• Recursive listing(-R)
The recursive option lists all sub-directories and files in a directory tree structure.
$ls -xR
08_packets.html calendar cptodos.sh dept.lst
emp.lst helpdir progs usdsk07
./helpdir
forms.hlp graphics.hlp
./progs
arrays.pl n2words.pl
cp chap01 unit1
if destination file i.e unit1 does not exist, first it will be created before copying.if not it will
be simply overwritten without any warning.
• Copying a file to another directory
ex: assume there is a file named chap01 and it has to be copied to progs directory
cp chap01 progs
output: chap01 is now copied to directory named progs with the same name chap01.
• Copying a file to another directory with different name
ex: assume there is a file named chap01 and it has to be copied to progs directory with chap01
file renamed as unit1
cp chap01 progs/unit1
output: chap01 is now copied to directory named progs with the same name unit1
9.1 cp options:
Interactive copying (-i): the -i option warns the user before overwriting the destination file.
Ex: $ cp -i chap01 unit1
cp: overwrite unit1(yes/no)? y
A y at this prompt will overwrite the file.
Copying directory structure(-R) : the -R command behaves recursively to copy an entire directory
structure say progs to newprogs.
Ex say progs directory contains three files kernel, bash, korn. To copy all three files under progs to
newprogs directory
$ cp -R progs newprogs
10. rm : deleting files
The rm command deletes one or more files.
Ex 1: The following command deletes three files chap01, chap02, chap03.
$ rm chap01 chap02 chap03
Ex 2: to delete files named chap01 and chap02 under progs directory
$ rm progs/chap01 progs/chap02
Ex 3: to remove all file
$ rm*
10.1 rm options:
Interactive deletion (-i): the –i optin makes the command ask the user for confirmation before
removing each file.
$rm -i chap01 chap02 chap03
rm: remove chap01(yes/no)?y
rm: remove chap01(yes/no)?y
rm: remove chap01(yes/no)?y
Recursive deletion(-r or -R) deletes all subdirectories and files recursively. Rm wont normally
To rename a directory:
$ mv pts perdir
pts directory is renamed as perdir
$ od -b odfile
The -b option displays the octal values for each character.
000000 127 150 151 164 145 040 163 160 141 143 145 040 151 156 143 154
000000 165 144 145 163 040 141 040 011 012 124 150 145 040 007 040 143
Each line displays 16 bytes of data in octal , preceded by the offset in the file of the first byte in the
line.
The octal equivalent of characters are displayed ex for W- 127, i-151, \t (tab)-011, \n(newline)-012
^G(Bell character)- 007
1.2 Links
1.3 Ownership
1.4 Group ownership
1.5 File size
1.6 Last Modification date and time
1.7 File name
1.1 The file type and its permissions: The first column shows the type and permissions associated with
each file.The first character in this column is mostly a – which indicates that the file is an ordinary one.
In unix, file system has three types of permissions- read, write and execute.
1.2 Links: The second column indicates the number of links associated with the file. This is actually the
number of filenames maintained by the system of that file.
1.3 Ownership: The third column shows the owner of files. The owner has full authority to tamper with
files content and permissions. Similarly, you can create, modify or remove files in a directory if you are
the owner of the directory.
1.4 Group ownership: The fourth column represents the group owner of the file. When opening a user
account, the system admin also assigns the user to some group. The concept of a group of users also
owning a file has acquired importance today as group members often need to work on the same file.
1.5 File size: The fifth column shows the size of the file in bytes. The important thing to remember here
is that it only a character count of the file and not a measure of the disk space that it occupies.
1.6 Last modification time: The sixth, seventh and eighth columns indicate the last modification time of
the file, which is stored to the nearest second. A file is said to be modified only if its content have changed
in any way.If the file is less than a year old since its last modification time, the year won’t be displayed.
1.7 Filename: The last column displays the filename arranged in ASCII collating sequence.
For 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
drwxr-xr-x 2 kumar metal 512 may 09 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 09 09:57 progs
• Directories are easily identified in the listing by the first character of the first column, which
here shows a d.
• To see the attributes of a directory rather than the files contained in it, use ls –ld with the direc-
tory name.
3. FILE OWNERSHIP
• When you create a file, you become its owner and it shows up in the third column of the files
listing.
• Group name is seen in the fourth column; your group is the group owner of the file.
• 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.
• 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
4. FILE PERMISSIONS
• UNIX has a simple and well defined system of assigning permissions to files.
• Lets issue the ls –l command once again to view the permissions of a few lines .
$ls -l chap02 dept.lst dateval.sh
-rwxr-xr-- 1 kumar metal 25000 May 10 19:21 chap02
-rwxr-xr-x 1 kumar metal 890 Jan 10 23:17 dept.lst
-rw-rw-rw- 1 kumar metal 84 Feb 18 12:20 dateval.sh
Third group(r--):
• has the write and execute bits absent.
• This set is applicable to others i,e those who are neither the owner nor group.
• This category is referred to as the world.
Ex 1:
$ls –l xstart
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart
Here user is having the only read and execute permission .
Using relative file permission need to add the execute permission to user
chmod category operation(+,-) permission filename.
$chmod u + x xstart
$chmod u+x xstart
$ ls –l xstart
-rwxr--r-- 1 kumar metal 1906 sep 23:38 xstart
After executing the chmod command, the command assigns (+) execute (x) permission to the user (u),
other permissions remain unchanged.
Ex 2: To remove execute permission from all and assign read permission to group and others
$chmod a-x, go+r xstart /*to remove execute permission from all(a)ie user, group, others
/*to assign read permission to group and others (go+r)
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.
Ex 2:
To assign read and write for user and remove write, execute permissions from group and others
• Here to assign rw- corresponds to digit 6
• Remove write , execute permissions is nothing but assigning only read option to group and oth-
ers
• Only read permission is r—corresponds to 4
$chmod 644 xstart
Ex 3:
To assign all permissions to the owner, read and write to group and only execute for others.
$chmod 761 xstart
Ex 4
To assign all permissions to all categories.
$chmod 777 xstart
This is simply useless but still the user can delete this file
On the other hand,
chmod a+rwx xstart or
chmod 777 xstart
After Executing either of the one command it adds the all permission to all categories as shown
below
-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.
This makes all the files and subdirectories found in the shell_scripts directory, executable by all users.
6. DIRECTORY PERMISSIONS
• Directories also have their own permissions and the significance of these permissions differ
from those of ordinary files.
• The default permissions of a directory are,
rwxr-xr-x (755)
• A directory must never be writable by group and others
Ex1:
$mkdir c_progs ; ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
Here the c_progs directory is created (mkdir c_progs) and then the attributes of directory is listed out(ls
–ld 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.
The group owner of the file dept.lst is changed from metal to dba by issuing the command
$chgrp dba dept.lst
a=10
until [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
This loop continues forever because a is always greater than or equal to 10 and it is never less than
10.
a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done
Upon execution, you will receive the following result. In the inner loop, you have a break command
with the argument 2. This indicates that if a condition is met you should break out of outer loop and
ultimately from the inner loop as well.
1 0
1 5
The continue statement
The continue statement is similar to the break command, except that it causes the current iteration
of the loop to exit, rather than the entire loop.
This statement is useful when an error has occurred but you want to try to execute the next iteration
of the loop.
Padmaja.k, Asst Prof, Dept of Page 49
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
Syntax
continue
Like with the break statement, an integer argument can be given to the continue command to skip
commands from nested loops.
continue n
Here n specifies the nth enclosing loop to continue from.
Example
The following loop makes use of the continue statement which returns from the continue statement
and starts processing the next statement −
Live Demo
#!/bin/sh
NUMS="1 2 3 4 5 6 7"
Once you have good programming practice you will gain the expertise and thereby, start using
appropriate loop based on the situation. Here, while and for loops are available in most of the other
programming languages like C, C++ and PERL, etc.
Nesting Loops
All the loops support nesting concept which means you can put one loop inside another similar one
or different loops. This nesting can go up to unlimited number of times based on your requirement.
Here is an example of nesting while loop. The other loops can be nested based on the programming
requirement in a similar way −
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
This will produce the following result. It is important to note how echo -n works here. Here -n option
lets echo avoid printing a new line character.
0
1 0
2 1 0
Padmaja.k, Asst Prof, Dept of Page 51
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
CASE STATEMENTS
Syntax
The syntax is as follows:
case $variable-name in
pattern1)
command1
...
....
commandN
;;
pattern2)
command1
...
....
commandN
;;
patternN)
command1
...
....
commandN
;;
*)
esac
OR
case $variable-name in
pattern1|pattern2|pattern3)
command1
...
....
commandN
;;
pattern4|pattern5|pattern6)
command1
...
....
commandN
;;
pattern7|pattern8|patternN)
command1
...
Padmaja.k, Asst Prof, Dept of Page 52
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
....
commandN
;;
*)
esac
• The case statement allows you to easily check pattern (conditions) and then process a command-line if that
condition evaluates to true.
• In other words the $variable-name is compared against the patterns until a match is found.
• *) acts as default and it is executed if no match is found.
• The pattern can include wildcards.
• You must include ;; at the end of each commandN. The shell executes all the statements up to the two
semicolons that are next to each other.
• The esac is always required to indicate end of case statement.
Example
Create a shell script called rental.sh:
#!/bin/bash
chmod +x rental.sh
./rental.sh
./rental.sh jeep
./rental.sh enfield
./rental.sh bike
Sample outputs:
Sorry, I can not get a *** Unknown vehicle *** rental for you!
Padmaja.k, Asst Prof, Dept of Page 53
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
The case statement first checks $rental against each option for a match. If it matches "car", the echo command will
display rental for car. If it matches "van", the echo command will display rental for van and so on. If it matches
nothing i.e. * (default option), an appropriate warning message is printed.
The following shell script demonstrate the concept of command line parameters processing using the case
statement (casecmdargs.sh):
#!/bin/bash
OPT=$1 # option
FILE=$2 # filename
Run it as follows:
chmod +x casecmdargs.sh
./casecmdargs.sh
./casecmdargs.sh -e /tmp/file
./casecmdargs.sh -E /tmp/file
./casecmdargs.sh -e
./casecmdargs.sh -D
#!/bin/bash
# A shell script to backup mysql, webserver and files to tape
opt=$1
case $opt in
sql)
echo "Running mysql backup using mysqldump tool..."
;;
sync)
echo "Running backup using rsync tool..."
;;
tar)
echo "Running tape backup using tar tool..."
;;
*)
echo "Backup shell script utility"
echo "Usage: $0 {sql|sync|tar}"
echo " sql : Run mySQL backup utility."
echo " sync : Run web server backup utility."
echo " tar : Run tape backup utility." ;;
esac
chmod +x allinonebackup.sh
# run sql backup
./allinonebackup.sh sql
# Dump file system using tape device
./allinonebackup.sh tar
# however, the following will fail as patterns are case sensitive
# you must use command line argument tar and not TAR, Tar, TaR etc.
./allinonebackup.sh TAR
SHIFT COMMAND
• The shift command in UNIX is used to move the command line arguments to one position left. The first
argument is lost when you use the shift command.
• Shifting command line arguments is useful when you perform a similar action to all arguments one-by-one,
without changing the variable name. The shift command throws away the left-most variable (argument
number 1) and reassigns values to the remaining variables.
• The value in $2 moves to $1, the value in $3 moves to $2, and so on.
EXECUTION RESULTS:
SET COMMAND
Syntax
The basic syntax:
set [options]
set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
#!/bin/bash
set -x # Turn on debug
....
...
...
set +x # Turn off debug
....
...
POSITIONAL PARAMETRES
A positional parameter is a parameter denoted by one or more digits, other than the single digit 0
. Positional parameters are assigned from the shell's arguments when it is invoked, and may be
reassigned using the set builtin command.
Positional Parameters
When we last left our script, it looked something like this:
#!/bin/bash
##### Constants
##### Functions
system_info()
{
echo "<h2>System release info</h2>"
echo "<p>Function not yet implemented</p>"
} # end of system_info
show_uptime()
{
echo "<h2>System uptime</h2>"
echo "<pre>"
uptime
echo "</pre>"
} # end of show_uptime
drive_space()
{
echo "<h2>Filesystem space</h2>"
Padmaja.k, Asst Prof, Dept of Page 57
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
echo "<pre>"
df
echo "</pre>"
} # end of drive_space
home_space()
{
# Only the superuser can get this information
} # end of home_space
##### Main
We have most things working, but there are several more features we can
add:
2. Let's offer an interactive mode that will prompt for a file name and
warn the user if the file exists and prompt the user to overwrite it.
All of these features involve using command line options and arguments.
To handle options on the command line, we use a facility in the shell
called positional parameters. Positional parameters are a series of special
variables ($0 through $9) that contain the contents of the command line.
If some_program were a bash shell script, we could read each item on the
command line because the positional parameters contain the following:
#!/bin/bash
#!/bin/bash
Second, the shell maintains a variable called $# that contains the number
of items on the command line in addition to the name of the command
($0).
#!/bin/bash
if [ $# -gt 0 ]; then
echo "Your command line contains $# arguments"
else
echo "Your command line contains no arguments"
fi
Command Line Options
As we discussed before, many programs, particularly ones from the GNU
Project, support both short and long command line options. For example,
to display a help message for many of these programs, we may use either
the "-h" option or the longer "--help" option. Long option names are
typically preceded by a double dash. We will adopt this convention for our
scripts.
interactive=
filename=~/sysinfo_page.html
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
The first two lines are pretty easy. We set the variable interactive to be
empty. This will indicate that the interactive mode has not been requested.
Then we set the variable filename to contain a default file name. If
nothing else is specified on the command line, this file name will be used.
After these two variables are set, we have default settings, in case the user
does not specify any options.
Next, we construct a while loop that will cycle through all the items on the
command line and process each one with case. The case will detect each
possible option and process it accordingly.
Now the tricky part. How does that loop work? It relies on the magic
of shift.
#!/bin/bash
done
Getting an Option's Argument
Our "-f" option requires a valid file name as an argument. We
use shift again to get the next item from the command line and assign it
to filename. Later we will have to check the content of filename to make
sure it is valid.
#!/bin/bash
##### Constants
##### Functions
system_info()
{
echo "<h2>System release info</h2>"
echo "<p>Function not yet implemented</p>"
} # end of system_info
show_uptime()
{
echo "<h2>System uptime</h2>"
echo "<pre>"
uptime
echo "</pre>"
} # end of show_uptime
drive_space()
{
echo "<h2>Filesystem space</h2>"
echo "<pre>"
df
echo "</pre>"
} # end of drive_space
home_space()
{
# Only the superuser can get this information
} # end of home_space
write_page()
{
cat <<- _EOF_
<html>
<head>
<title>$TITLE</title>
Padmaja.k, Asst Prof, Dept of Page 63
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
</head>
<body>
<h1>$TITLE</h1>
<p>$TIME_STAMP</p>
$(system_info)
$(show_uptime)
$(drive_space)
$(home_space)
</body>
</html>
_EOF_
usage()
{
echo "usage: sysinfo_page [[[-f file ] [-i]] | [-h]]"
}
##### Main
interactive=
filename=~/sysinfo_page.html
response=
if [ -f $filename ]; then
echo -n "Output file exists. Overwrite? (y/n) > "
read response
if [ "$response" != "y" ]; then
echo "Exiting program."
exit 1
fi
fi
fi
We display the current value of filename since, the way this routine is
coded, if the user just presses the enter key, the default value
of filename will be used. This is accomplished in the next two lines where
the value of response is checked. If response is not empty,
then filename is assigned the value of response. Otherwise, filename is
left unchanged, preserving its default value.
After we have the name of the output file, we check if it already exists. If it
does, we prompt the user. If the user response is not "y," we give up and
exit, otherwise we can proceed.
COMMAND <<InputComesFromHERE
...
...
...
InputComesFromHERE
A limit string delineates (frames) the command list. The special symbol << precedes the limit
string. This has the effect of redirecting the output of a command block into the stdin of the
program or command. It is similar to interactive-program < command-file, where command-
file contains
command #1
command #2
...
interactive-program <<LimitString
command #1
command #2
...
LimitString
Choose a limit string sufficiently unusual that it will not occur anywhere in the command list and
confuse matters.
Note that here documents may sometimes be used to good effect with non-interactive utilities and
commands, such as, for example, wall.
#!/bin/bash
wall <<zzz23EndOfMessagezzz23
E-mail your noontime orders for pizza to the system administrator.
(Add an extra dollar for anchovy or mushroom topping.)
# Additional message text goes here.
# Note: 'wall' prints comment lines.
zzz23EndOfMessagezzz23
exit
Even such unlikely candidates as the vi text editor lend themselves to here documents.
TRAP COMMAND
When you press the Ctrl+C or Break key at your terminal during execution of a shell program,
normally that program is immediately terminated, and your command prompt returns. This may not
always be desirable. For instance, you may end up leaving a bunch of temporary files that won't get
cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax −
$ trap commands signals
Here command can be any valid Unix command, or even a user-defined function, and signal can be
a list of any number of signals you want to trap.
There are two common uses for trap in shell scripts −
You can modify the preceding trap to also remove the two specified files in this case by adding signal
number 1 to the list of signals −
$ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2
Now these files will be removed if the line gets hung up or if the Ctrl+C key gets pressed.
The commands specified to trap must be enclosed in quotes, if they contain more than one command.
Also note that the shell scans the command line at the time that the trap command gets executed and
also when one of the listed signals is received.
Thus, in the preceding example, the value of WORKDIR and $$ will be substituted at the time that
the trap command is executed. If you wanted this substitution to occur at the time that either signal 1
or 2 was received, you can put the commands inside single quotes −
$ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2
Ignoring Signals
If the command listed for trap is null, the specified signal will be ignored when received. For example,
the command −
$ trap '' 2
This specifies that the interrupt signal is to be ignored. You might want to ignore certain signals when
performing an operation that you don't want to be interrupted. You can specify multiple signals to be
ignored as follows −
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is not equivalent to writing
the following, which has a separate meaning of its own −
$ trap 2
If you ignore a signal, all subshells also ignore that signal. However, if you specify an action to be
taken on the receipt of a signal, all subshells will still take the default action on receipt of that signal.
Resetting Traps
After you've changed the default action to be taken on receipt of a signal, you can change it back
again with the trap if you simply omit the first argument; so −
$ trap 1 2
This resets the action to be taken on the receipt of signals 1 or 2 back to the default.
test - command
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[]
[ OPTION
DESCRIPTION
Padmaja.k, Asst Prof, Dept of Page 68
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
Tag Description
--version
An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit
status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
EXPRESSION1 -o EXPRESSION2
-n STRING
-z STRING
STRING1 = STRING2
STRING1 != STRING2
FILE1 and FILE2 have the same device and inode numbers
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be
Padmaja.k, Asst Prof, Dept of Page 71
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
escaped (e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of
STRING.
NOTE: your shell may have its own version of test and/or [, which usually supersedes the version
described here. Please refer to your shell’s documentation for details about the options it supports.
• Arithmetic Operators
• Relational Operators
• Boolean Operators
• String Operators
• File Test Operators
Bourne shell didn't originally have any mechanism to perform simple arithmetic operations but it uses
external programs, either awk or expr.
The following example shows how to add two numbers −
Live Demo
#!/bin/sh
val=`expr 2 + 2`
echo "Total value : $val"
Arithmetic Operators
The following arithmetic operators are supported by Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
Show Examples
+ (Addition) Adds values on either side of the operator `expr $a + $b` will give 30
- (Subtraction) Subtracts right hand operand from left hand `expr $a - $b` will give -10
operand
* (Multiplication) Multiplies values on either side of the operator `expr $a \* $b` will give
200
/ (Division) Divides left hand operand by right hand operand `expr $b / $a` will give 2
% (Modulus) Divides left hand operand by right hand operand `expr $b % $a` will give 0
and returns remainder
== (Equality) Compares two numbers, if both are same then [ $a == $b ] would return
returns true. false.
!= (Not Equality) Compares two numbers, if both are different then [ $a != $b ] would return
returns true. true.
It is very important to understand that all the conditional expressions should be inside square braces
with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is incorrect.
All the arithmetical calculations are done using long integers.
Relational Operators
Bourne Shell supports the following relational operators that are specific to numeric values. These
operators do not work for string values unless their value is numeric.
For example, following operators will work to check a relation between 10 and 20 as well as in
between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then −
Show Examples
-eq Checks if the value of two operands are equal or not; if yes, [ $a -eq $b ] is not true.
then the condition becomes true.
-ne Checks if the value of two operands are equal or not; if values
[ $a -ne $b ] is true.
are not equal, then the condition becomes true.
-gt Checks if the value of left operand is greater than the value of
[ $a -gt $b ] is not true.
right operand; if yes, then the condition becomes true.
-lt Checks if the value of left operand is less than the value of
[ $a -lt $b ] is true.
right operand; if yes, then the condition becomes true.
-le Checks if the value of left operand is less than or equal to the
value of right operand; if yes, then the condition becomes [ $a -le $b ] is true.
true.
It is very important to understand that all the conditional expressions should be placed inside square
braces with spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.
Boolean Operators
The following Boolean operators are supported by the Bourne Shell.
Assume variable a holds 10 and variable b holds 20 then −
Show Examples
-o This is logical OR. If one of the operands is true, [ $a -lt 20 -o $b -gt 100 ] is true.
then the condition becomes true.
String Operators
The following string operators are supported by Bourne Shell.
= Checks if the value of two operands are equal or not; if yes, [ $a = $b ] is not true.
then the condition becomes true.
-z Checks if the given string operand size is zero; if it is zero [ -z $a ] is not true.
length, then it returns true.
str Checks if str is not the empty string; if it is empty, then it [ $a ] is not false.
returns false.
-b file Checks if file is a block special file; if yes, then the condition [ -b $file ] is false.
becomes true.
-c file Checks if file is a character special file; if yes, then the [ -c $file ] is false.
condition becomes true.
-d file Checks if file is a directory; if yes, then the condition [ -d $file ] is not true.
becomes true.
-g file Checks if file has its set group ID (SGID) bit set; if yes, then [ -g $file ] is false.
the condition becomes true.
-k file Checks if file has its sticky bit set; if yes, then the condition [ -k $file ] is false.
becomes true.
-p file Checks if file is a named pipe; if yes, then the condition [ -p $file ] is false.
becomes true.
-t file Checks if file descriptor is open and associated with a [ -t $file ] is false.
terminal; if yes, then the condition becomes true.
-u file Checks if file has its Set User ID (SUID) bit set; if yes, then [ -u $file ] is false.
the condition becomes true.
-r file Checks if file is readable; if yes, then the condition becomes [ -r $file ] is true.
true.
-w file Checks if file is writable; if yes, then the condition becomes [ -w $file ] is true.
true.
-x file Checks if file is executable; if yes, then the condition [ -x $file ] is true.
becomes true.
-s file Checks if file has size greater than 0; if yes, then condition [ -s $file ] is true.
becomes true.
-e file Checks if file exists; is true even if file is a directory but [ -e $file ] is true.
exists.
Exit Status
• Every Linux command executed by the shell script or user, has an exit status.
• The exit status is an integer number.
• The Linux man pages stats the exit statuses of each command.
• 0 exit status means the command was successful without any errors.
• A non-zero (1-255 values) exit status means command was failure.
• You can use special shell variable called $? to get the exit status of the previously executed command. To
print $? variable use the echo command:
echo $?
date # run date command
echo $? # print exit status
foobar123 # not a valid command
echo $? # print exit status
date
echo $?
Sample Output:
date1
echo $?
ls /eeteec
echo $?
Sample Output:
According to ls man page - exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
ls -l /tmp
status=$?
Padmaja.k, Asst Prof, Dept of Page 77
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
#!/bin/bash
# set var
PASSWD_FILE=/etc/passwd
chmod +x finduser.sh
./finduser.sh
Sample Outputs:
Run it again:
chmod +x finduser.sh
./finduser.sh
Sample Outputs:
You can combine the grep and if command in a single statement as follows:
then
echo "User '$username' found in $PASSWD_FILE file."
else
echo "User '$username' not found in $PASSWD_FILE file."
fi
An exit code, or sometimes known as a return code, is the code returned to a parent
process by an executable. On POSIX systems the standard exit code is 0 for success
and any number from 1 to 255 for anything else.
Exit codes can be interpreted by machine scripts to adapt in the event of successes of failures. If exit
codes are not set the exit code will be the exit code of the last run command.
cat file.txt
hello world
echo $?
0
The command was successful. The file exists and there are no errors in reading the file or writing it to
the terminal. The exit code is therefore 0.
In the following example the file does not exist.
cat doesnotexist.txt
cat: doesnotexist.txt: No such file or directory
echo $?
1
#!/bin/bash
cat file.txt
if [ $? -eq 0 ]
then
Padmaja.k, Asst Prof, Dept of Page 79
CSE, Mycem,Mysore
UNIX AND SHELL PROGRAMMING
If the command was unsuccessful the exit code will be 0 and ‘The script ran ok’ will be printed to the
terminal.
How to set an exit code
To set an exit code in a script use exit 0 where 0 is the number you want to return. In the following
example a shell script exits with a 1. This file is saved as exit.sh.
#!/bin/bash
exit 1
Executing this script shows that the exit code is correctly set.
bash exit.sh
echo $?
1
#!/bin/bash
• /etc/profile
• profile
The process is as follows −
• The shell checks to see whether the file /etc/profile exists.
• If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
• The shell checks to see whether the file .profile exists in your home directory. Your home
directory is the directory that you start out in after you log in.
• If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
As soon as both of these files have been read, the shell displays a prompt −
$
This is the prompt where you can enter commands in order to have them executed.
Note − The shell initialization process detailed here applies to all Bourne type shells, but some
additional files are used by bash and ksh.
Your prompt will become =>. To set the value of PS1 so that it shows the working directory, issue the
command −
=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
The result of this command is that the prompt displays the user's username, the machine's name
(hostname), and the working directory.
There are quite a few escape sequences that can be used as value arguments for PS1; try to limit
yourself to the most critical so that the prompt does not overwhelm you with information.
1
\t
Current time, expressed as HH:MM:SS
2
\d
Current date, expressed as Weekday Month Date
3
\n
Newline
4
\s
Current shell environment
5
\W
Working directory
6
\w
Full path of the working directory
7
\u
Current user’s username
8
\h
9
\#
Command number of the current command. Increases when a new command is
entered
10
\$
If the effective UID is 0 (that is, if you are logged in as root), end the prompt with
the # character; otherwise, use the $ sign
You can make the change yourself every time you log in, or you can have the change made
automatically in PS1 by adding it to your .profile file.
When you issue a command that is incomplete, the shell will display a secondary prompt and wait for
you to complete the command and hit Enter again.
The default secondary prompt is > (the greater than sign), but can be changed by re-defining
the PS2 shell variable −
Following is the example which uses the default secondary prompt −
$ echo "this is a
> test"
this is a
test
$
1
DISPLAY
Contains the identifier for the display that X11 programs should use by default.
2
HOME
Indicates the home directory of the current user: the default argument for the
cd built-in command.
3
IFS
Indicates the Internal Field Separator that is used by the parser for word splitting
after expansion.
4
LANG
LANG expands to the default system locale; LC_ALL can be used to override this.
For example, if its value is pt_BR, then the language is set to (Brazilian)
Portuguese and the locale to Brazil.
5
LD_LIBRARY_PATH
A Unix system with a dynamic linker, contains a colonseparated list of directories
that the dynamic linker should search for shared objects when building a process
image after exec, before searching in any other directories.
6
PATH
Indicates the search path for commands. It is a colon-separated list of directories
in which the shell looks for commands.
7
PWD
Indicates the current working directory as set by the cd command.
8
RANDOM
Generates a random integer between 0 and 32,767 each time it is referenced.
9
SHLVL
Increments by one each time an instance of bash is started. This variable is useful
for determining whether the built-in exit command ends the current session.
10
TERM
Refers to the display type.
11
TZ
Refers to Time zone. It can take values like GMT, AST, etc.
12
UID
Expands to the numeric user ID of the current user, initialized at the shell startup.
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$
readonly var
readonly var=value
readonly p=/tmp/toi.txt
# error
p=/tmp/newvale
readonly -f functionName
• Make it readonly:
readonly -f hello
# invoke it
hello
Sample outputs:
readonly
OR
readonly -p
Sample outputs:
readonly
declare -ar BASH_VERSINFO='([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" [5]="i486-
pc-linux-gnu")'
declare -ir EUID="1000"
declare -ir PPID="7628"
declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-
comments:monitor"
declare -ir UID="1000"
declare -r pwdfile="/etc/passwd"
readonly -f
Sample outputs:
hello ()
{
echo "Hello world"
}
declare -fr hello