USP_ Module 02 (1)-Notes
USP_ Module 02 (1)-Notes
RV Educational Institutions®
Module 02
File Attributes & Permissions
The ls command with options:
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.
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
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
• 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:
• 000 signifies absence of all permissions for all categories, but still we
can delete a file.
• It is the directory permissions that determine whether a file can be
deleted or not.
• Only owner can change the file permissions. User 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)
• A directory must never be writable by group and others .
• Example:
$mkdir c_progs
$ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
• 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.
This makes all files and subdirectories found in the tree-walk executable by all
users. We can provide multiple directories 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:
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.
The meta characters that are used to construct the generalized pattern for
matching filenames belong to a category called wild-cards. The following table
lists them:
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 .???*
However, if the filename contains a dot anywhere but at the beginning, it need
not be matched explicitly.
Similarly, these characters don’t match the / in a pathname. So, you cannot
use, $cd /usr?local to change to /usr/local.
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]
$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 input: The file (stream) representing input, connected to the keyboard.
Standard error: The file (stream) representing error messages that emanate from the
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
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
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 |
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:
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
Examples:
Ignoring case (-i):
When you look for a name but are not sure of the case, use the -i (ignore)
option.
pattern agarwal.
found.
$ 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
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.
ERE make it possible to match dissimilar patterns with a single expression. grep
uses ERE characters with -E option.
egrep is another alternative to use all the ERE characters without -E option. This
ERE uses some additional characters set shown in below table.
Expression Significance
ch+ Matches one or more occurrences of character ch
ch? Matches zero or one occurrence of character ch
Exp1 | Exp2 Matches exp1 or exp2
GIF | JPEG Matches GIF or JPEG
(x1|x2)x3 Matches x1x3 or x2x3
(hard|soft)ware Matches hardware or software
Examples:
The + and ?
+ - Matches one or more occurrences of the previous character
? - Matches zero or one occurrence of the previous character.
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.
As a command interpreter, the shell provides the user interface to the rich
set of GNU utilities. The programming language features allow these utilities to be
combined. Files containing commands can be created and become commands
themselves. These new commands have the same status as system commands in
directories such as /bin, allowing users or groups to establish custom
environments to automate their commands tasks.
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”.
Bash is based on the Bourne shell, sh, originally written by Stephen Bourne.
Bash is extremely portable and can be built on most UNIX systems because
many of the environment dependent variables are determined at build time. Bash
has been ported as a shell for several non-UNIX platforms like QNX, Minix,
OS/2, windows 95 & Windows NT.
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.
Files containing commands allow users build their own commands thus
tailoring the system to their own needs. Such files are called: shell scripts, shell
programs, or command files. These commands have access to the command line
parameters and have the same status as others Unix Commands.
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 .cshrc files contains commands, variable definitions and aliases used
any time the C shell is run. When one logs in, the C shell starts by reading the
.cshrc file, & sets up any variables and aliases.
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.
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.
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: $@"
The shell offers a variable $? and a command test that evaluates a command's
exit status.
The parameter $? stores the exit status of the last command.
It has the value 0 if the command succeeds and a non-zero value if it fails. This
parameter is set by exit's argument.
Examples:
Ex1:
$ grep director emp.lst >/dev/null; echo $?
0 #Success
Ex2:
$ grep director emp.lst >/dev/null; echo $?
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
0 #True
$ test $y -eq $z
0 #True- 7.2 is equal to 7
The last example proves that numeric comparison is restricted to integers only. The
[ ] is used as shorthand for test.
Hence, above example may be re-written as:
test $x -eq $y or [ $x -eq $y ] #Both are equivalent.
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!” echo
else
“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.
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.
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:
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
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
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
shift transfers the contents of a positional parameters to its immediate lower numbered
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.
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.
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