@vtucode - in BCS515C Module 2 PDF
@vtucode - in BCS515C Module 2 PDF
@vtucode - in BCS515C Module 2 PDF
ls with options:
Option Description
-x Multicolumnar Output
-F Marks executables with *, directories with / and symbolic
link with @
-a Shows all filenames beginning with a dot including . and ..
-R Recursive list
-r Sorts filenames in reverse order(ASCII collating sequence
by default)
-l Long listing in ASCII collating sequence seven attributes of
a file.
-d dirname Lists only dirname if dirname is a directory.
-t Sort filenames by last modification time.
-lt Sorts listing by last modification time.
-u Sorts filenames by last access time.
-lu Sorts by ASCII collating sequence but listing shows last
access time.
-lut As above but sorted by last access time.
-i Displays inode number.
vtucode.in Page 1
1. File type & Permission:
The first column shows the type and permissions associated
with each file. The first character in this column is mostly a -,
which indicates that file is an ordinary one.
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.
3. Ownership:
When you create a file, you automatically become its owner. The
third column shows kumar is the owner of all of these files. The
owner has full authority to tamper with a files contents and
permissions—a privilege not available with others except the
root user.
4. Group Ownership:
The fourth column represents the group owner of the file. Every
user is attached to a group owner. Every member of that group
can access the file depending on the permission assigned.
5. File size:
File size in bytes is displayed. It is the number of character in
the file rather than the actual size occupied on disk.
For Ex:
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs
vtucode.in Page 2
Changing File Permission:
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 -rw-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.
Relative Permissions:
• chmod only changes the permissions specified in the command line
and leaves the other permissions unchanged.
• Its syntax is:
chmod category operation permission filename(s)
• chmod takes an expression as its argument which contains:
• user category (user, group, others).
• operation to be performed (assign or remove a permission).
• type of permission (read, write, execute).
Example:
• Initially,
-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart
$chmod u+x xstart
-rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart
• The command assigns (+) execute (x) permission to the user (u),
other permissions remain unchanged.
$chmod ugo+x xstart or chmod a+x xstart or chmod +x
xstart
$ls –l xstart
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
vtucode.in Page 3
• Let Initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
$chmod go-r xstart
Then it becomes
$ls –l xstart
-rwx—x--x 1 kumar metal 1906 sep 23:38 xstart
Absolute Permissions:
• Here, we need not to know the current file permissions. We can set
all nine permissions explicitly. A string of three octal digits is used
as an expression. 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:
vtucode.in Page 4
• Only owner can change the file permissions. User cannot change
other user‘s file‘s permissions.
• But the system administrator can do anything.
Directory Permissions:
• It is possible that a file cannot be accessed even though it has read
permission, and can be removed even when it is write protected. The
default permissions of a directory are,
rwxr-xr-x (755)
This makes all files and subdirectories found in the tree-walk executable by
all users. We can provide multiple directory and filenames.
If we want to use chmod on your home directory tree then “cd” to it and use
it in one of these ways:
vtucode.in Page 5
The Shell Interpretive Cycle
Introduction:
The shell sits between you and the operating system, acting as a
command interpreter. It reads your terminal input and translates the
commands into actions taken by the system. The shell is analogous to
command.com in DOS. When you log into the system you are given a default
shell. When the shell starts up it reads its startup files and may set
environment variables, command search paths, and command aliases, and
executes any commands specified in these files. The original shell was the
Bourne shell, sh. Every Unix platform will either have the Bourne shell, or a
Bourne compatible shell available.
Numerous other shells are available. Some of the more well known of these
may be on your Unix system: the Korn shell, ksh, by David Korn, C shell,
csh, by Bill Joy and the Bourne Again SHell, bash, from the Free Software
Foundations GNU project, both based on sh, the T-C shell, tcsh, and the
extended C shell, cshe, both based on csh.
Even though the shell appears not to be doing anything meaningful when
there is no activity at the terminal, it swings into action the moment you key
in something.
The following activities are typically performed by the shell in its interpretive
cycle:
✓ The shell issues the prompt and waits for you to enter a command.
✓ After a command is entered, the shell scans the command line for
meta characters and expands abbreviations (like the * in rm *) to
recreate a simplified command line.
✓ It then passes on the command line to the kernel for execution.
✓ The shell waits for the command to complete and normally can’t do
any work while the command is running.
✓ After the command execution is complete, the prompt reappears and
the shell returns to its waiting role to start the next cycle. You are free
to enter another command.
vtucode.in Page 6
Wild Card Matches
* Any number of characters including none.
? A single character.
[ijk] A single character – either an i, j or k.
[x-z] A single character that is within the ASCII range of characters x and z.
[!ijk] A single character that is not an i, j or k (Not in C shell).
[!x-z] A single character that is not within the ASCII range of the characters x
and z (Not in C Shell).
{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell).
Examples:
To list all files that begin with chap, use, $ls chap*
To list all files whose filenames are six character long and start with chap,
use , $ls chap??
Note: Both * and ? operate with some restrictions. for example, the * doesn’t
match all files beginning with a . (dot) or the / of a pathname. If you wish to
list all hidden filenames in your directory having at least three characters
after the dot, the dot must be matched explicitly.
$ ls .???*
Similarly, these characters don’t match the / in a pathname. So, you cannot
use, $cd /usr?local to change to /usr/local.
You can frame more restrictive patterns with the character class. The
character class comprises a set of characters enclosed by the rectangular
brackets, [ and ], but it matches a single character in the class. The pattern
[abd] is character class, and it matches a single character – an a,b or d.
Examples:
You can negate a character class to reverse matching criteria. For example:
✓ To match all filenames with a single-character extension but not the .c
ot .o files, use *.[!co]
✓ To match all filenames that don’t begin with an alphabetic character,
use [!a-zA-Z]*
vtucode.in Page 7
Escaping & Quoting:
Escaping is providing a \ (backslash) before the wild-card to remove (escape)
its special meaning.
$cat chap0\[1-3\]
rm My\ Document.doc
Quoting is enclosing the wild-card, or even the entire pattern, within quotes.
Anything within these quotes (barring a few exceptions) are left alone by the
shell and not interpreted. When a command argument is enclosed in quotes,
the meanings of all enclosed special characters are turned off.
Examples:
Standard error: The file (stream) representing error messages that emanate
from the command or shell, connected to the display.
vtucode.in Page 8
✓ Another program using a pipeline.
A file is opened by referring to its pathname, but subsequent read and write
operations identify the file by a unique number called a file descriptor. The
kernel maintains a table of file descriptors for every process running in the
system. The first three slots are generally allocated to the three standard
streams as,
0 – Standard input
1 – Standard output
2 – Standard error
Examples:
Assuming file2 doesn’t exist, the following command redirects the standard
output to file myOutput and the standard error to file myError.
Commands in the fourth category are called filters. Note that filters can also
read directly from files whose names are provided as arguments.
Example:
To perform arithmetic calculations that are specified as expressions in input
file calc.txt and redirect the output to a file result.txt, use
vtucode.in Page 9
Connecting Commands: Pipe
With piping, the output of a command can be used as input (piped) to a
subsequent command.
$ command1 | command2
Output from command1 is piped into input for command2.
Examples:
$ ls -l | wc –l Displays number of file in current directory
$ who | wc –l Displays number of currently logged in users
To discuss all the examples in this chapter we use following emp.lst as the
reference file
$ 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 | director | 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. saxena | g. m. | marketing | 12/03/45 | 8000
0110 | v. k. agrawal | g. m. | marketing | 31/12/40 | 9000
grep scans its input for a pattern displays lines containing the pattern, the
line numbers or filenames where the pattern occurs. The command uses the
following syntax:
vtucode.in Page 10
grep searches for pattern in one or more filename(s), or the standard input if
no filename is specified.
The first argument (except the options) is the pattern and the remaining
arguments are filenames.
Examples:
$ grep “sales” emp.lst
here, grep displays 4 lines containing pattern as “sales” from the file emp.lst.
Here, first column shows file name. when grep is used with multiple
filenames, it displays the filenames along with the output.
grep options:
The below table shows all the options used by grep.
Option Significance
-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 Matches multiple patterns
-f filename Takes patterns from file, one per line
-E Treats patterns as an ERE
-F Matches multiple fixed strings
vtucode.in Page 11
Examples:
✓ Ignoring case (-i):
When you look for a name but are not sure of the case, use the -i (ignore) option.
vtucode.in Page 12
✓ Taking patterns from a file (-f):
You can place all the patterns in a separate file, one pattern per line.
Grep uses -f option to take patterns from a file:
$ cat patterns.lst
director
manager
chairman
Like the shell's wild-cards which matches similar filenames with a single
expression, grep uses an expression of a different type to match a group of
similar patterns. Unlike shell's wild-cards, grep uses following set of meta-
characters to design an expression that matches different patterns.
Symbols/Expression Matches
* Zero or more occurrences of the previous character.
g* Nothing or g, gg, ggg, gggg, etc.
.(Dot) A single character.
.* Nothing or any number of characters.
[pqr] A single character p, q or r
[c1-c2] A single character withing ASCII range shown by c1
and c2
[0-9] A digit between 0 and 9
[^pqr] A single character which is not a p, q or r
[^a-zA-z] A non-alphabetic character
^pat Pattern pat at beginning of line
pat$ Pattern pat at end of line
^bash$ A bash as the only word in line
^$ Lines containing nothing
vtucode.in Page 13
Examples:
✓ The *
The * (asterisk) refers to the immediately preceding character. Here, it
indicates that the previous character can occur many times, or not at
all.
✓ The Dot:
A . matches a single character. The pattern 2... matches a four-
character patten beginning with a 2. The pattern .* matches any
number of characters, or none.
vtucode.in Page 14
$ grep '7...$' emp.lst
9876|jai sharma |director |production |12/03/50|7000
2365|barun sengupta |director |personnel |11/05/47|7800
3564|sudhir Agarwal |executive |personnel |06/07/47|7500
vtucode.in Page 15
Examples:
✓ The + and ?
+ - Matches one or more occurrences of the previous character
? - Matches zero or one occurrence of the previous character.
vtucode.in Page 16
SHELL PROGRAMMING
Introduction:
A shell is a program that acts as the interface between user & the
linux system, allowing user to enter commands for the operating system to
execute. A linux shell is both a command interpreter and a programming
language.
The most commonly used shells are SH(Bourne Shell) CSH(C Shell)
and KSH(Korn Shell). KSH is based on SH & so is BASH(Bourne again shell).
TCSH(Extended C Shell) is based on CSH.
Bash Shell:
Bash is the shell or command language interpreter, for the gnu OS.
The name is an acronym for the “Bourne-Again Shell”.
vtucode.in Page 17
Bourne Shell:
The Bourne shell is one of number of Unix shell. Like the others, it is
both a command language and a programming language. As a command
language it provides a user interface to Unix/Linux. It executes commands
entered by the user or from a file.
C Shell:
The C shell was written by Bill Joy at the University of California at
Berkele. Csh is new a command language interpreter good features of other
shells and a history mechanism similar to the redo of INTERLISP. While
incorporating many features of other shells which make writing shell
programs easier, most of the features unique to chs are designed more for
the interactive UNIX user.
The C shell has three separate files which are used for customizing its
environment. These three files are .chrc, .login, & .logout. because these
files begin with a period (.) they do not usually appear when one types the ls
command. In order to see all files beginning with periods. The -a option is
used with the ls command.
The C shell reads the .login file after it has read the .cshrc file. This
file read once only for login shells. This file should be used to set up
terminal settings, for example, backspace suspend and interrupt characters.
vtucode.in Page 18
The .logout file contains commands that are run when the users logs
out of the system.
Unix Shell:
Ordinary Variables:
Within a shell, a shell parameter is associated with a value that is
accessible to the user. There are several kinds of shell parameters, in the
korn shell, all data are stored as strings.
The .profile:
The .profile file is present in user home ($HOME) directory. It is
customize as user individual working environment. Because the .profile file
is hidden, use the ls -a command to list it.
The .profile file contains your individual profile that overrides the
variables set iin /etc/profile file. The .profile file is often used to set exported
environment variables & terminal modes. You can customize your
environment by modifying the .profile file. Use the .profile file to control the
following defaults.
1. Shells to open.
2. Prompt appearance.
3. Keyboard sound.
This hidden file is read every time you open a shell in unix and you can put
in commands to define the experience you want when you interact with a
shell.
vtucode.in Page 19
The following are the profile files of the commonly used shells:
Shell Profile File
Ksh .profile
Bourne .profile
Bash .bash_profile
Tcsh .login
Csh .login
You can put aliases into your .profile file, which customize commands.
Example:
#!/bin/sh #Sample Shell Script - simple.sh
echo "Enter Your First Name:"
read fname
echo "Enter Your Last Name:"
read lname
echo "Your First Name is: $fname"
echo " Your Last Name is: $lname"
For example, the following script generates an error while trying to change
the value of NAME –
#!/bin/sh
NAME="Ramu"
readonly NAME
NAME="Bantu”
Example:
#!/bin/sh
#Shell Script to demonstrate command line arguments - sample.sh
echo "The Script Name is: $0"
echo "Number of arguments specified is: $#"
echo "The arguments are: $*"
echo "First Argument is: $1"
echo "Second Argument is: $2"
echo "Third Argument is: $3"
echo "Fourth Argument is: $4"
echo "The arguments are: $@"
vtucode.in Page 21
exit & Exit status of command:
C program and shell scripts have a lot in common, and one of them is that
they both use the same command ( or function in c ) to terminate a
program. It has the name exit in the shell and exit( ) in C.
The command is usually run with a numeric arguments:
✓ exit 0 #Used when everything went fine
✓ exit 1 #Used when something went wrong
Examples:
Ex1:
$ grep director emp.lst >/dev/null; echo $?
0 #Success
Ex2:
$ grep director emp.lst >/dev/null; echo $?
vtucode.in Page 22
Using Test And [ ] To Evaluate Expressions
When you use if to evaluate expressions, you need the test statement
because the true or false values returned by expression's can't be directly
handled by if.
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
decision.
Numeric Comparision:
Numerical Comparison operators used by test:
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-ge Greater the or equal to
-lt Less than
-le Less than or equal to
The numerical comparison operators used by test always begins with a -
(hyphen), followed by a two-letter string, and enclosed on either side by
whitespace.
Examples:
$ x=5, y=7, z=7.2
$ test $x -eq $y; echo $?
1 #Not Equal
$ test $y -eq $z
0 #True- 7.2 is equal to 7
vtucode.in Page 23
The if conditional:
The if statement makes two-way decisions depending on the fulfillment of a
certain condition. In the shell, the statement uses the following forms:
Example:
#!/bin/sh
#Shell script to illustrate
if conditional if grep 'director' emp.lst
then
echo “Pattern found in File!”
else
echo “Pattern not-found in File!”
fi
The second form of branching is the case statement. A case differs from an if
block in that if branches based upon the value of one variable.
An if does not have such restrictions because it uses the test function and a
test can be any string of logical expressions as has been shown.
case expression in
pattern1) command1 ;;
pattern2) command2 ;;
pattern3) command3 ;;
…........
esac
the case block takes as its argument a variable. To denote the variable, it
must be surrounded by parenthesis. The case compares the variables value
against against each pattern.
vtucode.in Page 24
The pattern may be any legal regular expression. If variable’s value matches
the pattern, then the shell executes the command block immediately
following the pattern. The command block terminates with a pair of double
semi colons. As soon as it reaches them, the shell continues past the end of
the case block as denoted by the esac.
Example:
#!/bin/sh
#Shell script to illustrate CASE conditional – menu.sh
echo "\t MENU\n 1. List of files\n 2. Today's Date\n 3. Users of System\n
4. Quit\n";
echo "Enter your option: \c";
read choice
case "$choice" in
1) ls -l ;;
2) date ;;
3) who ;;
4) exit ;;
*) echo "Invalid Option!"
esac
While Looping:
A while loop is different from a for loop in that is uses a test condition to
determine whether or not to execute its command block. As long as the
condition returns true when tested, the loops executes the commands.
If it returns false, the script skips the loop and proceeds beyond its
terminate point. Consequently, the command set could conceiveably never
run. This is further illustrated by the loop’s syntax:
vtucode.in Page 25
Syntax:
while condition
do
command1
.
.
.
commandn
done
because the loop starts with the test, if the condition fails, then the program
continues at whatever action immediately follows the loops done statement.
If the condition passes then the script executes the enclosed command
block.
After performing the last command in the block, the loop starts tests the
condition again & determine whether to proceed beyond the loop or fail
through it once more.
Example:
Here is the example that uses while loop to open through the given list
of numbers:
i=1
while [ $i -le 5 ]
do
echo $i
i=`expr $i + 1`
done
vtucode.in Page 26
for loop:
The for loop operates on lists of items. It repeats a set of commands for
every item in a list.
Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
Done
Here var is the name of a variable and word1 to wordN are sequences of
characters separated by spaces (words). Each time the for loop executes, the
value of the variable var is set to the next word in the list of words, word1 to
wordN.
Example:
#!/bin/sh
for var in 1 2 3 4 5
do
echo $var
done
Output:
1
2
3
4
5
Example:
$ set `date` #Output of date command assigned to positional parameters $1,
$2 & so on.
$ echo $*
Thu Oct 22 15:40:13 IST 2020
vtucode.in Page 27
Shift: Shifting arguments left
Example:
$ set `date`
$ echo $*
Sun Jan 13 15:40:13 IST 2013
$ echo $1 $2 $3
Sun Jan 13
$ echo $1 $2 $3
$ shift 2 #Shifts 2 places
$ echo $1 $2 $3
15:40:13 IST 2013
Example:
mailx kumar << MARK
Your program for printing the invoices has been executed on `date`.Check
the print queue The updated file is $flname MARK
The string (MARK) is delimiter. The shell treats every line following the
command and delimited by MARK as input to the command. Kumar at the
other end will see three lines of message text with the date inserted by
command. The word MARK itself doesn’t show up.
vtucode.in Page 28
Example:
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
The script search.sh will run non-interactively and display the lines
containing “director” in the file emp.lst.
When a script is sent any of the signals in signal_list, trap executes the
commands in command_list. The signal list can contain the integer values
or names (without SIG prefix) of one or more signals – the ones used with
the kill command.
Example: To remove all temporary files named after the PID number of the
shell:
trap ‘rm $$* ; echo “Program Interrupted” ; exit’ HUP INT TERM trap is a
signal handler. It first removes all files expanded from $$*, echoes a
message and finally terminates the script when signals SIGHUP (1), SIGINT
(2) or SIGTERM(15) are sent to the shell process running the script.
A script can also be made to ignore the signals by using a null command
list.
vtucode.in Page 29
Example:
trap ‘’ 1 2 15
Programs
#!/bin/sh IFS=“|”
While echo “enter dept code:\c”;
do Read dcode
Set -- `grep “^$dcode”<<limit
01|ISE|22
02|CSE|45
03|ECE|25
04|TCE|58
limit`
Case $# in
echo “dept name :$2 \n emp id:$3\n” *) echo “invalid code”;
continue
esac
done
Output:
$valcode.sh
vtucode.in Page 30