Key To Unix - Notes
Key To Unix - Notes
Page 1 of 63
1. Introduction to UNIX
1.1. Single-User Systems
The personal computer (PC) is a small, general-purpose system that can execute programs to perform a wide variety of tasks. The PC, however, was designed for use by one person at a time; that is, it is SingleUser oriented with MS-DOS as the de facto standard operating system for this range of machines. Single user systems became very popular due to the low cost hardware and wide range of software available for these machines.
Key to Unix
Page 2 of 63
The system unit may be connected to user terminals located at remote sites or to remote system units through "Communications lines". Thus, a terminal of one multi-user system can communicate with any other multi-user system for sharing a database or sending important information. Just as a PC has serial ports to connect to a serial printer, so also multi-user systems will have special communication ports to link one system with another.
www.wilshiresoft.com
Key to Unix
Page 3 of 63
Linus Torvalds is the father of Linux, the free UNIX that is now making significant inroads into the commercial world. It came into being in 1991 when Torvalds, then a student at the University of Helsinki (Finland), made the entire software available on the Internet. This offering also included the complete source code that encouraged hundreds of programmers to add to it and make changes. Linux is distributed under the GNU General Public License, which makes it mandatory for developers and sellers to make the source code public. The license also lets you buy one copy of Linux and load it on a thousand machines (or more). Linux is particularly strong in networking and Internet features, and is an extremely cost-effective solution in setting up a web site or a local intranet.
www.wilshiresoft.com
Key to Unix
Page 4 of 63
UNIX is a multi-user system designed to support a group of users simultaneously. The system allows for the sharing of process power and peripheral resources while at the same time providing excellent security features. Hierarchical file system UNIX uses a hierarchical structure to store information. This structure has the maximum flexibility in grouping information in a way that reflects its natural state. It allows for easy maintenance and efficient implementation. Unix shell UNIX has a simple user interface called the shell that has the power to provide the services that the user wants. It protects the user from having to know the intricate hardware details. Pipes and filters UNIX has facilities called pipes and filters, which permit the user to create complex programs from simpler programs. Utilities UNIX has over 200 utility programs for various functions. These utilities form a powerful set of tools to cater to user requirements without the necessity of writing new programs. Background Processing UNIX has facility which the user can start a task and then proceed to work on other tasks while the system runs the first task in the background and the second task in the foreground. Background processing helps the user in effective utilization of time. Software Development Tools UNIX offers an excellent variety of tools for software development for all phases, from program editing to maintenance of software. Maturity UNIX is a time-tested operating system. It offers a bug free environment and high level of reliability.
The UNIX SYSTEM supports a wide variety of languages - C, Fortan, Basic, PALCAL, Ada, COBOL, Lisp and Prolog.
Developed AT & T Berkeley Software Distribution of Berkeley University Microsoft for micros DEC for minis
Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 5 of 63
AIX
IBM
The kernel is the heart of the system- the core of the UNIX system is the Kernel- the operating system program. The kernel controls the computers resources, allotting them to different users and to different users, decides process priorities and performs all the tasks, which the user wouldnt like to bother about. It interacts directly with the system hardware. There is only one kernel for any system. Kernel, in traditional parlance, is often called the operating system. SHELL USER
SYSTEM SW UNIX COMMANDS
HARD WARE
DATABASE PACKAGES
COMPILERS
SHELL
The Structure Of The Unix System The shell, on the other hand, is technically a UNIX command. It is the interpreter of user request. Computers dont have inherent capability of translating commands into action. An interpreter is required, and that job in the UNIX system is handled by the shell. It takes the commands from the user, deciphers it, and by exchanging information, and sends that command to the kernel. It is actually interface between user and kernel. It has a programming capability of its own.
Key to Unix
Page 6 of 63
allotted to you by your system administrator. This user account is maintained by the system to check for illegal responses. So, you have to ask your administrator for your account name. NOTE:-The system never indicates what went wrong- the login name or the password. This procedure followed by users to start a UNIX session- the login procedure-has been explained earlier. The login procedure ensures that only authorized users can try to access the system. However, it is easy for anyone to find out a users login name since this is displayed on screen when being entered. Thus the login procedure is not a foolproof system. To overcome this problem, UNIX allows a user an additional measure of security by allowing him to have a password associated with his name. To login using password-protected user names, the user has to enter not only the user name but also the password. UNIX to see if they are authorized entries checks the combination of the two. The $ prompt appears only if the user has entered both the name and password correctly, otherwise he is asked to reenter the user name followed by the password. Passwords are not displayed on the screen while they are being entered. Sample login sequence is show below:
Note that the user name and password are checked only after both have been entered.
$ passwd <RET> Changing password for gopal New password: Re-enter new password: (user enters the desired password) (to double-check the new password)
The user is asked to re-enter the new password for confirmation. If this entry differs form the earlier entry, and then UNIX does not accept the password.
www.wilshiresoft.com
Key to Unix
Page 7 of 63
If the user entries match, then the password is associated with the users name. Subsequent attempts to login will require that the user enter his password
It is desirable to have slightly complex passwords so that they are not easily deciphered. The use of simple passwords has in fact become a major cause for concern in the U. S. A, where there have been many incidents of unauthorized access to a UNIX system simple because users used very simple and obvious passwords such as their own names, or birth dates or age.
However, passwords should not be so complex that they cannot be remembered.
$passwd <RET> Changing password for Gopal Old password: New password: Re-enter new password: $_
The passwd command asks for the old password to ensure that the authorized user (and not just anyone) is trying to change the password.
In case UNIX does not recognize the old password, it displays a sorry message and the $ prompt appears on the screen.
If the old password is entered correctly, then the user is asked to enter the new password and re-enter it to confirm. If the two entries do not match, then the user is given another chance to enter the new password.
<RET>
Key to Unix
Page 8 of 63
Re-enter new password: They do not match, try again. New password: Re-enter new password: $
When changing the password, the new password should differ from the old password by at least 3 positions.
Here, /usr/user1 is the directory in which the user user1 is presently working.
www.wilshiresoft.com
Key to Unix
Page 9 of 63
c. Device files - Most of the system files in UNIX are special files. The device file is special in the sense that any output directed to it will be reflected onto the respective I/O (input/output) device associated with the filename. Special files are typically associated with input-output devices. Such files are found in the standard UNIX directories such as /dev and /etc. Users cannot alter special files.
Root(/)
bin
dev
etc
mnt
tmp
unix
lib
var
bin
kumar
sharma u1001p
www.wilshiresoft.com
Key to Unix
Page 10 of 63
The directories shown directly under root are normally found in very UNIX system, in-addition to others
Root is actually a directory file, and it has a number of sub-directories (or branches) under it. These subdirectories, in turn, have more sub-directories and other files (leaves) under them. For instance, bin and usr are two directories directly under root, while a second bin and kumar are sub-directories under UNIX.
NOTE: -
It is possible for two directories to have the same name. Every file, apart from root, must have a parent, and it should be possible to trace the ultimate parentage of a file to root. It is not possible that the two directories or files have the same name in one level.
From this listing, it is impossible to tell whether asha1 is a directory file or an ordinary file or a special file; whether tempdir is actually a directory file as its name indicates.
UNIX provides a way of doing so by using the l option of ls command this is shown below:
This ls l command shows a long listing for files in the current working directory with:
www.wilshiresoft.com
Key to Unix
Page 11 of 63
The file type as the first character on the extreme left. a hyphen in this position indicates an ordinary file d in this position indicates a directory file Special files will be indicated by either b,c or p in this position. These indicate different categories of special files a total maintained by the system.
FUNCTION: It creates a new directory specified as argument, under the current directory.
Ex:
The new directory dir1 is created under the current directory called user1. Creating a directory does not change the users position in the tree structure.
2.5.Changing Directories
You can move around the file system by using the CD (Change Directory) command.
FUNCTION: The command changes the directory to the directory specified as argument.
Ex:
Here, the cd command changes the directory /user/user1 in to the sub-directory called dir1.
The cd command used without arguments gets the user back to his HOME directory from wherever he is.
www.wilshiresoft.com
Key to Unix
Page 12 of 63
To get back to the parent directory, the special argument . . (dot-dot) is used with cd. The symbol . always means the parent of the current directory .
ls stands for lists . It lists the names of files and sub-directories in the current directory. It has number of options, which can be given as arguments. It also sorts the names of the files alphabetically by default.
-r -R -s -t -d -F
-i
-c
A sample file.
Good bye. $_ Here, the contents of the file file1 are displayed on the screen using cat command. You can concatenate two files using cat command
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 13 of 63
Here, the contents of file1 and file2 are displayed and they are redirected to the file3. File3 contains the contents of file1 and file2. If file3 is already exists it simply over writes the file. Using cat command you can create file
$cat>file4
Here `cat command takes the input from the keyboard and redirects the output to the file4. After entering the text you have to press `ctr+d` to save and come out of that file. NOTE: The [Control-d] OPTION is used to terminate all input not only with cat, but with several other commands as well.
$
Every absolute pathname must trace its origin to root, as discussed earlier in this chapter. The first /indicated root directory, while subsequent ones act as delimiters to sub-directories. This method can be inconvenient and tedious, especially when the absolute pathname is quite long i.e., when you located a number of generations away from root. If you have a long chain of sub-directories, then the use of an absolute pathname is an unsatisfactory solution to the problem of moving a single generation up the hierarchy. UNIX also permits use of a relative pathname, which locates a filename with respect to the current directory. In ls command you discovered two hidden directories--. and.. Indicating the current and parent directory, respectively. Using. . As an argument to cd, you can issue an instruction to move to the parent directory /usr/kumar/dir1/data/text, i.e. the directory /usr/kumar/dir1/data:
www.wilshiresoft.com
Key to Unix
Page 14 of 63
$ pwd /usr/kumar/dir1/data/text $cd .. /usr/kumar/dir1/data $_ This method is compact and more useful when ascending the hierarchy.
The cp command copies the source file to the target file if the target file is already exist in that directory. Ex: cp i file1 file2
Here i, option allows you to copy the files interactively. If file2 exist than it will prompt you to over write the file.
Note: using cp command you cannot copy multiple files to a single file.
Here, file1 is renamed with file2 In UNIX, a file can also be moved to another directory as shown below. Like cp, mv also overwrites the file, if the target file is exist. Ex: $ mv file1 /usr/user1/dir1
>
www.wilshiresoft.com
Key to Unix
Page 15 of 63
Options -I
Significance Interactive rm prompts for confirmation before each file is removed. Forcibly removes files permission on them. that dont have write
-f
-r
UNIX allows users to protect their own files from other users working on the system. It also provides protection for system files. Before an explanation of the method of protecting their own files from other users working on the system. It also provides protection for system files.
Before an explanation of the method of protecting files, the following concepts will be discussed:
The types of files in UNX The concept of File Owner, Group Owner and Other Users. Files access permissions.
2.14. The File Owner, Group Owner and Other Users File Owner
The user who creates a file is said to be the owner of that file. The owner of a file can perform any operation on that file, e.g. copying, deleting, and editing (through some editor) etc.
Group Owner
Consider the following situation: A project team of 5 people of the SMARTEST CONSULTANTS PVT. LTD. is working on a software development project for a private detective agency. The team is headed by an analyst. The other 4 members are all programmers. The team is working on a UNIX system. Each programmer has been given a few programs to develop. The data, provided by the detective agency, is of a highly confidential nature, and so the data file has been created in the analysts HOME directory. One programmer may have to link his program to another programmers program in order to test his program. In this situation, each programmer is the File Owner of his own program files. Each program, however, would also belong to the other programmers, so that they could use the programs for linking. The project team (of 5 users) is said to be the Group Owner for the file.
In UNIX, it is possible to define which user of the system is member of a group. A group of users are also given a name, just as a user is given a name.
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 16 of 63
So, for the project team, the programmer Anita may be the owner of her program files. The group of users in the project team may be called PROJECT-ABC, which becomes the Group Owner of Anitas program files.
Other Users
In the example of the SMARTEST CONSULTANTS PVT. LTD., all users of the system who are not members of the group PROJECT-ABC are referred to as Other Users for all files that belong to that group. Their Other Users are thus those users who do not have any common needs for data or programs with the Group Owner. For e.g. people of the accounts department who use the system to generate reports, or data entry operators entering data for payroll can be referred to as Other Users for the files that belong to the group PROJECT-ABC. To summarize: Associated with each file in UNIX is: a File Owner a Group Owner which consists of users who need to share that file Other users who do not belong to the group.
The permissions of each file are stored as follows: 1. 2. 3. The permissions are given in the first column from the second position The first three characters show the read, write and execute permissions for the file owner the next three are the permissions of the Group owner and the last three are the permissions of the Other users. If a permission is allowed to a user, then r (read), w (write) or x (execute) is displayed otherwise a hyphen appears in the place. Cd ls Cp Into X X X X X cp from X X
www.wilshiresoft.com
Key to Unix
Page 17 of 63
X X
X X X
X X X
No read permission
Does not allow the user : To list the contents Remove the directory using rm r.
No write permission
Does not allow the user : Copy the files to the directory. Remove the files in the directory. Rename files in the directory. Make a sub-directory. Remove a sub-directory from the directory. Move files to and from the directory.
No execute permission
Does not allow the user: Display the contents of directory file from within the directory Change to that directory. Display a file in that directory. Copy a file to or from a directory.
2.16. Changing the FAP for the File Owner, Group Owner and other users:
To control the access permissions for a file for different users, the File Owner can use chmod command.
The following example shows how the file access permissions for the file file1 can be changed. The permissions for this file are shown below:
-rwxrwxrwx 1 vara unix2 20 jan 31 10:40 file1
To change permissions for all the three categories of users, the file owner has to indicate:
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 18 of 63
Which permission is to be changed? Whether the permission is to be given or removed. The file for which the permissions are to be changed.
SYNTAX: $chmod
<permissions>
filename
For example: The file Owner can remove the write permission for all users for the file file1 by the following command: $chmod $_ The FAP for the file file1 will now appear as follows: -r-xr-xr-x 1 vara unix2 20 jan 31 10:40 file1 -w filename <RET>
Note that in place of all Ws, hyphens have appeared. To add back the write permission, the File Owner can give the following command:
$chmod
+w
file1
<RET>
$_
More than one-file access permissions can be changed in one command (instead of using two or more commands).
For example:
$chmod
+wx $_
file1 <RET>
This is done by specifying one of the following before the + or sign in the command.
for changing FAP for File Owner only example : chmod u+r x.c
for changing FAP for Group Owner only Example : chmod g-r x.c
for changing FAP for all Other Users only Example: chmod o+x x.c
www.wilshiresoft.com
Key to Unix
Page 19 of 63
3. Unix Utilities
3.1. The wc Command
The wc filter counts the number of lines, words and characters in the specified file or files. It is capable of accepting input directly from the keyboard. By entering wc without any arguments, it waits for the user to type in the input. By default, wc prints all the three counts lines, words, bytes. It comes with three options, which allow user to obtain the number of lines, words or characters individually or in any desired combination.
Options -c
-w
Ex:
file1
$ wc
file1 4 4 19 file1
In the above example, it displays the number of lines, words and number of characters along with the filename.
Ex:
$ wc
-l 4
file1 file1
www.wilshiresoft.com
Key to Unix
Page 20 of 63
Here, more allows the user to view a file, one screen at a time.
3.4. Pipes
UNIX has a feature by which filters and other commands can be combined in such a way that the standard output of one filter/command can be sent as standard input to other filter/command. The vertical bar (|) is the pipe character.
1)
$ls | wc
Here, the output of ls command becomes the input to wc which promptly counts the number of lines it receives as input and displays this count on the screen.
1)
$ who | sort Here, instead of displaying the output of who to the screen it is piped to sort. Sort sorts whatever it receives as input and displays the sorted output.
3.5. The man command displays pages from the UNIX reference manual
For Example, if the user wants detailed information about the ls command, the command to be given is: $man ls NAME : Command/file name, along with a brief description SYNOPSIS : How to use the command, it contains the optional and non-optional arguments. DESCRIPTION : An explanation of how to use the command, and also an explanation of each option. EXAMPLE : gives example of how to use a command. FILES : files which have to be available for this command to work SEE ALSO : commands similar in purpose DIAGNOSTICS : Explanation of error messages WARNINGS : Things to be careful about when using the command BUGS : Known problems and suggested improvements.
www.wilshiresoft.com
Key to Unix
Page 21 of 63
anil kumar barun gupta Krishna murthy Satish kumar Singhvi chanchal Sunitha chowdary sunitha roy c.s. das
www.wilshiresoft.com
Key to Unix
Page 22 of 63
Displays only the month of year Displays only the month in character Displays only the year
$who
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 23 of 63
30 30 30 30
Sharma tty05
-H option with who command displays the heading for each column The who command, when used with the arguments am I, displays a single line of output only, i.e., the login details pertaining to the user who invoked this command: $ who am I kumar tty01 jan 30 14:09
Wild-Card *
Significance Matches one or more character or a string of more than one character. Matches exactly one character.
[]
When * (asterisk) is appended to the string file, the pattern file* expands in to all files, in which the first four constitute the string file and after this four characters it can be anything or nothing i.e. the above command lists all files in the current directory as shown above.
www.wilshiresoft.com
Key to Unix
Page 24 of 63
Ex: Assume that files, file1.txt, file2.txt, file3.abc already exists and gives the command
$ls file*.* file2.txt file3.abc
file1.txt $_
This displays the files starting with file and containing any sequence of characters or no characters followed by a . (dot) and then followed by any sequence of characters or no characters .
1) $ls ? a $_ The ? matches exactly any one character. Here all the files whose names are of single character are displayed. 2) $ls file? file1 $_ file2 b c
In the second example, it is listing all files with a pattern file and followed by any single character i.e. file1 and file2. In the third example, it is displaying for any file with a. (dot) extension of three characters. In fourth example, it displays the files with pattern file2 and with a . extension of a single character.
www.wilshiresoft.com
Key to Unix
Page 25 of 63
Ex: Assume that the files a, b, c, d, file1, file2, file3 exists in the current directory.
1) $ls [abc] a $_ It displays the files with single characters of either a or b or c. b c
All the above Wild-Cards can be combined with each other. Ex: 1)$ls *.??? It will display files with pattern anything with the . (dot) extension of three characters. 2)$ls [a-z]* It displays all files starting with a to z and anything or nothing afterwards. 3)$ls [!a-c]* It displays all the files which do not start with a or b or c.
3.17. Redirection
Most UNIX commands perform one simple function, which consists of the following steps :
- They take some data as input from some source - They process the data - They produce the results as output and send it to some destination In UNIX, when a user executes a command, the shell runs the command and assigns to the command: - The terminal keyboard as the source of input - The terminal VDU as the destination of output
In UNIX keyboard and VDU are referred by special names - the keyboard is the Standard input file and the VDU is the Standard output file. Consider the cat command. When followed by the file name, this command displays line by line from that file, tills the end of the file. Without the file name it takes its input from the standard input file and writes its output to the standard output file. The cat command waits for the input from the keyboard. When the user presses the <RET> key after entering a line, the cat command does what it is supposed to do - simply displays the entire line on the screen. The user can keep on inputting lines. To indicate the end of input, the user has to press <Ctrl> d Not all commands use the Standard input file for input and the Standard output file for output. Given below are examples of the commands that use one or neither of these files. a. Command that uses neither the Standard input file nor the Standard output file.
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 26 of 63
E.g. cd - does not except input from the keyboard - does not write anything onto the VDU.
b. Command that uses the Standard output file but not the Standard input file.
E.g. pwd - does not expect input from the keyboard - displays its output ( the name of the current working directory) on the VDU.
file descriptor 0 1 2 Ex: $ls>abc Redirects the output of ls command to abc file
$ls -l 2> abc
Redirects the error messages of the command to abc file $ls -l > abc 2> err Redirects the output if any to abc and the error messages of the command to err file.
www.wilshiresoft.com
Key to Unix
Page 27 of 63
4. Simple Filters
4.0. Filter:
A filter is a command or a user program that takes its input from standard input, filters the input and sends output to the standard output. These are file-related commands.
$sort
<RET>
The user enters names, each on one line. When the user indicates end of input by pressing <ctrl>d, sort compares the corresponding characters of each line and displays the lines in proper sequence. The following are the options with sort:
Options -t -n -r -f +n[-m]
Significance Specifies the field separator Sorts in numeric order Sorts in reverse order. Ignores case while sorting. Skips n fields before sorting and then sorts through field m. Sorts in dictionary order.
-o file
www.wilshiresoft.com
Key to Unix
Page 28 of 63
$ sort
-t -
+2 -3
file1
2) The -n option: Since sort arranges its input in ASCII sequence, numbers would also be arranged so. To indicate to sort that the input is to be sorted according to numeric value of the numbers and not in ASCII sequence, the n option must be given in the command.
Ex:
$sort 6 5 8 2 9
-n
<RET>
<ctrl>d 2 5 6 8 9 3) The r option: This changes the output of sort to reverse order. Consider the following example: Ex: $sort -r <RET>
$_
4) The -f option: In case the input is to sort consists of digits and alphabets and other characters, sort arranges data in ASCII sequence. In the ASCII sequence of characters, A-Z has lower values than the corresponding lowercase alphabets.
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 29 of 63
So, sort would place anitha after Anitha since a has higher ASCII value than A. The f option folds all lowercase characters into uppercase characters, i.e. it considers all lowercase characters as uppercase characters without actually changing them. Ex: $sort -f <RET>
Anitha asha rama Rama Prasad <ctrl>d asha Anitha Prasad rama Rama $_
5) The +pos1
-pos2 option:
By default sort assumes that fields are separated by a blank space or a tab character. To sort a file on one field, the position of the field in the file has to be specified. This is done as follows:
i. ii. The number of field separators that have to be ignored to reach that field must be specified as +pos1 where the pos1 the appropriate number. The field separator at which sort is to stop must be specified as pos2 where the pos2 is the appropriate number.
For example, to specify sorting on third field, the user will have to specify +2 and -3 i.e. ignore the first two field separators and stop at the third separator. The sort command would then be as follows: $sort +2 -3 filename
6) The -o option: The output of sort always appears on the VDU (standard output file). In case, this output is to be saved on a disk file, the o option should be used. Consider the following example:
$sort -o file2 file1
Here, the o option indicates that the output is to be sent to the file specified after it. The name of the file to be sorted is entered at the end of the command. o is followed by the name of the output to which the sorted data should be written.
Key to Unix
Page 30 of 63
-d
-c
1)
The -f option:
This option is used to specify the fields to be listed out. Consider the example: Ex: cut -f 1, 2 filename
Here, the name of the file whose fields are to be displayed is specified at the end of the command. The f option is followed by the number of each field to be displayed. The numbers are separated a comma. So, this command displays the first and second fields from the given file.
A range of fields can also be specified with cut as shown in the following examples: cut -f 1,4 filename -cut -f1-4 filename -Displays first and fourth fields in the given file. Displays all the fields up to fourth field.
2)
The -d option:
The cut command assumes that a tab character separates the fields. If some character other than default tab character delimits the fields, cut supports an option -d, which allows to set the delimiter. The field separator must be enclosed in double-quotes.
Ex: Consider the file file1 may have the information for each student stored in the following format:
Name: rollno: total: grade Each piece of information is separated by a colon, hence we require the field delimiter to be recognized as :. The command for listing the name and grade fields would now be: $cut -d : -f 1, 4 filename 3) The -c option: The option is used to cut the specified columns from a file. For example, UNIX $cut -c 2,4 filename As a result, the second column and the fourth column from each line in the given file would be displayed.
We can specify a line count and display say, the first three lines of the file. Use the - symbol, followed by a numeric argument as follows: Ex: $head - 3 filename Here, the first three lines of a file would be displayed.
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 31 of 63
Here, the last three lines from the given file will be displayed.
Ex:
$cat file1 <RET> Akanksha Krishna $cat file2 <RET> 6556688 7612214 $ paste file1 file2 <RET> Akanksha Krishna 6556688 7612214
Key to Unix
Page 32 of 63
2 1 2
4.9. Sending the output to some other file using tee command:
SYNTAX: cat names.lst |sort |tee names1.lst Here, cat command displays the contents of file called names.lst, sort command uses that out put and sorts the file in ascending order, tee command divides that output into two parts. One part will be sent to the file name specified as argument, and second part will be displayed on the screen.
www.wilshiresoft.com
Key to Unix
Page 33 of 63
5. The Vi Editor
Vi is visual editor used to enter and edit text files containing data or documents or programs. It displays the contents of files on the screen and allows the user to add, insert, delete or change parts of the text.
a) Command Mode: In this mode all the keys pressed by the user are interpreted to be editor command mode. In
command mode the keys that are hit are not displayed on the screen.
b) Insert Mode: This mode permits the insertion of new text, editing of existing text or replacement of existing text. c)
Each of these operations can be performed only after changing over from the command mode to insertion mode using appropriate commands. The insertion mode is also known as input-text mode. The ex Mode: This mode permits us to give commands at the command line. The bottom line of the VI editor is called the command line. All commands entered in the ex mode are displayed in the command line.
i,I,a,A,r,R,o,O,s,S
COMMAND MODE
<ESC>
:sh <enter>
INPUT MODE
<control-d>
ex MODE
If the give file does not exist then vi displays a blank screen with a ( ~) tilde character at the beginning of each line as follows:
~ ~ ~ ~ ~ ~ ~ ~
www.wilshiresoft.com
Key to Unix
Page 34 of 63
If the file exists, then its contents are read in to the memory and also displayed on the screen. Assume that the given file is a new file and the user wishes to enter text into his file. In vi to add, change, delete text etc., commands have to be given. These commands are summarized in the following pages.
Command i
The above command allows a user to start entering text. The user has to press i. Key or the a key and then start entering the text. To end insertion or appending of text, the user has to press <ESC> key. The user thus keeps changing between command mode and edit mode. Assume that the entered some text in the file and presses the <ESC> key after entering the second line. This takes him out of edit mode and command mode meaning that he cannot make the changes to lines but can only give another command. The <ESC> key toggles between the command mode and the edit mode.
Command H
www.wilshiresoft.com
Key to Unix
Page 35 of 63
line. ^D ^U ^F ^B Ng 0 (zero) $ Scroll down half screen. Scroll up half screen. Page forward. Page backward. Go to nth line. Beginning of the line. End of the line. Insert and Replace Commands : Command A Action Append after current character.
I O O rR
Insert before current character. Inserts a blank line below and allows insertion. Inserts a blank line above and allows insertion. Replaces current character. Replaces characters with text. Word Movement Commands :
Command W B E
Action Moves to the first character of the next word. Moves to the first character of the previous word. Moves to the end of the current word. Deletion and Modification commands :
Command Dw Dd Cw Cc xX
Action Delete current word. Delete a line. Change word. Change line. Delete character at cursor position. Delete character position. Join lines. Undo last change. Restore last change. before cursor
J uU
www.wilshiresoft.com
Key to Unix
Page 36 of 63
. (dot)
Command Yy
Action Yank or anchor on current line (which is to be copied) Yank n number of lines.
Nyy
Fx /pattern
?pattern
containing
Key to Unix
Page 37 of 63
to Scroll the window forward, use <control-d> and to scroll backward, use <control-u>
Paging functions
Command Function
5.8. Customizing vi
VI can be tailored to behave in a way desirable to the user. There are a number of commands available in the ex mode which make it suitable for writing programs as well as preparing documents. You make like to set a pleasant and comfortable environment for yourself so that the editor behaves and responds to your directives exactly in the way you would like it to. There is also the facility to store all the user-set options in a separate file, which is used by the editor to take its startup instructions from. This is the file .exrc containing all user-defined settings conceptually similar to the .profile which is maintained by each user.
Ic
www.wilshiresoft.com
Key to Unix
Page 38 of 63
6. Advanced Filters
6.1. The grep Filter
Grep stands for Global regular expression print. The grep filter is used to search for a particular pattern of characters in a file and displays all the records that contain that pattern. grep cannot be used without specifying a regular expression. The format of the grep filter is: Syntax: grep [options] <pattern > Ex: $grep picture file1 This would search for the pattern picture in the given file file1 and if found, the lines containing would be displayed on the screen. Note that regular expression is always given in double-quotes. We can use grep to search a pattern in several files. For example, Ex: $grep picture file1 file2 Here, the word picture would be searched in the files, file1 and file2 and if found, the lines containing it would be displayed along with the name of the file where it occurred. <filename(s)>
Significance
only the number of without quoting the
-n
Displays line number of matched text, including the text. Ignores case while searching. Displays lines that do not match the text Displays only filenames containing a match, without quoting the text. Searches multiple patterns in a file. This is equal to egrep command
-i. -v
-l
-E
The options must be specified before the regular expressions. Options can also be combined (ex -n and v can be used together as nv). Note that regular expressions are always given in double-quotes. While forming the patterns to be searched, we can use shell met meta-characters also. Character [] Use To specify a pattern this consists of any one of a set of characters. Ex: [abc] specifies the pattern either a or b or c. So, the command : $grep new[abc]
www.wilshiresoft.com
Key to Unix
Page 39 of 63
specifies the search patterns as newa or newb or newc. [ ] with hyphen To specify a pattern which consists of any one of a range of characters . Ex: [a-c] specifies the pattern either a or b or c. So, the command $grep new[a-c] Also specifies the searches for either a or b or c. Ex: [a-zA-Z] specifies a pattern of any one alphabet ,either uppercase or lowercase.
^character
To specify that the pattern following must occur at the beginning of each line. Ex: ^[abc] specifies the pattern either a or b or c at the beginning of each line.
^[ ]
To specify that the pattern must not contain any character in the set/range specified within [ ] Ex: [^abc] specifies a pattern which does not contain either a or b or c. So, the command ; $grep new[^abc] Specifies the search patterns as any pattern containing the word new followed by any character but a or b or c.
Sed by default prints all lines in addition to lines affected by the action. So, the addressed lines are printed twice. Use of, -n option disables this default printing sed -n 'p' myfile sed -n '10p' myfile #print 10th line from myfile
sed -n '5,$p' Mayfield #print all lines from 5th to the last line sed '/abc/p' Mayfield #print the line containing expression 'abc' sed -n '3,$!p' Mayfield #Lines from 3 to end are not displayed when !(negation) is used
Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
www.wilshiresoft.com
Key to Unix
Page 40 of 63
Delete(d): d- Instead of p in above examples would delete the lines. sed '1,/^$/d' Mayfield Above example may be used to delete a mail header from an Internet mail message that user have saved in a file Substitute(s): Syntax: sed '[address]s/pattern/replacement/[flags]' file Flags : :n : replacement should be made for the nth occurrence of the pattern. g: replaces all occurrences of a pattern in each line Example: $cat Mayfield
. .
col1<tab>col2<tab>col3<tab>col4 . . . . . . #replaces first tab in each line with '~' #replaces all tabs of each line with '~' #replaces second tab with '~' #replaces the beginning of all non-blank lines with tab
sed 's/<tab>/~/' Mayfield sed 's/<tab>/~/g' Mayfield sed 's/<tab>/~/2' Mayfield sed '/^$/!s/^/<tab>/' Mayfield
Read command can be useful for inserting the contents of one file at a particular place in another file. For instance, let's say there are set of files that should close with same statement(s). A sed script would allow the user to maintain the closing separately and append it to all files later.
www.wilshiresoft.com
Key to Unix
Page 41 of 63
NOTE: awk assumes by default space as the delimiter. If you have any delimiter other than space you have to specify the delimiter using F option. Examples: awk '{print $1,$2}' Mayfield awk '{print NR,$1,$2}' Mayfield #print record numbers in the output awk '$1~/vara/{print}' Mayfield ~: match operator for regular expressions. It searches for expression vara in the first column awk 'NR==5{print}' Mayfield #displays all columns from 5th record
statements of awk can be divided into 2 sections BEGIN and END. BEGIN: contains statements that get executed before processing the lines Syntax: awk BEGIN{statements}{statements}END{statements} filename awk 'BEGIN{FS=":";OFS="\t";print "headings"}{print}' Illustration of END section: awk '/^$/{print x++}' #Gives the running count of blank lines
Key to Unix
Page 42 of 63
awk program to find the total number of bytes occupied by all files of current directory awk ' BEGIN{print "BYTES\tFILE"} { sum += $5 ++files print $5"\t"$9 } END{print "Total : ",sum," bytes" "Files : ",files}' Option -f Description It allows another filename. i.e. the file contains the procedure for the awk command It allows you to specify delimiter If you have any delimiter other than space
-F
2) The -name option: This option lists out specific named files in all directories beginning from the named directory.
Ex: $ find. name aaa print Here, it searches for the file aaa from current directory downward and prints it. Note that unless we use print option the file gets located but does not get printed.
Ex: $find . name [ab]* print Here, it finds all files which begin with a or b from current directory downwards and print them. 3) The -mtime option: This option displays the names of all files that have been modified within a specific number of days, before the current date. -mtime is followed by number as follows: -mtime n - Specifies that the files have been modified in exactly n days
Key to Unix
Page 43 of 63
Searches in current directory downwards all files, which are modified exactly 2 days back. So, if the current date is 29/01/2001, then this would indicate that the files have been modified on 27/01/2001. -mtime +n - Specifies that the files have been modified more than n days ago. Ex: $find. mtime +2 -print Searches in current directory downwards all files, which have not been modified since last 2 days i.e. which have been modified more than 2 days ago. So, if the current date is 29/01/2001, then this would display the files that have been modified before 27/01/2001. -mtime -n - Specifies that the files have been modified in less than n days. Ex: $find. mtime -2 print Searches in current directory downwards all files, which have been modified from the past 2 days. So, if the current date is 29/01/2001, then this would display the files that have been modified between 26/01/2001 and 28/01/2001.
4)
This option lists out the names of the files and directories in the named directory and its sub-directories, which have the number of links specified. Ex: $find . links 2 -print
Here, it lists all the files and directories from the current directory downwards which have exactly 2 links.
5) The -size option: This option lists out all the files and directories from the current directory downwards which are of mentioned size. Ex: $find . size 10 print Searches in current directory downwards all files whose size is 10 blocks. Ex: $find . size 10c -print
6) The -perm option : This option lists out all the files and the directories permissions. Ex: $find . perm 777 print Searches in the current directory downwards all files and directories with permissions 777. 7) The -inum option: This option lists the file or a directory existing with the specified inode number. If the inode number of one file is known then all the hard links to that file can be identified using the following command. Ex: $find . inum 1234 -print Searches in the current directory downwards for a file with an inode number 1234 . from the current directory downwards with specified
8) The -exec option : The -exec option allows a user to perform file operations on the files that are located by the find command. This option is specified and the end of the find command. The operation is always specified after exec. Ex: $find . name abc -print -exec rm {} \; Here, the command rm is to be executed on each file called abc that is located by the find command . The {} must be specified by the exec command . The find command replaces the {} with the pathname of the files located by it and the action rm is executed on each file.
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 44 of 63
The back-slash followed by a semi-colon is required after the exec option. We execute any command using exec option in find command.
9) The ok option : The exec option always execute the command on each file located by find. Another option, -ok, also allows a user to operate on the files located. But for each file it asks for confirmation (yes/no) from the user before executing the command. Ex: $find . name abc -ok rm {} \; These options, if used, must be specified immediately after the pathname in the find command and before the -print option. More than one of these options also can be used in the same command in any order.
www.wilshiresoft.com
Key to Unix
Page 45 of 63
7. Process
7.1.Ps command
The ps command is used to display attributes of a process. It is one of the few command of the UNIX system, that has knowledge of the kernel built into it. It reads through the kernel data structures or process tables to fetch the process characteristics. Ps is also a highly variant command, the actual output depending on the version of UNIX
$ps PID 476 659 684 TTY tty03 tty03 tty03 TIME 00:00:01 00:00:01 00:00:00 CMD login sh ps
$ sort 278 $ vi
filename2
Here, the user starts sorting the file with command sort and then immediately begins editing new file, without waiting for the completion of the previous process. The shell initiates the sorting process, the kernel assigns a process number (278) and then it returns control of the screen to the user. When performing background processing, the user has to be careful that the background process and the foreground process do not interfere with each other. Any screen output from the background process could not disturb the screen in the middle of the users work with the foreground process. In the above example, redirecting the standard output and standard error of the sort command can solve this problem. Ex: $ sort file1 >file2 2>temp & <RET> 279
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 46 of 63
$ vi newfile
www.wilshiresoft.com
Key to Unix
Page 47 of 63
8. Communications
In
Multi-user system, it is often necessary for one user to know what the other is doing. When there are a hundred odd users sharing the system resources, some of them are invariably located quite a distance apart. Communication through the system seems quite natural and necessary the system administrator also requires to send messages to some, and sometimes to all of them. Since UNIX was invented at Bell Laboratories, its not surprising that the electronic mail facilities are simple and quite elaborate. Its rich communication features can be exploited by the user, both to communicate with others, as well as with himself. These features are so useful that for some people UNIX means electronic mail.
$ write u1003p have you completed your program ? I have completed mine- kumar o
Write is used interactively in this manner, both users need to invoke the command individually. This establishes two write channels, one for the receiver, and the other for the sender. However, if there is need for an extended communication, then one user can simply wait after he has keyed in this message, without pressing [control-d]. write is so designed that it enables the user to send and receive messages at the same time, without his having to quit to the shell. NOTE: For two users to communicate with each other, both must invoke the write command. if two users have the same login name, then the terminal name has to be used along with the login name.
Key to Unix
Page 48 of 63
when there is no urgency. Even though it has a host of options and internal commands, few of them are really complex. Like write, mail uses standard input, and a primitive way of using this program is to take the input from the keyboard: $ mail <user id> subject : new system
the new system will start functioning from next month. [Control d] EOT #to indicate the end of text $_ mail is essentially a single-channel communication command, and the sender doesnt use it for conversation. The message doesnt directly appear on the receivers terminal either. If the receiver is running a program, then mail waits for program execution to finish before flashing the following message. New mails are stored in /var/spool/mail The advantage of mail is that you neednt handle the moment you are informed of it, and can reply to it whenever you want (if at all). However, it is good practice to see incoming mail immediately upon arrival, rather than defer it for future viewing. To do that, received user also has to invoke the mail command (but without any argument) to see the mail, that has sent by his friend. Note:- You can send mail to a user even if he is not logged onto the machine.
www.wilshiresoft.com
Key to Unix
Page 49 of 63
9. Introduction to Shell
The shell is an intermediary program, which interprets the commands that are typed at the terminal, and translates them into commands that the kernel understands.
Key to Unix
Page 50 of 63
for each x (1 2 3 4 ) #for each is used instead of 'for' in bourne(sh) echo "The value of x is $x" end In C shell exit status of previous command is stored in variable status ($? in bourne). ALIASES Alias l ls -l #No = symbol
www.wilshiresoft.com
Key to Unix
Page 51 of 63
The SHELL variable : Shell variable determines the type of shell that a user sees on logging in. Today, there are a host of shells that accompany any UNIX system, and you can select the one you like the most. The C shell is known by the program csh, and the Korn shell by ksh. In linux default shell is bash. The TERM variable: TERM indicates the terminal type being used. There are some utilities which are terminaldependent, and they require to know the type of terminal you are using. One of them is the Vi editor, which makes use of a control file in a sub-directory under the directory /usr/lib/terminfo. If TERM is not set correctly, Vi wont work, and the display will be faulty.
Key to Unix
Page 52 of 63
grep: cant open emp3.lst 2 grep is one command which returns these values; 1 if the pattern cant be found, and 2 if the file cannt be opened
www.wilshiresoft.com
Key to Unix
Page 53 of 63
Test -f filename Test d filename Test r filename Test w filename Test x filename Test s filename Operator -eq -ne -gt -ge -lt -le
True if file exists and is an ordinary file True if file exists and is a directory True if file exists and is a readable True if file exists and is writeable True if file exists and is executable True if file exists and is not empty Meaning Equal to Not equal to Greater than Greater than or equal Less than Less than or equal to
When you utilize if to evaluate expressions, the test statement is invariably used as its control command. On most UNIX system, it is an internal feature of the shell, test evaluates the condition place on its right, and returns either a true or false exist status. This return value is used by if for taking decisions. #Illustrating test option if test -f abc then echo "File is an ordinary file" else echo "File is not an ordinary file" fi Arithmetic tests and string tests #finding the percentages and grades of the students
echo enter the maths marks read maths echo enter the science marks read science echo enter the social marks read social total=`expr $maths + $science + $social` echo the total of the three subjects is $total percentage=`expr $total / 3` echo the percentage of the subjects is $percentage if test $maths -lt 35 -o $science -lt 35 -o $social -lt 35 then echo student is failed else
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 54 of 63
if test $percentage -lt 35 then echo sorry the candidate is failed else if test $percentage -ge 35 -a $percentage -lt 60 then echo congratulations u got "c" grade else if test $percentage -ge 60 -a $percentage -lt 80 then echo congratulations u got "B" grade else echo congratulations u got "A" grade fi fi fi fi
# SCRIPT TO SEARCH FOR A STRING IN A FILE USING STRING OPERATORS $cat emp.sh echo enter the string to be searched : read pname if [ -z $pname ] then echo you have not entered the string exit 1 fi echo enter the file name to be used: read flname if [ ! n $fname ] then echo you have not entered the file name exit 2 else echo seach for $pname from $fname?: read answer if [ $answer = y ] then grep $fname $flname || echo pattern not found else
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 55 of 63
exit 3 fi fi $
case..esac construct evaluates the value of the variable and compares it with each value specified(value1,value2,...) When the value of the variable matches one of the values specified, the set of commands specified under the value is/are executed. The last command to be executed for any value of the variable must be followed by a pair of semicolons. For example using this construct. You can devise a script, which accepts values from 1 to 5, and performs some action depending on the number keyed in. the code for this menu is stored in the file menu.sh :
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 56 of 63
$ cat menu.sh echo menu 1. list of files 2. process of user 3. todays date 4. users of system 5. quit to system echo Enter your option :\c read choice case $choice in 1) 2) 3) 4) 5) esac The five menu choices are displayed with a multi-line echo statement. The meanings attached to these choices are obvious enough. The user response from the keyboard is accepted to the variable choice. The case statement then matches the value of this variable for the strings 1,2,3,4, and 5, as shown in the five lines below the key word case. It then relates each value to a command that has to be executed. Note : The last case option need not have the ;; though you can provide them if you want. #Script to check if a given character is Vowel or not echo "Enter a character" read a case $a in [aA])echo $a is Vowel;; [eE])echo $a is Vowel;; [iI])echo $a is Vowel;; [oO])echo $a is Vowel;; [uU])echo $a is Vowel;; [0-9]*)echo $a is a number;; *)echo $a is consonant;; esac * is interpreted as any value other than those listed earlier with case ls l ;; ps f ;; date ;; who ;; exit
9.15. for..do..done
The for loop is different in structure from the ones used in other programming languages. There is no next statement here, neither can a step be specified. Unlike while and until, it doesnt test a condition but uses as list instead. The syntax is as follows: syntax : for variable name in list of values do command command : : done Example
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 57 of 63
for x in 1 2 3 4 do echo $x done #Demonstrating a for loop #To rename all .c files to .cpp for fname in *.c do nname=`echo $fname | sed 's/.c$/.cpp/'` mv $fname $nname done
9.16. while..do..done
The pattern scanning scripts developed so far still suffer from serious defects. You have no way of giving the user another chance when he has made a faulty response. And you are able to scan only one pattern at a time. Loops let you perform a set of instructions repeatedly. The shell features three types of loops while, until and for. The first two are basically complementary to each other. All of them repeat the instruction set enclosed by certain keywords as often as the control command permits. The while statement should be quite familiar to most programmers. It repeatedly performs a set of instructions till the control command returns a true exist status. The general syntax of this command is as follows: while condition do command command : : done # sample script using while loop $ cat menu.sh while : do echo menu 6. list of files 7. process of user 8. todays date 9. users of system 10. quit to system echo Enter your option :\c read choice case $choice in 6) 7) 8) 9) 10) ls l ;; ps f ;; date ;; who ;; exit
esac echo do you want to continue read chc if [ $chc = y o Y ] then continue else break done
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 58 of 63
#Script to display "Hello" 10 times i=1 while [ $i -le 10 ] do echo Hello i=`expr $i + 1` done #Script demonstrating while true loop while true do clear cat<<MENU 1.list of files 2.Current directory 3.Users 4.exit MENU
echo -e "Enter Your choice: \c" read choice case $choice in 1)ls;; 2)pwd;; 3)who;; 4)break;; *)echo "Invalid Choice" esac read done
#contains number of arguments on the command line #contains arguments of the command line
#SS1 #Script illustrating use of positional parameters echo echo echo echo echo "The name of the program is $0" "The first argument is $1" "The second argument is $2" "The number of arguments are $#" "They are $*"
Key to Unix
Page 59 of 63
A sample run : $SS one two three <RET> The name of the program is SS The first argument is one The second argument is two The number of arguments are 3 They are one two three $_
www.wilshiresoft.com
Key to Unix
Page 60 of 63
APPENDIX A
Block commands in vi
Command :nd :m,nd :n mo p :m,n mo p :m,n w filename :m,n w >> filename :r filename :r! Filename :m co p :m,n co p Function Deletes nth line Deletes lines from m to n Moves line n after line p Moves lines m to n after line p Writes lines m to n to a file Appends lines m to n to a file Reads the contents of the file filename at current cursor position Executes shell command and output of the command is read at the current position. Copies line m after line p Copies lines m to n after line p
An abbreviation is defined for longform Lists currently defined abbreviations Unabbreviates the abbreviation
Set Commands :
:se nu :se ic :set showmode :set aw Set display of line numbers on Ignore case while searching a pattern Display mode in which we are working Automatically write buffer contents to disk before switching to next file during multiple file editing
Map command:
The map command lets you assign the undefined keys so that when a key is pressed it expands in to a command sequence. The command :map is followed by the key, which needs mapping, and the key sequence, which is mapped. For example we can use one of the function keys say f2 and map it to the function.
www.wilshiresoft.com
Key to Unix
Page 61 of 63
APPENDIX B
Logical operators && and || Above code in italics can simply be written as who|grep "^$1"||echo "$1 not logged in". Command after '||' get executed only if the first one fails. Command after '&&' get executed only if the first one succeeds. These operators have a limitation that they are used for making simple decisions. Handling command line options using getopts getopts reads the options given on the command line and decides whether they are valid or not. getopts accepts single character options from the command line provided the option is preceded by a minus (-) sign. On encountering an option getopts stores it in the variable at the end of getopts command. On encountering an illegal option getopts stores a '?' in the variable at the end of getopts. The ':' tells getopts that an argument is expected along with an option. The word immediately following the option on the command line is read as argument to that option. getopts stores argument to an option in the variable OPTARG. #Script illustrating getopts while getopts abcd choice do case $choice in a)echo "you entered a";; b)echo "you entered b";; c)echo "you entered c";; d)echo "you entered d";; ?)echo "Wrong choice" esac done #Script illustrating arguments for an option while getopts a:c:d choice do case $choice in a) echo "Argument for a is $OPTARG" c) echo "Argument for c is $OPTARG" d) echo "Argument for d is $OPTARG" esac done Shell Functions: Syntax: funame() { . . }
www.wilshiresoft.com
Key to Unix
Page 62 of 63
Shell Scripts and Shell Functions : A shell script is stored in a file. A shell function is stored in the shell's local environment. The current shell executes a function where as the shell script is run in a child process. Thus any changes made to the environment by a function are made to the current shell's environment. A shell script's changes are made to the environment of the child process and have no effect on the current shell's environment. A function only exists in the shell where it is defined. They cannot be exported from one process to another. Functions like shell script's can access the command line arguments via the shell positional parameters. Functions can be exported in Linux to subshells (Syntax : export -f <fname> ). eval: It evaluates the command line to complete any shell substitutions and then executes the command. (a) Consider the following : y=x z=y echo $$z Above echo displays 1324z instead of 'x'. Since $$ gives PID of current shell. This PID gets printed followed by 'z'. If we wish to get 'x' as output we need to give the following command $eval echo \$$z eval is used when a single pass of shell substitution does not complete all the needed expansion. '\' is necessary, when the statement is scanned '\' ensures that the first $ is preserved such that it can later be used to extract the value of the variable y. (b) Consider the following: output="myfile>yourfile" cat $output If we execute this script it reports that file > does not exist. This is because I/O redirection and piping is done before variable substitution. So once the variable has been substituted the shell cannot do redirection and thus attempts to cat three files Mayfield, > and yourfile. Hence eval cat $output (c) Consider the following: cat<ch*17 I/O redirection is done before filename substitution. Hence before the shell can replace ch*17 with matching files, it would attempt to redirect the standard input to ch*17. And if such file does not exist an error results. Hence eval cat \< ch*17 Commands built into the Shell If a separate process executes the cd, it will change the directory of that new process and have no effect on the current shell. For cd to work, it has to be built into the shell, so that a new process isn't required to execute it. Such commands are called built-in shell commands. The echo and test commands are examples. Unix process creation consists of 2 steps. First, a copy of current process is forked. Then the new process overwrites the command portion of the process by executing a command. When the shell executes a command, it forks and then a new process execs the command. You can exec commands directly from the current process, using exec. If you exec a command from your login shell, you will be logged out when the execed command is over. $exec date
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
Key to Unix
Page 63 of 63
Fri Dec 29 09:45:18 PST 1989 Login: When date was executed, the current process's command area was overwritten with date's executable code. After date prints the current date and time, it terminates, ending your login session. exec also enables you to change the current process's standard streams. For example exec Mayfield modifies the current process so that it takes input from Mayfield #Script demonstrating exec echo Enter filename read fname terminal='tty' exec< $fname #exec ensures that input for 'read' comes from a file and not from the keyboard while read line do lines=`expr $lines + 1` set -- $line #set assigns the values to positional parameters words=`expr $words + $#` done echo "Number of lines = $lines" echo "Number of Words = $words" exec< $terminal
www.wilshiresoft.com
INDEX
1. Introduction to UNIX................................................................................................................................1
1.1. Single-User Systems...........................................................................................................................................1 1.2. Multi-user Systems .............................................................................................................................................1 1.3. Hardware - Multi-user Systems...........................................................................................................................1 1.4. Classes of Multi-User Systems. ..........................................................................................................................2 1.5. Multi-User Operating System - UNIX ..................................................................................................................2 1.6. STALLMAN AND TORVALDS: GNU AND LINUX ..............................................................................................2 1.7. History of UNIX ...................................................................................................................................................3 1.8. Features and Benefits of UNIX ...........................................................................................................................3 1.9. Versions of UNIX ................................................................................................................................................4 1.10. THE KERNEL AND SHELL...............................................................................................................................5 1.11. Security for the Unix User .................................................................................................................................5 1.12. Creating a Password.........................................................................................................................................6 1.13. Rules for Passwords .........................................................................................................................................7 1.14. Changing the Password. ...................................................................................................................................7 1.15. Check your current Directory the pwd command ...........................................................................................8
3.9. The banner command.......................................................................................................................................22 3.10. The calendar command ..................................................................................................................................22 3.11. Displaying the SYSTEM DATE with Date Command......................................................................................22 3. 12. The who command.........................................................................................................................................22 3.13. Wild-Card Matching.........................................................................................................................................23 3.14. The * Wild-Card: .............................................................................................................................................23 3.15. The ? Wild-Card:.............................................................................................................................................24 3.16. The [ ] Wild-Card.............................................................................................................................................24 3.17. Redirection......................................................................................................................................................25 3.18. Standard Error ................................................................................................................................................26 3.19. The two special files- /dev/null and /dev/tty.....................................................................................................26
5. The Vi Editor...........................................................................................................................................33
The three modes of vi ..............................................................................................................................................33 5.2. Getting started with vi:.......................................................................................................................................33 5.3. Cursor Movement Commands of vi:..................................................................................................................34 5.4. Commands to Copy Lines:................................................................................................................................36 5.5. Saving and Exiting Commands: ........................................................................................................................36 5.6. Pattern Finding Commands: .............................................................................................................................36 5.7. Paging and scrolling:.........................................................................................................................................36 5.8. Customizing vi...................................................................................................................................................37 5.9. The set command .............................................................................................................................................37 5.10. Options used by vi ..........................................................................................................................................37
6. Advanced Filters....................................................................................................................................38
6.1. The grep Filter...................................................................................................................................................38 6.2. Options with grep ..............................................................................................................................................38 6.3. sed Filter : .........................................................................................................................................................39 6.4. Reading and Writing to files (r,w): .....................................................................................................................40 6.5. Bracketed regular expression: ..........................................................................................................................41 6.6. awk Filter :.........................................................................................................................................................41 6.7. Locating files using the find command: .............................................................................................................42 6.8. Options of the find command ............................................................................................................................42 6.9. The ln command ...............................................................................................................................................44
7. Process...................................................................................................................................................45
7.1.Ps command ......................................................................................................................................................45 7.2. Background Processing: ...................................................................................................................................45 7.3. Requesting a Background Process:..................................................................................................................45 7.4. Checking on Background Process : ..................................................................................................................46 7.5. Terminating a Background Process : ................................................................................................................46
www.wilshiresoft.com Wilshire Software Technologies Ph: 2761-2214 / 2760-6414
8. Communications ...................................................................................................................................47
8.1. To-way communication-The Write Command ...................................................................................................47 8.2. The mesg Command.........................................................................................................................................47 8.3. The mail command- The Mail Box ....................................................................................................................47 8.4. The wall command:...........................................................................................................................................48
9. Introduction to Shell..............................................................................................................................49
9.1. Login process....................................................................................................................................................49 9.2. Types of shells : ................................................................................................................................................49 9.3. expr Command .................................................................................................................................................50 9.4. Environment Variables: .....................................................................................................................................50 9.5. The HOME variable:..........................................................................................................................................50 9.6. The PATH variable:...........................................................................................................................................50 9.7. Shell Script........................................................................................................................................................51 9.8. EXIT statement: ................................................................................................................................................51 9.9. Break AND continue:.........................................................................................................................................51 9.10. Exit status of the last command- the $? ..........................................................................................................51 9.11. if..then..else condition .....................................................................................................................................52 9.12. String test Operators .......................................................................................................................................54 9.13. Logical operators ............................................................................................................................................55 9.14. The case...esac construct ...............................................................................................................................55 9.15. for..do..done....................................................................................................................................................56 9.16. while..do..done................................................................................................................................................57 9.17. Parameter handling in shell scripts .................................................................................................................58
APPENDIX A...............................................................................................................................................60
Block commands in vi ..............................................................................................................................................60 Set Commands : ......................................................................................................................................................60 Map command: ........................................................................................................................................................60
APPENDIX B...............................................................................................................................................61
Shell Scripts and Shell Functions :...........................................................................................................................62
www.wilshiresoft.com