tip

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows
Syntax:
. command-name

Example:
$ . foo

 

 #################################################################################################

 

There are three types of quotes

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

Example:
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back quote?

 

############################################################################################

 

Wild cards (Filename Shorthand or meta Characters)

 

Wild card /Shorthand Meaning Examples
* Matches any string or group of characters. $ ls * will show all files
$ ls a* will show all files whose first name is starting with letter 'a'
$ ls *.c   will show all files having extension .c
$ ls ut*.c will show all files having extension .c but file name must begin with 'ut'.
? Matches any single character. $ ls ?   will show all files whose names are 1 character long 
$ ls fo? will show all files whose names are 3 character long and file name begin with fo
[...] Matches any one of the enclosed characters $ ls [abc]*  will show all files beginning with letters a,b,c

Note:
[..-..] A pair of characters separated by a minus sign denotes a range.

Example:
$ ls /bin/[a-c]*

Will show all files name beginning with letter a,b or c like

   /bin/arch           /bin/awk           /bin/bsh     /bin/chmod           /bin/cp
   /bin/ash           /bin/basename   /bin/cat      /bin/chown           /bin/cpio
   /bin/ash.static   /bin/bash          /bin/chgrp   /bin/consolechars  /bin/csh

But
$ ls /bin/[!a-o]
$ ls /bin/[^a-o]

If the first character following the [ is a ! or a ^ ,then any character not enclosed is matched i.e. do not show us file name that beginning with a,b,c,e...o, like

   /bin/ps            /bin/rvi              /bin/sleep /bin/touch      /bin/view
   /bin/pwd           /bin/rview        /bin/sort   /bin/true        /bin/wcomp
   /bin/red           /bin/sayHello     /bin/stty   /bin/umount   /bin/xconf
   /bin/remadmin  /bin/sed           /bin/su      /bin/uname     /bin/ypdomainname
   /bin/rm            /bin/setserial    /bin/sync   /bin/userconf  /bin/zcat
   /bin/rmdir         /bin/sfxload      /bin/tar    /bin/usleep
   /bin/rpm           /bin/sh            /bin/tcsh    /bin/vi

####################################################################

NOTE:
$# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to script.

 

########################################################################

 

It means when ever there is any type of comparison in Linux Shell It gives only two answer one is YES and NO is other.

 

In Linux Shell Value  Meaning Example
Zero Value (0) Yes/True  0
NON-ZERO Value No/False -1, 32, 55 anything but not zero

############################################################################################

 

 

For string Comparisons use

Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined 
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist

Shell also test for file and directory types

Test Meaning
-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

 

###########################################################################################

 

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. To clear your idea onsider another example by writing shell script as follows:

$ 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

#################################################################################################

 

 Now consider following shell script:

$ cat > testsign1
#
# Why to trap signal, version 1
#
Take_input1()
{
 recno=0
 clear
 echo "Appointment Note keeper Application for Linux"
 echo -n "Enter your database file name : "
 read filename
if [ ! -f $filename ]; then
  echo "Sorry, $filename does not exit, Creating $filename database"
  echo "Appointment Note keeper Application database file" > $filename
fi
echo "Data entry start data: `date`" >/tmp/input0.$$
#
# Set a infinite loop
#
while :
do
     echo -n "Appointment Title:"
     read na
     echo -n "time :"
     read ti
     echo -n "Any Remark :"
     read remark
     echo -n "Is data okay (y/n) ?"
     read ans
if [ $ans = y -o $ans = Y ]; then
    recno=`expr $recno + 1`
    echo "$recno. $na $ti $remark" >> /tmp/input0.$$
fi
echo -n "Add next appointment (y/n)?"
read isnext
 if [ $isnext = n -o $isnext = N ]; then
     cat /tmp/input0.$$ >> $filename
     rm -f /tmp/input0.$$
    return # terminate loop
 fi
done
}
#
#
# Call our user define function : Take_input1
#
Take_input1

Save it and run as
$ chmod +x testsign1
$ ./testsign1

It first ask you main database file where all appointment of the day is stored, if no such database file found, file is created, after that it open one temporary file in /tmp directory, and puts today's date in that file. Then one infinite loop begins, which ask appointment title, time and remark, if this information is correct its written to temporary file, After that, script asks user , whether he/she wants to add next appointment record, if yes then next record is added , otherwise all records are copied from temporary file to database file and then loop will be terminated. You can view your database file by using cat command. Now problem is that while running this script, if you press CTRL + C, your shell script gets terminated and temporary file are left in /tmp directory. For e.g. try it as follows
$./testsign1
After given database file name and after adding at least one appointment record to temporary file press CTRL+C, Our script get terminated, and it left temporary file in /tmp directory, you can check this by giving command as follows
$ ls /tmp/input*
Our script needs to detect such signal (event) when occurs; To achieve this we have to first detect Signal using trap command.
Syntax:
trap {commands} {signal number list}

 

Signal Number When occurs
0 shell exit 
1 hangup
2 interrupt (CTRL+C)
3 quit 
9 kill (cannot be caught)

To catch signal in above script, put trap statement before calling Take_input1 function as trap del_file 2 ., Here trap command called del_file() when 2 number interrupt ( i.e. CTRL+C ) occurs. Open above script in editor and modify it so that at the end it will look like as follows:

$ vi testsign1
#
# signal is trapped to delete temporary file , version 2
#
del_file()
{
  echo "* * * CTRL + C Trap Occurs (removing temporary file)* * *"
  rm -f /tmp/input0.$$
  exit 1
}


Take_input1()
{
recno=0
clear
echo "Appointment Note keeper Application for Linux"
echo -n "Enter your database file name : "
read filename
if [ ! -f $filename ]; then
  echo "Sorry, $filename does not exit, Creating $filename database"
  echo "Appointment Note keeper Application database file" > $filename
fi
echo "Data entry start data: `date`" >/tmp/input0.$$
#
# Set a infinite loop
#
while :
do
  echo -n "Appointment Title:"
  read na
  echo -n "time :"
  read ti
  echo -n "Any Remark :"
  read remark
  echo -n "Is data okay (y/n) ?"
  read ans
  if [ $ans = y -o $ans = Y ]; then
   recno=`expr $recno + 1`
   echo "$recno. $na $ti $remark" >> /tmp/input0.$$
  fi
  echo -n "Add next appointment (y/n)?"
  read isnext
  if [ $isnext = n -o $isnext = N ]; then
    cat /tmp/input0.$$ >> $filename
    rm -f /tmp/input0.$$
    return # terminate loop
  fi
done # end_while
}
#
# Set trap to for CTRL+C interrupt i.e. Install our error handler
# When occurs it first it calls del_file() and then exit
#
trap del_file 2
#
# Call our user define function : Take_input1
#
Take_input1

Run the script as:
$ ./testsign1

After giving database file name and after giving appointment title press CTRL+C, Here we have already captured this CTRL + C signal (interrupt), so first our function del_file() is called, in which it gives message as "* * * CTRL + C Trap Occurs (removing temporary file)* * * " and then it remove our temporary file and then exit with exit status 1. Now check /tmp directory as follows
$ ls /tmp/input*
Now Shell will report no such temporary file exit.

 

################################################################################################

 

You can also move the positional parameters over more than one place by specifying a number with the shift command. The following command would shift the positional parameters two places:

shift 2

 

################################################################################################

 

As you can see shift command can use to parse the command line (args) option. This is useful if you have limited number of command line option. If command line options are too many then this approach works slowly as well as complex to write and maintained. You need to use another shell built in command - getopts. Next section shows the use of getopts command. You still need the shift command in conjunction with getopts and for other shell scripting work.

 

################################################################################################

 

Examlpe:
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)

 See because of getopts, we can pass command line argument in different style. Following are invalid options for ani script
$ ani -nLassie -a4 -sFemal -w20Kg

Animal Name: Lassie, Age: 4, Sex: Femal, Weight: 20Kg (user define mode)

 No space between option and their value.

$ ani -nLassie-a4-sFemal-w20Kg

Animal Name: Lassie-a4-sFemal-w20Kg, Age: 2 Mouths, Sex: Male, Weight: 3Kg (user define mode)

 $ ani -n Lassie -a 4 -s Female -w 20Kg -c Mammal

./ani: illegal option -- c
Usage: ./ani -n -a -s -w -d

 -c is not one of the valid options.

 

##################################################################################################

 

sname

Sr.No     Name
11          Vivek
12          Renuka
13          Prakash
14         Ashish
15         Rani

smark

Sr.No    Mark
11          67
12          55
13          96
14          36
15          67

 

 

 

 $cut -f2 sname
Vivek
Renuka
Prakash
Ashish
Rani


cut utility cuts out selected data from sname file. To select Sr.no. field from sname give command as follows:
$cut -f1 sname
11
12
13
14
15

 

Command Explanation
cut    Name of cut utility
-f1  Using (-f) option, you are specifying the extraction field number. (In this example its 1 i.e. first field)
sname File which is used by cut utility and which is use as input for cut utility.

 

 

$ paste sname smark
11    Vivek      11    67
12    Renuka   12    55
13    Prakash 13    96
14    Ashish   14     36
15    Rani      15     67

 

 

$join sname smark
11   Vivek       67
12   Renuka    55
13   Prakash  96
14   Ashish     36
15   Rani        67

 

 

$ tr "h2" "3x" < sname
11   Vivek
1x   Renuka
13   Prakas3
14   As3is3
15   Rani

 

$ tr "[a-z]" "[A-Z]"
hi i am Vivek
HI I AM VIVEK
what a magic
WHAT A MAGIC

 

##################################################################################################

 

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

awk utility, select each record from file containing the word "good" and performs the action of printing the third field (Quantity of available goods.). Now try the following and note down its output.
$ awk '/good/ { print $1 " " $3 }' inventory

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

################################################################################################

 

personame

Hello I am vivek
12333
12333
welcome
to
sai computer academy, a'bad.
what still I remeber that name.
oaky! how are u luser?
what still I remeber that name.


After creating file, issue following command at shell prompt
$ uniq personame
Hello I am vivek
12333
welcome
to
sai computer academy, a'bad.
what still I remeber that name.
oaky! how are u luser?
what still I remeber that name.

Above command prints those lines which are unique. For e.g. our original file contains 12333 twice, so additional copies of 12333 are deleted. But if you examine output of uniq, you will notice that 12333 is gone (Duplicate), and "what still I remeber that name" remains as its. Because the uniq utility compare only adjacent lines, duplicate lines must be next to each other in the file. To solve this problem you can use command as follows
$ sort personame | uniq

 

################################################################################################

 

demo-file

hello world!
cartoons are good
especially toon like tom (cat)
what
the number one song
12221
they love us
I too

After saving file, issue following command,
$ grep "too" demofile
cartoons are good
especially toon like tom (cat)
I too

grep will locate all lines for the "too" pattern and print all (matched) such line on-screen. grep prints too, as well as cartoons and toon; because grep treat "too" as expression. Expression by grep is read as the letter t followed by o and so on. So if this expression is found any where on line its printed. grep don't understand words.

Syntax:
grep "word-to-find" {file-name}

 

##############################################################################################

 

Command like
:g/the/p
It is different from all other Os
My brother Vikrant also loves linux who also loves unix.

Will find word like theater, the, brother, other etc. What if you want to just find the word like "the" ? To find the word (Let's say Linux) you can give command like
:/\<Linux\>
Linux is cooool.
:g/\<Linux\>/p
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux

The symbol \< and \> respectively match the empty string at the beginning and end of the word. To find the line which contain Linux pattern at the beginning give command

 ##################################################################################

 

Even you can try it as follows
:g/[0-9]
Linux is now 10 years old.
Next year linux will be 11 year old.

Here range of digit is specified by giving first digit (0-zero) and last digit (1), separated by hyphen. You can try [a-z] for lowercase character, [A-Z] for uppercase character. Not just this, there are certain named classes of characters which are predefined. They are as follows:

Predefined classes of characters Meaning
[: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)

For e.g. To find digit or alphabet (Upper as well as lower) you will write
:/[0-9A-Za-Z]

Instead of writing such command you could easily use predefined classes or range as follows
:/[[:alnum:]]

The . (dot) matches any single character.
For e.g. Type following command
:g/\<.o\>
She only loves to play games and nothing else.
Do you know?

This will include lo(ves), Do, no(thing) etc.

* Matches the zero or more times
For e.g. Type following command
:g/L*
Hello World.
This is vivek from Poona.
....
....

:g/Li*
Linux is cooool.
Linux is now 10 years old.
Rani my sister never uses Linux

: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.

In the regular expression metacharacters such . (DOT) or * loss their special meaning if we use as \. or \*. The backslash removes the special meaning of such meatcharacters and you can use them as ordinary characters. For e.g. If u want to search . (DOT) character at the beginning of line, then you can't use command as follows
:g/^.
Hello World.
This is vivek from Poona.
....
..
...
. (DOT) is special command of linux.

Okay! I will stop.

Instead of that use
:g/^\.
. (DOT) is special command of linux.

 

##########################################################################################################

 

awk reads the input from given file (or from stdin also) one line at a time, then each line is compared with pattern. If pattern is match for each line then given action is taken. Pattern can be regular expressions. Following is the summery of common awk metacharacters:

Metacharacter Meaning
. (Dot) Match any character
* Match zero or more character
^ Match beginning of line
$ Match end of line
\ Escape character following
[ ] List
{ } Match range of instance
+ Match one more preceding
? Match zero or one preceding
| Separate choices to match

 

########################################################################################################################################################

 

Consider following text database file

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

In above file fields are Sr.No,Product,Qty,Unit Price. Field is the smallest element of any record. Each fields has its own attributes. For e.g. Take Qty. field. Qty. fields attribute is its numerical (Can contain only numerical data). Collection of fields is know as record. So
1. Pen 5 20.00 ----> Is a Record.

Collection of record is know as database file. In above text database file each field is separated using space (or tab character) and record is separated using new-line character ( i.e. each record is finished at the end of line ). In the awk, fields are access using special variable. For e.g. In above database $1, $2, $3, $4 respectively represents Sr.No, Product, Qty, Unit Price fields. (Don't confuse $1,$2 etc with command line arguments of shell script)

For this part of tutorial create text datafile inven (Shown as above). Now enter following simple awk program/command at shell prompt:
$ awk '{ print $1 $2 "--> Rs." $3 * $4 }' inven
1.Pen--> Rs.100
2.Pencil--> Rs.20
3.Rubber--> Rs.10.5
4.Cock--> Rs.91

Above awk program/command can be explained as follows:

awk program statement Explanation
'{ print $1 $2 "--> Rs." $3 * $4 } ' print command is used to print contains of variables or text enclose in " text ". Here $1, $2, $3,$4 are all the special variable. $1, $2,  etc all of the variable contains value of field. Finally we can directly do the calculation using $3 * $4 i.e. multiplication of third and fourth field in database. Note that "--> Rs." is string which is printed as its.

Note $1,$2 etc (in awk) also know as predefined variable and can assign any value found in field.

Type following awk program at shell prompt,
$ awk '{ print $2 }' inven
Pen
Pencil
Rubber
Cock

awk prints second field from file. Same way if you want to print second and fourth field from file then give following command:
$awk '{ print $2 $4}' inven
Pen20.00
Pencil2.00
Rubber3.50
Cock45.50

$0 is special variable of awk , which print entire record, you can verify this by issuing following awk command:
$ awk '{ print $0 }' inven
1. Pen 5 20.00
2. Pencil 10 2.00
3. Rubber 3 3.50
4. Cock 2 45.50

You can also create awk command (program) file as follows:

$ cat > prn_pen
/Pen/ { print $3 }

And then you can execute or run above "prn_pen" awk command file as follows
$ awk -f prn_pen inven
5
10

In above awk program /Pen/ is the search pattern, if this pattern is found on line (or record) then print the third field of record.
{ print $3 }
is called Action. On shell prompt , $ awk -f prn_pen inven , -f option instruct awk, to read its command from given file, inven is the name of database file which is taken as input for awk.

Now create following awk program as follows:

 $cat > comp_inv
3 > 5 { print $0 }

Run it as follows:
$ awk -f comp_inv inven
2. Pencil 10 2.00

Here third field of database is compared with 5, this the pattern. If this pattern found on any line database, then entire record is printed.

 

###############################################################################################

 

Create awk file as follows:

$cat > def_var
{
print "Printing Rec. #" NR "(" $0 "),And # of field for this record is " NF
}

Run it as follows.
$awk -f def_var inven
Printing Rec. #1(1. Pen 5 20.00),And # of field for this record is 4
Printing Rec. #2(2. Pencil 10 2.00),And # of field for this record is 4
Printing Rec. #3(3. Rubber 3 3.50),And # of field for this record is 4
Printing Rec. #4(4. Cock 2 45.50),And # of field for this record is 4

NR and NF are predefined variables of awk which means Number of input Record, Number of Fields in input record respectively. In above example NR is changed as our input record changes, and NF is constant as there are only 4 field per record. Following table shows list of such built in awk variables.

 

awk Variable Meaning
FILENAME Name of current input file
RS Input record separator character (Default is new line)
OFS Output field separator string (Blank is default)
ORS Output record separator string (Default is new line)
NF Number of input record
NR Number of fields in input record
OFMT Output format of number
FS Field separator character (Blank & tab is default)

 #############################################################################################################################################################

 

 

 

posted @ 2014-01-01 15:16  baihuahua  阅读(339)  评论(0编辑  收藏  举报