You are on page 1of 20

UNIT-VII

uniq,sed,awk,sleep,time and
troubleshooting the scripts

BASICS OF OPERATING SYSTEM AND


SHELL SCRIPTING
uniq command
uniq command is used remove duplicates from a sorted
list.
paul@debian5:~/pipes$ cat music.txt
Queen
Brel
Queen
Abba
paul@debian5:~/pipes$ sort music.txt |uniq
Abba
Brel
Queen
uniq command
uniq can also count occurrences with the -c
option.
paul@debian5:~/pipes$ sort music.txt |uniq -c
1 Abba
1 Brel
2 Queen
sed command
The stream editor sed can perform editing functions in
the stream, using regular expressions.
paul@debian5:~/pipes$ echo level5 | sed 's/5/42/'
level42
paul@debian5:~/pipes$ echo level5 | sed 's/level/jump/'
jump5
sed command
Using sed you can remove lines from a stream containing a
character.
paul@debian5:~/test42$ cat tennis.txt
Venus Williams, USA
Martina Hingis, SUI
Justine Henin, BE
Serena williams, USA
Kim Clijsters, BE
paul@debian5:~/test42$ cat tennis.txt | sed '/BE/d'
Venus Williams, USA
Martina Hingis, SUI
Serena williams, USA
sleep command
The sleep command is sometimes used in
scripts to wait a number of seconds.

This example shows a five second sleep.


paul@rhel55 ~$ sleep 5
paul@rhel55 ~$
time command
The time command can display how long it takes to execute a command.
The date command takes only a little time.
paul@rhel55 ~$ time date
Sat Apr 17 13:08:27 CEST 2015
real 0m0.014s
user 0m0.008s
sys 0m0.006s
The sleep 5 command takes five real seconds to execute, but consumes little
cpu time.
paul@rhel55 ~$ time sleep 5
real 0m5.018s
user 0m0.005s
sys 0m0.011s
gzip and gunzip command
gzip is used to compress the file
paul@rhel55 ~$ gzip demo.txt
gunzip is used uncompress the file
paul@rhel55 ~$ gzip demo.gz
Troubleshooting the script
bash -x allows you to see the commands that the
shell is executing
paul@debian6~/test$ cat demo.sh
var4=42
echo $var4
paul@debian6~/test$ bash -x demo.sh
+ var4=42
+ echo 42
42
(( ))
The (( )) allows for evaluation of numerical
expressions
paul@test$ (( 42 > 33 )) && echo true || echo
false
true
paul@test$ (( 42 > 1201 )) && echo true ||
echo false
false
/etc/password
The local user database on Linux is
/etc/passwd.
[root@RHEL5 ~]# /etc/passwd
inge:x:518:524:art dealer:/home/inge:/bin/ksh
ann:x:519:525:flute player:/home/ann:/bin/bash
frederik:x:520:526:rubiu spoet:/home/frederik:/bin/bash
/etc/shadow
User passwords are encrypted and kept in /etc/shadow.
The /etc/shadow file is read only and can only be read
by root(Note: root is superuser)
[root@RHEL5 ~]# tail /etc/shadow
inge:$1$yWMSimOV$YsYvcVKqByFVYLKnU3ncd0:14
054:0:99999:7:::
ann:!!:14054:0:99999:7:::
frederik:!!:14054:0:99999:7:::
steven:!!:14054:0:99999:7:::
pascale:!!:14054:0:99999:7:::
Special variable
$0 The filename of the current script.
$n These variables correspond to the arguments with which a script
was invoked. Here n is a positive decimal number corresponding to
the position of an argument (the first argument is $1, the second
argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two
arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script
receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell.
$! The process number of the last background command
awk in linux
awk is an excellent tool for building
UNIX/Linux shell scripts.
awk is a programming language that is
designed for processing text-based data,
either in files or data streams, or using shell
pipes.
You can combine awk with shell scripts or
directly use at a shell prompt.
Print Text and Field
Print a Text File
awk '{ print }' /etc/passwd //Print the content of file
OR
awk '{ print $0 }' /etc/passwd // Print filename
Print Specific Field
Use as the input field separator and print first field
only i.e. usernames (will print the the first field. all
other fields are ignored):
awk -F':' '{ print $1 }' /etc/passwd
Send output to sort command using a shell pipe:
awk -F':' '{ print $1 }' /etc/passwd | sort
Pattern matching using awk
Pattern Matching-You can only print line of the
file if pattern matched.
awk '$9 == 500 { print }' /httpd/access.log
Display all lines from Apache log file if HTTP
error code is 500 (9th field logs status error
code for each http request)
Print Lines Containing tom, jerry AND vivek
awk '/tom|jerry|vivek/' /etc/passwd
Print pattern possibly on separate lines.
Arithmatic using awk
You get the sum of all the numbers in a column:

awk '{total += $1} END {print total}' earnings.txt

Shell cannot calculate with floating point


numbers, but awk can:
awk 'BEGIN {printf "%.3f\n", 2005.50 / 3}'
Environment variables

You might also like