Monday, July 4, 2011

Sheel Script - Easy Reference.

Vi Commands

To insert new text - esc + i ( You have to press 'escape' key then 'i')
To save file - esc + : + w (Press 'escape' key then 'colon' and finally 'w')
To save file with file name (save as) - esc + : + w "filename"
To quit the vi editor - esc + : + q
To quit without saving - esc + : + q!
To save and quit vi editor - esc + : + wq


To search for specified word in forward direction

esc + /word (Press 'escape' key, type /word-to-find,
for e.g. to find word 'shri', type as /shri)
To continue with search - n

To search for specified word in backward direction

esc + ?word (Press 'escape' key, type word-to-find)

To copy the line where cursor is located - esc + yy
To paste the text just deleted or copied at the cursor - esc + p
To delete entire line where cursor is located - esc + dd
To delete word from cursor position - esc + dw


To Find all occurrence of given word and Replace then globally
without confirmation

esc + :$s/word-to-find/word-to-replace/g

For. e.g. :$s/mumbai/pune/g

Here word "mumbai" is replace with "pune"



To Find all occurrence of given word and Replace then globally with confirmation

esc + :$s/word-to-find/word-to-replace/cg

To run shell command like ls, cp or date etc within vi

esc + :!shell-command

For e.g. :!pwd



Arithmetic Operators:-

1) expr op1 operator op2 ( Space is required by number and operators)

Eg:
expr 6 + 4
expr 6 - 4
expr 6 / 2
expr 6 % 4
expr 6 \* 3 (Since * is a wild card character we need to add a back slash before * symbol)

No space should be given while assigning a value to a variable

$ z=`expr 6 + 4`
$ echo $z


Shell Built in Variables Meaning

$# - Number of command line arguments. Useful to test no. of command line args in shell script.
$* - All arguments to shell
$@ - Same as above
$- - Option supplied to shell
$$ - PID of shell
$! - PID of last started background process (started with &)

Eg:-
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"


Some Commands:-

clear - clears the screen.


echo command:-

echo [options] [string, variables...]


echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l


-e Enable interpretation of the following backslash escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash

About quotes:-

" Double Quotes - Anything enclose in double quotes removed meaning of that characters (except \ and $).
' Single quotes - Enclosed in single quotes remains unchanged.
` Back quote - To execute command

$ echo "Today is `date`"
Today is Tue Jun 14 11:08:49 BST 2011
$ echo 'Today is `date`'
Today is `date`

To get the exit status $?

Eg"

$ expr 6 + 3
$ echo $?


Input command:-

read

eg:-

echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"


Execute more than 1 command in CLI

$date;who

Use Semicolon (;) to execute more than one commands simultaneously.


Commands

sort
tr
diff


Filters:-

head , tail, uniq


Conditional Statement:-

Syntax:-
if condition
then
stmt
elif
stmt
else
stmt
fi


eg

#/bin/sh
# Script to test rm command and exist status
#
if rm $1
then
echo "$1 file deleted"
fi


Test Command:-

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]


test or [ expr ] works with
1.Integer ( Number without decimal point)
2.File types
3.Character strings

Integer:-

-eq - Equal to
-ne - Not equal to
-lt - Less than
-le - Less than or equal to
-gt - Greater than
-ge - Greater than or equal to

Eg:-

if test 5 -lt 6 then
if [ 5 -lt 6 ]; then

Strings :-


string1 = string2 - String1 equal to string2
string1 != string2 - String1 not equal to string2
string1 - string1 is not null or not defined
-n string1 - string1 is not null does exists
-z string1 - string1 is null and does exists


File type and directories:-

-s file Non empty file
-f file Is File exist or normal file and not a directory
-d dir Is Directory exist and not a file
-w file Is writeable file
-r file Is read-only file
-x file Is file is executable


Logical operators:-


!expression - Logical NOT
expression1 -a expression2 - Logical AND
expression1 -o expression2 - Logical OR


Loop commands:-

for loop

Syntax:-
for {varibale} in { list..}
do
stmt1
stmtn
done
eg:-

for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

While Loop

Syntax:-

while [ condition ]
do
stmt1
stmtn
done


Case Statement:-

Syntax :-

case $varibale-name in
pattern1) command
command;;
pattern2) command
command;;
patternn) command
command;;
*) command
command;;
esac


Eg:-


rental=$1
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac


Debug Shell scripts:-

Syntax
sh option script_name

Option can be

-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable,
followed by the command and its expanded arguments.


I/O Redirections and File descriptions:-
> - Redirects to

Eg:-

cat file1 > file2

The contents of file1 will be redirected to file2


< - Gets from

cat < file2


The contents of file2 will be dispalyed, means the cat commands get the contents of file2.


In Linux (And in C programming Language) your keyboard, screen etc are all treated as files.
Following are name of such files

Standard File File Descriptors number Use Example
stdin 0 as Standard input Keyboard
stdout 1 as Standard output Screen
stderr 2 as Standard error Screen


By default in Linux every program has three files associated with it,
(when we start our program these three files are automatically opened by your shell). The use of first two files
(i.e. stdin and stdout) , are already seen by us. The last file stderr (numbered as 2) is used by our program to print error on screen.
You can redirect the output from a file descriptor directly to file with following syntax

Syntax:

file-descriptor-number>filename

Eg:-
Examples: (Assemums the file bad_file_name111 does not exists)
$ rm bad_file_name111
rm: cannot remove `bad_file_name111': No such file or directory
Above command gives error as output, since you don't have file. Now if we try to redirect this error-output to file,
it can not be send (redirect) to file, try as follows:

$ rm bad_file_name111 > er
Still it prints output on stderr as rm: cannot remove `bad_file_name111': No such file or directory,
and if you see er file as $ cat er , this file is empty, since output is send to error device and you can not
redirect it to copy this error-output to your file 'er'. To overcome this problem you have to use following command:

$ rm bad_file_name111 2>er
Note that no space are allowed between 2 and >, The 2>er directs the standard error output to file. 2 number is
default number (file descriptors number) of stderr file.


Eg:-

$ cat > demoscr
if [ $# -ne 2 ]
then
echo "Error : Number are not supplied"
echo "Usage : $0 number1 number2"
exit 1
fi
ans=`expr $1 + $2`
echo "Sum is $ans"

Run it as follows:
$ chmod 755 demoscr
$ ./demoscr
Error : Number are not supplied
Usage : ./demoscr number1 number2
$ ./demoscr > er1
$ ./demoscr 5 7
Sum is 12

For first sample run , our script prints error message indicating that you have not given two number.

For second sample run, you have redirect output of script to file er1, since it's error we have to show it to user,
It means we have to print our error message on stderr not on stdout. To overcome this problem replace above echo statements as follows
echo "Error : Number are not supplied" 1>&2
echo "Usage : $0 number1 number2" 1>&2
Now if you run it as follows:

$ ./demoscr > er1
Error : Number are not supplied
Usage : ./demoscr number1 number2


It will print error message on stderr and not on stdout. The 1>&2 at the end of echo statement,
directs the standard output (stdout) to standard error (stderr) device.
Syntax:
from>&destination



Functions:-

Syntax:-

function-name ()
{
command 1
command 2
.
.
command n
return
}

To automate function you need to add the function in /etc/bashrc file.

If you want to add particular function to particular user then open .bashrc file in users home directory as follows:

# vi .bashrc
OR
# mcedit .bashrc
At the end of file add following in .bashrc file
SayBye()
{
echo "Bye $LOGNAME ! Life never be the same, until you login again!"
echo "Press a key to logout. . ."
read
return
}

Tip: If you want to show some message or want to perform some action when you logout, Open file .bash_logout in your home
directory and add your stuff here For e.g. When ever I logout, I want to show message Buy! Then open your .bash_logout file
using text editor such as vi and add statement:
echo "Buy $LOGNAME, Press a key. . ."
read
return


getopts Command


This command is used to check valid command line argument are passed to script. Usually used in while loop.

Syntax:
getopts {optsring} {variable1}


getopts is used by shell to parse command line argument.
As defined in man pages:
"optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to
have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next
option in the shell variable variable1, When an option requires an argument, getopts places that argument into the
variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option
arguments are encountered. If an illegal option is seen, getopts places ? into variable1."

Example:-
We have script called ani which has syntax as
ani -n -a -s -w -d
Options: These are optional argument
-n name of animal
-a age of animal
-s sex of animal
-w weight of animal
-d demo values (if any of the above options are used their values are not taken)

Above ani script is as follows:
-------------------------------------------------------------------------------------
$ vi ani
#
# Usage: ani -n -a -s -w -d
#
#
# help_ani() To print help
#
help_ani()
{
echo "Usage: $0 -n -a -s -w -d"
echo "Options: These are optional argument"
echo " -n name of animal"
echo " -a age of animal"
echo " -s sex of animal "
echo " -w weight of animal"
echo " -d demo values (if any of the above options are used "
echo " their values are not taken)"
exit 1
}
#
#Start main procedure
#
#
#Set default value for variable
#
isdef=0
na=Moti
age="2 Months" # may be 60 days, as U like it!
sex=Male
weight=3Kg
#
#if no argument
#
if [ $# -lt 1 ]; then
help_ani
fi
while getopts n:a:s:w:d opt
do
case "$opt" in
n) na="$OPTARG";;
a) age="$OPTARG";;
s) sex="$OPTARG";;
w) weight="$OPTARG";;
d) isdef=1;;
\?) help_ani;;
esac
done
if [ $isdef -eq 0 ]
then
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"
else
na="Pluto Dog"
age=3
sex=Male
weight=20kg
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
fi
-------------------------------------------------------------------------------------
Save it and run as follows to view the output.
$ chmod +x ani
$ ani -n Lassie -a 4 -s Female -w 20Kg
$ ani -a 4 -s Female -n Lassie -w 20Kg
$ ani -n Lassie -s Female -w 20Kg -a 4


Essential Utilities:-


cut utility

Syntax:
cut -f{field number} {file-name}


Paste utility
Syntax:
paste {file1} {file2}

tr (translate utility)

Syntax:
tr {pattern-1} {pattern-2}


eg:-
tr "[a-z]" "[A-Z]" < file_name

awk utility:-

General Syntax of awk utility:
Syntax:
awk 'pattern action' {file-name}

filename -> inventory
egg order 4
cacke good 10
cheese okay 4
pen good 12
floppy good 5

After crating file issue command
$ awk '/good/ { print $3 }' inventory
10
12
5




sed utility - Editing file without using editor (sed - Steam line editor )
For this part of tutorial create data file as follows

Syntax:
sed {expression} {file}

Use of sed utility: sed is used to edit (text transformation) on given stream i.e a file or may be input from a pipeline.

check below example
filename : teaormilk

India's milk is good.
tea Red-Lable is good.
tea is better than the coffee.

After creating file give command
$ sed '/tea/s//milk/g' teaormilk > /tmp/result.tmp.$$
$ cat /tmp/result.tmp.$$:
India's milk is good.
milk Red-Lable is good.
milk is better than the coffee


Removing Duplicates:-

$ uniq filename

Expression:-

:p - Print line
:1,5 p - Print first 5 lines
:2 p - Print 2nd line.
:1,$ p - Print the whole file
:set number - It will set numbers to the lines.
:set nonubmer - Removes the numbers.
:1,d - delete first line
:1,5 d - delete first 5 lines.
:1,4 co $ - copy lines 1-4 and paste at the the end of the file. ($ end of the file)
:/linux/p - Find the word linux and print it.
:g /linux/p - Find the word linux on the entrie file and print it.
:1,$ /linux/p - Find the word linux on the entrie file and print it.
:8 s/learns/learn - Find the word learns in line 8 and replace it with learn
:1,$ s/learns/learn - Find the word learns in the entrie file and replace it with learn.
:g /sister/ s/never/always/ - Find the word sister and in that line if there is never then it will get replaced with always.
:g /Unix/ s/Unix/linux/ - Find the word unix and replace it with linux
:g /Unix/ s//linux/ - Find the word unix and replace it with linux (short cut)
:g /brother/s/also/xyz/ - Find the first occuerence of the word also and it will be replaced with xyz.
:g /brother/s/also/xys/g - Find all the occurence of the word also and it will be replaced with xyz.
:g /linux/s//unix/gc - Replace a word with users confirmation.
:g /the/p - Will find word like theater, the, brother, other etc.
:g /\ - Exact word.
:/^$ - Find empty line
:g /^$ - All blanks lines.
:g/[^/^$] - view file without blank lines.
:g /^$/d - Delete all the blank lines in a file
:u - Undo the last action.
:g/linux/p - Prints only linux
:g/[Ll]inux/p - Print linux and Linux
:g/[0-9] - number ranges.

Predenfied ranges:-

[:alnum:] - Letters and Digits (A to Z or a to z or 0 to 9)
[:alpha:] - Letters A to Z or a to z
[:cntrl:] - Delete character or ordinary control character (0x7F or 0x00 to 0x1F)
[:digit:] - Digit (0 to 9)
[:graph:] - Printing character, like print, except that a space character is excluded
[:lower:] - Lowercase letter (a to z)
[:print:] - Printing character (0x20 to 0x7E)
[:punct:] - Punctuation character (ctrl or space)
[:space:] - Space, tab, carriage return, new line, vertical tab, or form feed (0x09 to 0x0D, 0x20)
[:upper:] - Uppercase letter (A to Z)
[:xdigit:] - Hexadecimal digit (0 to 9, A to F, a to f)

eg: instead of writing :/[0-9A-Za-Z] we can write as :/[[:alnum:]]/

:g/c.*and

. (DOT) is special command of linux.

Here first c character is matched, then any single character (.) followed by n number of single character
(1 or 100 times even) and finally ends with and. This can found different word as follows command or catand etc

:1,$ s/[a-z]/\u &/g - Convert lower case to upper case.

1,$ Line Address location is all i.e. find all lines for following pattern
s Substitute command
/[a-z]/ Find all lowercase letter - Target
\u&/ Substitute to Uppercase. \u& means substitute last patter (&) matched with its UPPERCASE replacement (\u)
Note: Use \l (small L) for lowercase character.
g Global replacement



awk Examples:-

filename :- inven

Sr.No Product Qty Unit Price
1 Pen 5 20.00
2 Rubber 10 2.00
3 Pencil 3 3.50
4 Cock 2 45.50

$awk '{ print $1 $2 "--> Rs." $3 * $4 }' inven
$awk '{ print $2 $4}' inven
$awk '{ print $0 }' inven


You can easily, do the arithmetic with awk as follows


$ cat > math
{
print $1 " + " $2 " = " $1 + $2
print $1 " - " $2 " = " $1 - $2
print $1 " / " $2 " = " $1 / $2
print $1 " x " $2 " = " $1 * $2
print $1 " mod " $2 " = " $1 % $2
}

$ awk -f math
20 3
20 + 3 = 23
20 - 3 = 17
20 / 3 = 6.66667
20 x 3 = 60
20 mod 3 = 2
(Press CTRL + D to terminate)
------------------------------

Hi Use the \ operator in sheel scripts if you are using V$ tables.
Eg:-

V\$sql.













Thanks to Vivek G. Gite

No comments:

Post a Comment