Beruflich Dokumente
Kultur Dokumente
LAB OBJECTIVE:
Upon successful completion of this Lab the student will be able to--
1. Demonstrate how to use the following Bourne Shell commands: cat, grep, ls, more, ps,
chmod, finger, ftp, etc.
2. Use the following Bourne Shell constructs: test, if then, if then else, if then elif, for, while,
until, and case.
3. Learn tracing mechanisms (for debugging), user variables, Bourne Shell variables,
read-only variables, positional parameters, reading input to a Bourne Shell script, command
substitution, comments, and exporting variables. In addition, test on numeric values, test on file
type, and test on character strings are covered.
4. Copy, move, and delete files and directories
There are 64 systems (HCL) installed in this Lab. Their configurations are
as follows
RAM : 1 GB
Software:
All
systems are configured in DUAL BOOT mode i.e., Students can boot from Windows
XP or Linux as per their lab requirement.
This is very useful for students because they are familiar with different Operating
Systems so that they can execute their programs in different programming
environments.
Oracle 9i client version is installed in all systems. On the server, account for each
student has been created.
This is very useful because students can save their work (scenarios’, pl/sql
programs, data related projects, etc) in their own accounts. Each student work is safe
and secure from other students.
Latest Technologies like DOT NET and J2EE are installed in some systems. Before
submitting their final project, they can start doing mini project from 2nd year onwards.
MASM ( Macro Assembler ) is installed in all the systems
Students can execute their assembly language programs using MASM. MASM is
very useful students because when they execute their programs they can see
contents of Processor Registers and how each instruction is being executed in the
CPU.
Using this software, students can depict UML diagrams of their projects.
Software’s installed: C, C++, JDK1.5, MASM, OFFICE-XP, J2EE and DOT NET,
Rational Rose.
Systems are provided for students in the 1:1 ratio.
Systems are assigned numbers and same system is allotted for students when they do the lab.
There are two ways you can execute your shell scripts. Once you have created a script file:
Method 1:
Pass the file as an argument to the shell that you want to interpret your script.
For example, the script file show has the following lines
date
Step 2: To run the script, pass the filename as an argument to the sh (shell )
$ sh show
Method 2:
When we create a file, by default it is created with read and write permission turned on and
execute permission turned off. A file can be made executable using chmod.
For example, the script file show has the following lines
date
$ show
Step 1: Use an editor, such as vi, ex, or ed to write the program. The name of the file containing
the program should end in .c
#include<stdio.h>
int main()
{
printf(“ Welcome to CITM “);
return 0;
}
$ cc –c hello.c
If the program is okay, have to create execute file
$ cc –o hello hello.o
$. /helo
Welcome to CITM
Step 1: Use an editor, such as vi, ex, or ed to write the program. The name of the file containing
the program should end in .C
#include<iostream.h>
int main()
{
cout<<”\n Welcome to CITM”;
return 0;
}
Step 2: Submit the file to CC (the C++ Compiler) to compile the program
$ CC –c hello.C
If the program is okay, have to create execute file
$ CC –o hello hello.o
$. /helo
Welcome to CITM
Step 1: Use an editor, such as vi, ex, or ed to write the program. The name of the file containing
the class name of a program
$ javac test.java
$ java test
The vi Editor:
To write and edit some programs and scripts, we require editors. UNIX provides vi editor for
BSD system – created by Bill Joy. Bram Moolenaar improved vi editor and called it as vim (vi
improved) on Linux OS.
vi Basics
vi <filename>
In all probability, the file doesn’t exist, and vi presents you a full screen with the filename
shown at the bottom with the qualifier. The cursor is positioned at the top and all remaining lines
of the screen show a ~. They are non-existent lines. The last line is reserved for commands that
you can enter to act on text. This line is also used by the system to display messages. This is
the command mode. This is the mode where you can pass commands to act on text, using most
of the keys of the keyboard. This is the default mode of the editor where every key pressed is
interpreted as a command to run on text. You will have to be in this mode to copy and delete
text
For, text editing, vi uses 24 out of 25 lines that are normally available in the terminal. To
enter text, you must switch to the input mode. First press the key i, and you are in this mode
ready to input text. Subsequent key depressions will then show up on the screen as text input.
After text entry is complete, the cursor is positioned on the last character of the last line.
This is known as current line and the character where the cursor is stationed is the current
cursor position. This mode is used to handle files and perform substitution. After the command
is run, you are back to the default command mode. If a word has been misspelled, use ctrl-w to
erase the entire word.
Now press esc key to revert to command mode. Press it again and you will hear a beep.
A beep in vi indicates that a key has been pressed unnecessarily. Actually, the text entered has
not been saved on disk but exists in some temporary storage called a buffer. To save the
entered text, you must switch to the execute mode (the last line mode). Invoke the execute
mode from the command mode by entering a: which shows up in the last line.
vi provides repeat factor in command and input mode commands. Command mode
command k moves the cursor one line up. 10k moves cursor 10 lines up.
Esc u
ctrl-l
:set showmode
Messages like INSERT MODE, REPLACE MODE, CHANGE MODE, etc will appear in the last
line.
Pressing ‘i’ changes the mode from command to input mode. To append text to the right of the
cursor position, we use a, text. I and A behave same as i and a, but at line extremes
I inserts text at the beginning of line. A appends text at end of line. o opens a new line below the
current line
COMMAND FUNCTION
i inserts text
a appends text
When you edit a file using vi, the original file is not distributed as such, but only a copy of
it that is placed in a buffer. From time to time, you should save your work by writing the buffer
contents to disk to keep the disk file current. When we talk of saving a file, we actually mean
saving this buffer. You may also need to quit vi after or without saving the buffer. Some of the
save and exit commands of the ex mode is:
Command Action
:w <filename> save as
Navigation
A command mode command doesn’t show up on screen but simply performs a function.
k moves cursor up
Word Navigation
Moving by one character is not always enough. You will often need to move faster along a
line. vi understands a word as a navigation unit which can be defined in two ways, depending on
the key pressed. If your cursor is a number of words away from your desired position, you can
use the word-navigation commands to go there directly. There are three basic commands:
Example,
Scrolling
Faster movement can be achieved by scrolling text in the window using the control keys.
The two commands for scrolling a page at a time are
Absolute Movement
The editor displays the total number of lines in the last line
Editing Text
The editing facilitates in vi are very elaborate and invoke the use of operators. They use
operators, such as,
d delete
y yank (copy)
Deleting Text
Moving Text
p and P place text on right and left only when you delete parts of lines. But the same keys get
associated with “below” and “above” when you delete complete lines
Copying Text
Joining Lines
vim (LINUX) lets you undo and redo multiple editing instructions. u behaves differently
here; repeated use of this key progressively undoes your previous actions. You could even have
the original file in front of you. Further 10u reverses your last 10 editing actions. The function of
U remains the same.
You may overshoot the desired mark when you keep u pressed, in which case use ctrl-r
to redo your undone actions. Further, undoing with 10u can be completely reversed with 10ctrl-r.
The undoing limit is set by the execute mode command: set undolevels=n, where n is set to
1000 by default.
The. (dot) command is used for repeating the last instruction in both editing and command
mode commands
For example:
2dd deletes 2 lines from current line and to repeat this operation, type. (dot)
/ search forward
? search backward
/printf
The search begins forward to position the cursor on the first instance of the word
?pattern
n doesn’t necessarily repeat a search in the forward direction. The direction depends on
the search command used. If you used? printf to search in the reverse direction in the first
place, then n also follows the same direction. In that case, N will repeat the search in the
forward direction, and not n.
Command Function
n repeats search in same direction along which previous search was made
N repeats search in direction opposite to that along which previous search was made
We can perform search and replace in execute mode using: s. Its syntax is,
:address/source_pattern/target_pattern/flags
Interactive substitution: sometimes you may like to selectively replace a string. In that case, add
the c parameter as the flag at the end:
:1,$s/director/member/gc
Each line is selected in turn, followed by a sequence of carets in the next line, just below the
pattern that requires substitution. The cursor is positioned at the end of this caret sequence,
waiting for your response.
The ex mode is also used for substitution. Both search and replace operations also use
regular expressions for matching multiple patterns.
The features of vi editor that have been highlighted so far are good enough for a
beginner who should not proceed any further before mastering most of them. There are many
more functions that make vi a very powerful editor. Can you copy three words or even the entire
file using simple keystrokes? Can you copy or move multiple sections of text from one file to
another in a single file switch? How do you compile your C and Java programs without leaving
the editor? vi can do all this.
System Calls:
System calls are special set of functions available in several programming languages. They are
used by programs to communicate directly with an operating system. The operating system
communicates back to the user's program through the return value of the function.
Generally, system calls are slower than normal function calls. The reason is because when you
call a system call, control is relinquished to the operating system to perform the system call. In
addition, depending on the nature of the system call, your program may be blocked by the OS
until the system call has finished, thus making the execution time of your program even longer.
One rule of thumb should always be followed when calling a system call: Always check the
return value. The return value is the only method that the operating system has to communicate
information to your program. Thus, you should always check the return value in the event that
the system call failed.
close
dup
dup2
festal
lseek
lstat
open
read
stat
write
open is a system call that is used to open a new file and obtain its file descriptor.
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
Function Definition
Field Description
const
char The relative or absolute path to the file that is to be opened.
*path
A bitwise 'or' separated list of values that determine the method in which the file is to
int oflags be opened (whether it should be read only, read/write, whether it should be cleared
when opened, etc). See a list of legal values for this field at the end.
mode_t A bitwise 'or' separated list of values that determine the permissions of the file if it is
mode created. See a list of legal values at the end.
Returns the file descriptor for the new file. The file descriptor returned is always the
return
smallest integer greater than zero that is still available. If a negative value is returned,
value
then there was an error opening the file.
Example
#include <unistd.h>
#include <fcntl.h>
int main()
if(filedesc < 0)
return 1;
return 1;
return 0;
Value Meaning
O_RDWR Open the file so that it can be read from and written to.
If the file does not exist, create it. If the O_CREAT option is used, then you must
O_CREAT
include the third parameter.
Combined with the O_CREAT option, it ensures that the caller must create the file.
O_EXCL
If the file already exists, the call will fail.
Value Meaning
#include <unistd.h>
Function Definition
Field Description
int
The file descriptor to be closed.
fieldes
return Retuns a 0 upon success, and a -1 upon failure. It is important to check the return
value value, because some network errors are not returned until the file is closed.
Example
#include <stdlib.h>
#include <fcntl.h>
int main()
if(filedesc < 0)
return 1;
if(close(filedesc) < 0)
#include <unistd.h>
Function Definition
Field Description
The file descriptor of where to write the output. You can either use a file
int fildes descriptor obtained from the open system call, or you can use 0, 1, or 2, to
refer to standard input, standard output, or standard error, respectively.
const
A character array where the read content will be stored.
void *buf
size_t The number of bytes to read before truncating the data. If the data to be read is
nbytes smaller than nbytes, all data is saved in the buffer.
return Returns the number of bytes that were read. If value is negative, then the
value system call returned an error.
Example
#include <unistd.h>
int main()
char data[128];
exit(0);
#include <unistd.h>
Function Definition
Field Description
The file descriptor of where to write the output. You can either use a file descriptor
int fildes obtained from the open system call, or you can use 0, 1, or 2, to refer to standard
input, standard output, or standard error, respectively.
const
A null terminated character string of the content to write.
void *buf
size_t The number of bytes to write. If smaller than the provided buffer, the output is
nbytes truncated.
return Returns the number of bytes that were written. If value is negative, then the system
value call returned an error.
Example
#include <unistd.h>
int main()
return 1;
return 0;
#include <unistd.h>
#include <fcntl.h>
int main()
if(filedesc < 0)
return 1;
return 1;
return 0;
stat is a system call that is used to determine information about a file based on its file path.
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Function Definition
Field Description
const char
The file descriptor of the file that is being inquired.
*path
struct stat A structure where data about the file will be stored. A detailed look at all of the fields
*buf in this structure can be found in the struct stat page.
return
Returns a negative value on failure.
value
Example
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
if(argc != 2)
return 1;
if(stat(argv[1],&fileStat) < 0)
return 1;
printf("---------------------------\n");
printf("\n\n");
return 0;
$ ls -l
total 16
$ ./testProgram testfile.sh
---------------------------
Number of Links: 1
$ ./testProgram testProgram.c
---------------------------
Number of Links: 1
$ ./testProgram /home/stargazer
---------------------------
Number of Links: 61
fstat is a system call that is used to determine information about a file based on its file
descriptor.
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Function Definition
Field Description
int fildes The file descriptor of the file that is being inquired.
struct stat *buf A structure where data about the file will be stored.
Example
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
if(argc != 2)
return 1;
int file=0;
return 1;
if(fstat(file,&fileStat) < 0)
return 1;
printf("---------------------------\n");
printf("\n\n");
return 0;
$ ls -l
total 16
$ ./testProgram testfile.sh
---------------------------
Number of Links: 1
$ ./testProgram testProgram.c
---------------------------
Number of Links: 1
$ ./testProgram /home/stargazer
---------------------------
Number of Links: 61
lstat is a system call that is used to determine information about a file based on its filename.
lstat is exactly the same as the stat system call. The only difference between the two is when
the filename refers to a link. When this is the case, lstat returns information about the link itself,
whereas stat returns information about the actual file.
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Function Definition
Field Description
const char The file path of the file that is being inquired. This can be both relative and
*path absolute.
struct stat *buf A structure where data about the file will be stored.
Example
The code for using lstat is exactly the same as the code for using stat. As mentioned before, the
only difference between the two functions is when the target is a symbolic link.
lseek is a system call that is used to change the location of the read/write pointer of a file
descriptor. The location can be set either in absolute or relative terms.
#include <unistd.h>
#include <sys/types.h>
Function Definition
Field Description
int fildes The file descriptor of the pointer that is going to be moved.
off_t
The offset of the pointer (measured in bytes).
offset
int The method in which the offset is to be interpreted (relative, absolute, etc.). Legal
whence values for this variable are provided at the end.
return Returns the offset of the pointer (in bytes) from the beginning of the file. If the return
value value is -1, then there was an error moving the pointer.
Example
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
int file=0;
return 1;
char buffer[19];
printf("%s\n",buffer);
printf("%s\n",buffer); return 0;
$ cat testfile.txt
This is a test file that will be used to demonstrate the use of lseek.
$ ./testing
Value Meaning
dup is a system call similar to dup2 in that it creates an alias for the provided file descriptor. dup
always uses the smallest available file descriptor. Thus, if we called dup first thing in our
program, then you could write to standard output by using file descriptor 3 (dup uses 3 because
0, 1, and 2 are already taken by default). You can determine the value of the new file descriptor
by saving the return value from dup
#include <unistd.h>
Function Definition
Field Description
int fildes The file descriptor that you are attempting to create an alias for.
dup returns the value of the new file descriptor that it has created (which will always be
return
the smallest available file descriptor). A negative return value means that an error
value
occured.
Example
#include <unistd.h>
int main()
return 1;
return 0;
You can also use dup() to redirect standard output by taking advantage of the fact that it always
uses the smallest available file descriptor:
#include <unistd.h>
int main()
close(1);
/* Now we call dup to create a duplicate file descriptor. Because dup always
uses the smallest available file descriptor and we just closed file descriptor
if(dup(file) != 1) return 1;
/* We don't need our original file descriptor, because now the file descriptor 1
close(file);
return 0;
dup2 (C System Call) :dup2 is a system call similar to dup in that it duplicates one file
descriptor, making them aliases, and then deleting the old file descriptor. This becomes very
useful when attempting to redirect output, as it automatically takes care of closing the old file
descriptor, performing the redirection in one elegant command. For example, if you wanted to
redirect standard output to a file, then you would simply call dup2, providing the open file
descriptor for the file as the first command and 1 (standard output) as the second command.
#include <unistd.h>
Function Definition
Field Description
int fildes2 The file descriptor that will be used to make the copy.
dup2 returns the value of the first parameter (fildes) upon success. A negative return
return value
value means that an error occured.
Example
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
int main()
Why Use Shells? Well, most likely because the are a simple way to string together a bunch of
UNIX commands for execution at any time without the need for prior compilation. Also because
its generally fast to get a script going. Not forgetting the ease with which other scripters can
read the code and understand what is happening. Lastly, they are generally completely portable
across the whole UNIX world, as long as they have been written to a common standard.
The Shell History: The basic shells come in three main language forms. These are (in order of
creation) sh, csh and ksh. Be aware that there are several dialects of these script languages
which tend to make them all slightly platform specific. Where these differences are known to
cause difficulties I have made special notes within the text to highlight this fact. The different
dialects are due, in the main, to the different UNIX flavours in use on some platforms. All script
languages though have at their heart a common core which if used correctly will guarantee
portability.
Bourne Shell:Historically the sh language was the first to be created and goes under the name
of The Bourne Shell. It has a very compact syntax which makes it obtuse for novice users but
very efficient when used by experts. It also contains some powerful constructs built in. On UNIX
systems, most of the scripts used to start and configure the operating system are written in the
Bourne shell. It has been around for so long that is it virtually bug free. I have adopted the
Bourne shell syntax as the defacto standard within this book.
C Shell:Next up was The C Shell (csh), so called because of the similar syntactical structures
to the C language. The UNIX man pages contain almost twice as much information for the C
Shell as the pages for the Bourne shell, leading most users to believe that it is twice as good.
This is a shame because there are several compromises within the C Shell which makes using
the language for serious work difficult (check the list of bugs at the end of the man pages!).
True, there are so many functions available within the C Shell that if one should fail another
could be found. The point is do you really want to spend your time finding all the alternative
ways of doing the same thing just to keep yourself out of trouble. The features that guarantee its
continued use in this arena are aliases, and history lists. There are rumors however; that C
Shell is destined to be phased out, with future UNIX releases only supporting sh and ksh.
Differences between csh and sh syntax will be highlighted where appropriate.
Korne Shell:Lastly we come to The Korne Shell (ksh) made famous by IBM's AIX flavour of
UNIX. The Korne shell can be thought of as a superset of the Bourne shell as it contains the
whole of the Bourne shell world within its own syntax rules. The extensions over and above the
Bourne shell exceed even the level of functionality available within the C Shell (but without any
of the compromises!), making it the obvious language of choice for real scripters. However,
because not all platforms are yet supporting the Korne shell it is not fully portable as a scripting
language at the time of writing. This may change however by the time this book is published.
Korne Shell does contain aliases and history lists aplenty but C Shell users are often put off by
its dissimilar syntax. Persevere, it will pay off eventually. Any sh syntax element will work in the
ksh without change
We often need to search a file for a pattern, either to see the lines containing (or not
containing) it or to have it replaced with something else. This chapter discusses two important
filters that are specially suited for these tasks – grep and sed. grep takes care of all search
requirements we may have. sed goes further and can even manipulate the individual characters
in a line. In fact sed can de several things, some of then quite well.
It scans the file / input for a pattern and displays lines containing the pattern, the line
numbers or filenames where the pattern occurs. It’s a command from a special family in UNIX
for handling search requirements.
will display lines containing sales from the file emp.lst. Patterns with and without quotes is
possible. It’s generally safe to quote the pattern. Quote is mandatory when pattern involves
more than one word. It returns the prompt in case the pattern can’t be located.
When grep is used with multiple filenames, it displays the filenames along with the output.
grep options
grep is one of the most important UNIX commands, and we must know the options that
POSIX requires grep to support. Linux supports all of these options.
all the above three patterns are stored in a separate file pattern.lst
It is tedious to specify each pattern separately with the -e option. grep uses an
expression of a different type to match a group of similar patterns. If an expression uses meta
characters, it is termed a regular expression. Some of the characters used by regular
expression are also meaningful to the shell.
The basic regular expression character subset uses an elaborate meta character set,
overshadowing the shell’s wild-cards, and can perform amazing matches.
. A single character
grep supports basic regular expressions (BRE) by default and extended regular
expressions (ERE) with the –E option. A regular expression allows a group of characters
enclosed within a pair of [ ], in which the match is performed for a single character in the group.
A single pattern has matched two similar strings. The pattern [a-zA-Z0-9] matches a single
alphanumeric character. When we use range, make sure that the character on the left of the
hyphen has a lower ASCII value than the one on the right. Negating a class (^) (caret) can be
used to negate the character class. When the character class begins with this character, all
characters other than the ones grouped in the class are matched.
The *
The asterisk refers to the immediately preceding character. * indicates zero or more
occurrences of the previous character.
Notice that we don’t require to use –e option three times to get the same output!!!!!
The dot
A dot matches a single character. The shell uses ? Character to indicate that.
Most of the regular expression characters are used for matching patterns, but there are
two that can match a pattern at the beginning or end of a line. Anchoring a pattern is often
necessary when it can occur in more than one place in a line, and we are interested in its
occurance only at a particular location.
It is possible that some of these special characters actually exist as part of the text.
Sometimes, we need to escape these characters. For example, when looking for a pattern g*,
we have to use \
If current version of grep doesn’t support ERE, then use egrep but without the –E option.
-E option treats pattern as an ERE.
# ?include +<stdio.h>
sed is a multipurpose tool which combines the work of several filters. sed uses
instructions to act on text. An instruction combines an address for selecting lines, with an
action to be taken on them.
sed supports only the BRE set. Address specifies either one line number to select a single line
or a set of two lines, to select a group of contiguous lines. action specifies print, insert, delete,
substitute the text.
Line Addressing
Just similar to head –n 3 emp.lst. Selects first three lines and quits
p prints selected lines as well as all lines. To suppress this behavior, we use –n whenever we
use p command
sed –n ‘1,2p
7,9p
$p’ emp.lst
There is adequate scope of using the –e and –f options whenever sed is used with
multiple instructions.
Let us consider,
cat instr.fil
1,2p
7,9p
$p
-f option to direct the sed to take its instructions from the file
Context Addressing
We can also specify a comma-separated pair of context addresses to select a group of lines.
Sed –n ‘/sa[kx]s*ena/p
/gupta/p’ emp.lst
/manager/w mlist
Line addressing also is possible. Saves first 500 lines in foo1 and the rest in foo2
Text Editing
sed supports inserting (i), appending (a), changing (c) and deleting (d) commands for the
text.
$ sed ‘1i\
Will add two include lines in the beginning of foo.c file. Sed identifies the line without the \ as the
last line of input. Redirected to $$ temporary file. This technique has to be followed when using
the a and c commands also. To insert a blank line after each line of the file is printed (double
spacing text), we have,
sed ‘a\
’ emp.lst
Selects all lines except those containing director, and saves them in olist
Substitution (s)
Substitution is the most important feature of sed, and this is one job that sed does
exceedingly well.
[address]s/expression1/expression2/flags
Only the first instance of | in a line has been replaced. We need to use the g (global) flag
to replace all the pipes
We can limit the vertical boundaries too by specifying an address (for first three lines only).
Replace the word director with member in the first five lines of emp.lst
sed also uses regular expressions for patterns to be substituted. To replace all occurrence of
agarwal, aggarwal and agrawal with simply Agarwal, we have,
We can also use ^ and $ with the same meaning. To add 2 prefix to all emp-ids,
sed ‘s/<I>/<EM>/g
s/<B>/<STRONG>/g
s/<U>/<EM>/g’ form.html
An instruction processes the output of the previous instruction, as sed is a stream editor and
works on data stream
sed ‘s/<I>/<EM>/g
s/<EM>/<STRONG>/g’ form.html
When a ‘g’ is used at the end of a substitution instruction, the change is performed globally
along the line. Without it, only the left most occurrence is replaced. When there are a group of
instructions to execute, you should place these instructions in a file instead and use sed with the
–f option.
2233|a.k.shukla|g.m|sales|12/12/52|6000
9876|jai sharma|director|production|12/03/50|7000
5678|sumit chakrobarty|dgm|mrking|19/04/43|6000
Consider the below three lines which does the same job
The // representing an empty regular expression is interpreted to mean that the search and
substituted patterns are the same
The interval RE - { }
sed and grep uses IRE that uses an integer to specify the number of characters preceding a
pattern. The IRE uses an escaped pair of curly braces and takes three forms:
The value of m and n can't exceed 255. Let teledir.txt maintains landline and mobile phone
numbers. To select only mobile numbers, use IRE to indicate that a numerical can occur 10
times.
You have to identify the segments of a line that you wish to extract and enclose each segment
with a matched pair of escaped parenthesis. If we need to extract a number, \([0-9]*\). If we
need to extract non alphabetic characters,
\([^a-zA-Z]*\)
Every grouped pattern automatically acquires the numeric label n, where n signifies the nth
group from the left.
To get surname first followed by a , and then the name and rest of the line. sed does not use
compulsorily a / to delimit patterns for substitution. We can use only any character provided it
doesn’t occur in the entire command line. Choosing a different delimiter has allowed us to get
away without escaping the / which actually occurs in the pattern.
ls command is used to obtain a list of all filenames in the current directory. The output in
UNIX lingo is often referred to as the listing. Sometimes we combine this option with other
options for displaying other attributes, or ordering the list in a different sequence. ls look up the
file’s inode to fetch its attributes. It lists seven attributes of all files in the current directory and
they are:
For example,
$ ls –l
total 72
For example,
Directories are easily identified in the listing by the first character of the first column,
which here shows a d. The significance of the attributes of a directory differs a good deal from
an ordinary file. To see the attributes of a directory rather than the files contained in it, use ls –ld
with the directory name. Note that simply using ls –d will not list all subdirectories in the current
directory. Strange though it may seem, ls has no option to list only directories.
File Ownership
When you create a file, you become its owner. 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:
File Permissions
UNIX follows a three-tiered file protection system that determines a file’s access rights. It
is displayed in the following format:
For Example:
The first group has all three permissions. The file is readable, writable and executable by
the owner of the file. The second group has a hyphen in the middle slot, which indicates the
absence of write permission by the group owner of the file. The third group has the write and
execute bits absent. This set of permissions is applicable to others.
You can set different permissions for the three categories of users – owner, group and
others. It’s important that you understand them because a little learning here can be a
dangerous thing. Faulty file permission is a sure recipe for disaster
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:
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other
permissions unchanged. Its syntax is:
a - all (ugo)
Initially,
The command assigns (+) execute (x) permission to the user (u), other permissions remain
unchanged.
chmod +x xstart
Let initially,
Then, it becomes
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:
0 --- no permissions
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.
will assign all permissions to the owner, read and write permissions for the group and only
execute permission to the others.
777 signify all permissions for all categories, but still we can prevent a file from being
deleted. 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 can not change other user’s file’s permissions. But the
system administrator can do anything.
-rw-r--r--
----------
This is simply useless but still the user can delete this file
-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. When you know the shell meta characters well, you will appreciate that the * doesn’t
match filenames beginning with a dot. The dot is generally a safer but note that both commands
change the permissions of directories also.
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)
Example:
mkdir c_progs
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.
Usually, on BSD and AT&T systems, there are two commands meant to change the
ownership of a file or directory. Let kumar be the owner and metal be the group owner. If
sharma copies a file of kumar, then sharma will become its owner and he can manipulate the
attributes
chown
ls -l note
Once ownership of the file has been given away to sharma, the user file permissions that
previously applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note since
there is no write privilege for group and others. He can not get back the ownership either. But he
can copy the file to his own directory, in which case he becomes the owner of the copy.
WEEK 1
SESSION 1:
Login : citm
password:
Here we have to open a file named as my file text when we open the file by using the
above syntax one window will be opened in that we type text as our wish.
Example: vi myfile.text.
modify the file by opening the file again. An error will occur when
Example: vi530
that saves the file and Quits editing mode .then we come out from
the vi editor.
SESSION 2:
When we turn on the system one screen will appear. In this we have to type 192.168.1.61
then we enter into editor. It asks
Login : citm
password:
Syntax: vi filename
To add some text to the file which is already created first we have to open that file then
add text.
vi filename. Text
To change the some text in myfile.text move the cursor where we want to change
the text After that replace the text under cursor with other text first open the file as
follows.
vi myfile.text
To delete text in my file text we first move the cursor to end of that line and
then press delete then the line is erased to do this first we open the file as
vi myfile.text
esc:WQ
It means that saves the file and quits editing mode then we log out from the vi Mode.
In our current directory press exit to logout from the commands in desktop turn off
computer option will appear ok that option and then ok shutdown option then windows will
shutdown we logout from the system.
WEEK 2
Login : citm530
password:
B) Aim: Use the cat command to create a file containing the following
data .Call it mytable use
Cat command is used to create the files Here file name is mytable
Example: cat>mytable
$ cat >mytable
$ cat mytable
D) Aim: Use the vi command to correct any errors in the file ,mytable
Vi mytable
E) Aim: Use the sort command to sort the file mytable according to the first field. Call
the
short command is used to print the contents of the file in alphabetical order
$ cat mytable
$ lp mytable
G) Aim: use the cut and paste commands to swap fields 2 and 3 of mytable.Call it my
table(same name)
By using cut command we cut fields as well as characters from the file cut
command cuts mentioned characters and it is stored in another file as follows
the fields are removed by using the syntax as follows the out put is stored in
table
It means it cuts the characters from 5 to 9 the remaining 1,3 fields are stored
in table .To paste these two files we use paste command
$ cat mytable
$ cat mytable
$ lp mytable
In our current directory type exit to log out from the commands in desktop turnoff
computer option will appear ok that option and then ok shutdown option then windows will
shutdown we logout from the system.
Logout
[ctrl-d] or
Exit
WEEK 3
When we turn on the system one screen will appear in this we have to type
192.168.1.61 then we enter into one window it asks
Login : citm
password:
If we want to known in which shell you login shell the syntax is as follows
$ echo $SHELL
/bin/bash
Unix contains a system variable shell that identifiers the path to your login shell
$ cd /
$ ls
$ cd etc
$ ls
................................
$ cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
....................................................
...................................................
citm:x:501:501:Citmrushi:/home/citm:/bin/bash
D) Aim: use the who command and redirect the result to a file called myfile1.Use
the more command to see the contents of myfile1.
When we redirect the standard output the command output is copy to a file
rather than displayed on the monitor the redirection operator for output is >
Who>myfile1
More myfile1
$ who >myfile1
$ more myfile1
E) Aim: Use the date and who commands in sequence (in one line) such that the output of
date will display on the screen and the output of who will be redirected to a file called
myfile2. use the more command to check the contents of myfile2.
$ more myfile2
......... . ..............................................
$ cat mytable
B) Aim: Write a sed command that deletes the character before the
C) Aim: Write a sed command that swaps the first and second words in a
file.
WEEK 4
Aim: pipe your /etc/passwd file to awk , and print out the home directory of
A)
each user.
First we go to vi editor by using
$ vi home.awk
split(user[1],homedir, ":")
print homedir[1]
Sh filename.sh
awk command :
Administrator
Guest
sys1
IT
IT123
it
IT501
IT502
B) Aim: Develop an interactive grep script that asks for a word and a
file name and then tells how many lines contain that word.
$ vi grep.sh
read pattern
read filename
Output :
$ sh grep.sh
computer
sales.dat
10 computer 2345
10 computer 7685
WEEK 5
A) Aim: write a shell script that takes a command –line argument and
reports on whether it is
$ vi filetype.sh
read file
if [ -f $file ]
then
elif [ -d $file ]
then
else
fi
outputs:
$ sh filetype.sh
sales.dat
$ sh filetype.sh
IT
IT---> It is a DIRCTORY.
B) Aim: Write a shell script that accepts one or more file name as
arguments and converts all of them to uppercase, provided they exist in the
current directory.
$vi upper.sh
for file in *
do
if [ -f $file ]
then
fi
done
Reading input :
$ ls
afile
afile1.tar
afile.tar
aped.sed
errfile
exch.dat
exsed.sed
f1
for.sh
grep.sh
Output:
$ sh upper.sh
AFILE
AFILE.TAR
APED.SED
ERRFILE
EXCH.DAT
EXSED.SED
F1
FOR.SH
GREP.SH
C) Aim: Write a shell script that determines the period for which a
specified user is working On the system.
$vi logtime.sh
read user
last $user
Output :
$ sh logtime.sh
IT123
WEEK 6
A) Aim: write a shell script that accepts a file name starting and ending line
numbers as arguments and displays all the lines between the given line
numbers.
$ vi range.sh
read file
if [ -f $file ]
then
read snum
read enum
then
echo "The selected lines from $snum line to $enum line in $file :"
else
fi
else
fi
Output:
$sh range.sh
sales.dat
11 tvsets 8765
10 computer 7685
12 mouse 6785
B) Aim: write a shell script that deletes all lines containing a specified word in
one or more files supplied as arguments to it.
$ vi detline.sh
read word
for i in $*
do
grep -v $word $i
done
Output :
computer
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
11 tvsets 8765
12 mouse 6785
13 keyboard 2341
WEEK 7
A) Aim: write a shell script that computes the gross salary of a employee according
to the following rules:
i ) If basic salary is <1500 then HRA=10% of the basic and DA=90% of the basic
ii) If the basic salary is>=1500 then HRA=500/- and DA=98% of the basic
$ vi gsalary.sh
read bsal
then
gsal=$((bsal+((bsal/100)*10)+(bsal/100)*90))
fi
then
gsal=$(((bsal+500)+(bsal/100)*98))
fi
Output :
$ sh gsalary.sh
1200
$ sh gsalary.sh
2400
B) Aim: Write a shell script that accepts two integers as its arguments
and computers the value of first number raised to the power of the second number.
$ vi pow.sh
read int1
read int2
pv=$int1
i=1
do
i=`expr $i + 1 `
done
echo "The value of first number to the power of the second number :"
echo "$pv"
output:
$ sh pow.sh
WEEK 8
A) Aim: Write an interactive file –handling shell program. Let it offer the user
the choice of copying removing, renaming, or linking files. Once the user has
made a choice, have the same program ask the user for the necessary
information, such as the
file name ,new name and so on.
$ cat 8a.sh
echo "*******MENU*********"
echo "
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files."
read ch
case "$ch" in
ls –l
read ofile
read nfile
read rfile
read ofile
read nfile
read ofile
read lfile
esac
Output :
$ sh 8a.sh
*******MENU*********
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files.
f1
f0
Copied sucessfully.
B) Aim: Write shell script that takes a login name as command-line argument and reports
when that person logs in.
$vi login.sh
read user
last $user
Output :
$ sh logtime.sh
IT123
(or)
last $*
output :
C) Aim: Write a shell script which receives two file names as arguments. It should check
whether the two file contents are same or not. If they are same then second file should
be deleted.
$vi cheek.sh
read file1
read file2
if [ -e $file1 ]
then
if [ ! –e $file2 ]
then
else
fi
else
fi
Output:
$ sh cheek.sh
file1.dat
file3.dat
$ sh 8c.sh
file1.dat
file2.dat
$ sh 8c.sh
file5.dat
file3.dat
WEEK 9
A) Aim: write a shell script that displays a list of all the files in the current directory
to which the use has read ,write and execute permissions.
$vi perm.sh
for file in *
do
if [ -f $file ]
then
then
ls -l $file
fi
fi
done
Output :
$ sh perm.sh
B) Aim: Develop an interactive script that ask for a word and a file name and then tells
how many times that word occurred in the file.
$ vi wcount.sh
read word
read flname
Output:
$ sh wcount.sh
computers
sales.dat
$ vi strlen.sh
read string
strlen=${#string}
Output :
$ sh strlen.sh
WEEK 10
A) Aim: Write a C program that takes one or more file or directory names
as command line input and reports the following information on the file:
I)file type
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
struct stat b;
int i;
for(i=1;i<argc;i++)
if(lstat(argv[i],&b)==-1)
printf("%s",argv[i]);
fflush(stdout);
perror("lstat error");
continue;
else if(S_ISREG(b.st_mode))
printf("regular file\n");
else if(S_ISDIR(b.st_mode))
else if(S_ISCHR(b.st_mode))
else if(S_ISBLK(b.st_mode))
else if(S_ISFIFO(b.st_mode))
printf("pipefile \n");
else if(S_ISLNK(b.st_mode))
else if(S_ISSOCK(b.st_mode))
else
printf("unknown file");
OUTPUT :
$./a.out lab1.sh
regular file
B) Aim: Write a C program that takes one or more file or directory names as
command line input and reports the following information on the file:
#include<sys/stat.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int i;
if(argc<2)
printf("usage:./a.out< filename(s)");
exit(1);
for(i=1;argc!=i;i++)
if(access(argv[i],F_OK)<0)
continue;
else
if(lstat(argv[i],&statbuf)<0)
continue;
return(0);
OUTPUT :
No of links :1
WEEK 11
a) mv
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
if(argc!=3)
exit(1);
if(access(argv[1],F_OK)<0)
exit(2);
if(rename(argv[1],argv[2])<0)
printf("Rename Error");
exit(3);
return 0;
OUTPUT :
#include<stdio.h>
FILE *fp1,*fp2;
int ch;
fp1=fopen(argv[1],”r”);
fp2=fopen(argv[2],”r”);
while((ch=fgtc(fp1))!=-1)
fputc(ch,fp2);
OUT PUT:
Cc filename c
La out ab abc
WEEK 12
A) Aim: Write a C program that simulates ls command (use system calls/directory API)
#include<stdio.h>
#include<conio.h>
#include<unistd.h>
#include<derent.h>
Int main()
DIR *dirp;
Dirp=opendir(“”);
If(dirp==NULL)
Printf(“error”);
else
for(dp=readdir(dirp);dp!=NULL;dp=readdir(dirp))
printf(“%s\n”,dp->d=name);
Output:
cc filename c
la.output
a.out
ramesh
file1
file2