Unix and Shell Scripting

Translate Command:
$ tr “[a-z]” “[A-Z]” < sname > cap_names
$ cat cap_names

ls > myfile – Writes the output of ls to myfile
ls >> myfile – appends to myfile if it exists other wise behaves like >.
cat < myfile – prints the contents of myfile to the scree

Command Line Arguments:
myscript foo bar
$1 – Name of the script; i.e, myscript
$2 – foo
$3 – bar
Number of Arguments – $# is 2
$* or $@ – array of all the supplied arguments like `$1, $2`

Concatenate commands: Use a ;
Shell Arithmetic: expr 1 + 2
Back Quote: Executes a command. Eg. echo “Today is `date`”
read command: reads user input
eg. read name
echo “Hello $name”

uniq: prints those lines from a file which are unique

$? – Did the previous command execute successfully. 0 means true. 1 means false.
if condition
command1 if condition is true or if exit status
of condition is 0 (zero)

if condition
condition is zero (true – 0)
execute all commands up to else statement

if condition is not true then
execute all commands up to fi

for { variable name } in { list }
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)

while [ condition ]

How to debug the shell script?

sh -v {shell-script-name}
sh -x {shell-script-name}

Local and Global Variables:
Local variables are used within one shell where as global variables are used across shells. To make a variable global, use the export command.
export $Var

Conditional execution: && and ||
command1 && command2    command2 is executed if, and only if, command1 returns an exit status of zero.

command1 || command2    
command2 is executed if and only if command1 returns a non-zero exit status.

Dialog Utility

The Trap Command

trap {commands} {signal number list}
Signal Number
When occurs
shell exit
interrupt (CTRL+C)
kill (cannot be caught)
To catch signal, put the trap statement before calling the function.
Eg. trap ls 2. after executing this, if you press Ctrl+C. the ls command will be executed because a signal is set for that command.
Shift Command – is used to move the positional parameters to the left.
shift n – moves the positional parameters left by n positions. Useful if you have more than 9 arguments.


Unix Utilities:
Cut – cuts out selected data from a file or input.
Eg. cut -f1 students will give all the rows of column number 1 in the students table.
cut -f{field number} {file-name}
Paste – Paste utility joins textual information together.
E.g. paste file1 file2

The join utility: This is much like a database join.

join {file1} {file2}
Use of join utility: 
The join utility joins, lines from separate files. Join will only work, if there is common field in both file and if values are identical to each other.
Translate Command: Tr command is used to translate range of characters into other range of characters.
Eg. To make lower case to upper case:
$ tr “[a-z]” “[A-Z]” 
what a magic

input file contains a list of countries.
$ tr “u2” “a3” < inputfile
2 usa —> 3 asa

Each occurrence of character ‘u’ is replace with ‘a’ and ‘2’ with ‘3’.
tr {pattern-1} {pattern-2}


Consider an input file countries:

USA      Gold   4

UK        Gold 10

France   Gold   14

USA      Silver   10

France    Silver   20


Now, awk acts like a select utility in a database.

Eg. awk ‘/Gold/{print $3}’ countries





/Gold/ – is the pattern used for selecting lines from file.

{print $3} – action to be performed on third column.

countries – name of the file


sed- stream line editor is used to edit file without using an editor.

sed {expression} {file}


Eg. sed ‘/UK/s//China/g’ will replace all the occurrences of UK with China globally.


uniq – removing duplicate lines using uniq utility.

uniq utility compares adjacent lines, so duplicate lines must be next to each other. If there is more than one duplicate then first sort the file and then apply uniq.


unique {filename} or sort filename | unique


Grep – Matching patterns

grep “word” filename

Eg. grep “10” countries

UK Gold 10

USA Silver 10


ex – Expression.

Pattern is a set of characters which may be a word or not. If patterns are identified using special characters then such special characters are known as meta-characters.

Expression or regular expression is the combination of pattern and meta-characters.


ex {filename} starts the expression editor.

: {type commands or regular expression here}


:q – quits the ex editor



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s