). :wq
Step-3:
sh prg1
Result:
Let a = 10
Let b = 12
Output is 22
The reader is motivated to practice the following commands with all possible options
1. The ls command
: ls [option] filename
The ls command lists the names of files in given directory or in current directory if no filename is specified. The names of files are in ascending order.
[Options]: -l : long listing about each file.
-t : files listed in order in which they were last changed, most recent first.
2.
pwd
command — pwd
3.
mkdir
— mkdir <filename>
4.
cd
— cd <filename>
5.
rmdir
— rmdir <filename>
6.
chmod
— To Assign Permissions to files.
UNIX supports two levels of security one is through login and another security is implemented by assigning different types of access permissions to different files.
UNIX divides all users into three categories:
1. Owner
2. Group
3. Others
Syntax
Chmod nnn<file>
Where n is a number from 0 to 7 representing an octal value. First n denotes the permission for owner, next n for group and the last n for others. These numbers are:
4: For Read Permission (r)
2: For Write Permission (w)
1: For Execute Permission (x)
To assign more than one permission, respective octal values are added. As to assign read and write permission, octal value will be the sum of 4 (read) and 2 (write), i.e., 6. The permission set by these digits and their sum are given below:
Absolute Value
Break Up
Meaning
0
-
No permission
1
-
Only execute
2
-
Only write
3
2+1
Write & execute
4
-
Only read
5
4+1
Read & execute
6
4+2
Read & write
7
4+2+1
Read, write, execute
Examples:
$ chmod 400 <filename> : owner has only read permission.
$ chmod 700 <filename> : owner has read, write and execute permissions.
$ chmod 777 <filename> : owner, group and others have all permissions.
Another method to assigning permissions to files is symbolic method. To change permissions through this method one must specify:
Type of user (u,g,o).
Type of permission (r,w,x).
Whether the permission is to be granted(+) or revoked(-).
Name of the file.
Examples:
$ chmod u+r<file> : Add read permissions to owner.
$ chmod a+rw<file> : Add read/write permission to all users. (a means all users)
$ chmod -w<file> : Remove write permission from all users.
7. mv: Move files.
Syntax:
$ mv <file1> <file2>
8. cp: Copy files.
Syntax:
$ cp <file1> <file2>
9. rm: Remove files
Syntax:
$ rm <file1>
10. ln: Link files.
‘ln’ command is used for establishing an additional name for the same ordinary file so that it can be shared between two users.
Syntax:
$ ln <source> <target>
11. find: To Find files.
Syntax:
$ find <pathname><condition><action>
12. cat: To view files.
Syntax:
$ cat <filename>
13. Combine files.
Syntax:
$ cat file1.dat file2.bac file3.pqr>file4
This command merges the files (file1.dat, file2.bac and file3.pqr) into file4 to make a combined file.
14. pr: To Print files.
Syntax:
$pr <filename>
15. sort: To Sort the contents of a file.
Syntax:
$ sort <filename>
To explain this let us prepare a file:
$ cat temp.dat
Hyderabad
Delhi
Lucknow
Agra
Banglore
Now to arrange file in alphabetic order we can sort the file in this manner :
$ sort temp.dat It will display the following result on the screen
Agra
Banglore
Delhi
Hyderabad
Lucknow
16. cmp: To compare files.
Syntax:
$ cmp <file1> <file2> Result will look like
File1 file2 differ: char 280, line 18
Filters and Pipes
A filter is a program that takes input from the standard input, filters it and sends output to standard output. Some of filters provided by UNIX are grep, pg, wc, tr etc.
Filters and pipes commands
1.
grep
- search a file for keywords.
Syntax:
$grep regular_expr filename
Example
$grep “abc” emp.txt
2.
pg
- Used to display data one page (screenful) at a time.
Syntax:
$pg filename
3.
wc
- count number of lines/words/characters in file.
Syntax:
$wc [option] filename
Option
-l :Display number of lines
-w :Display number of words
-c :Display number of characters
4.
tr
– Translate or delete characters.
Syntax:
$tr [option] set1 set2
Example:
$tr ‘A-Z’ ‘a-z’| cat a1
5.
uniq -
The uniq command is used to display the uniq(ue) lines in a sorted file.
Syntax:
$uniq file1 file2
OTHER COMMANDS
1.
who -
list users currently logged in.
Syntax
$who
2.
tty -
Displays the terminal pathname.
syntax:
$tty
3.
echo –
display output on the screen. Echo also recognizes the c-language escape sequences that begin with a backslash.
\b : Backspace
\c : Print line without new line
\f : Form feed
\n : New-line
\r : Carriage return
\t : Tab
\v : Vertical Tab
\\ : Backslash
\nnn: It replaces the octal digits nnn to ASCII characters.
4. ps – Show list current processes.
Syntax:
$ps
5. date – display current date.
Syntax:
$date
6. password – Changes the password.
Syntax:
$passwd
7. clear – clear screen.
Syntax:
$clear
8. cal – display calendar.
Syntax:
$cal
$cal month year
$cal year
9. banner – prints the specified string in large letters.
Syntax:
$banner HELLO
10. man - read the online manual page for a command.
Syntax:
$man command
11. less - display a file a page at a time.
Syntax:
$less filename
12. tail - display the last few lines of a file.
Syntax:
$tail filename
13. head - display the first few lines of a file.