1 / 44

INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING

INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING. What is Shell. It is the command interpreter which interprets the user commands and convey them to the kernel that executes them. Thus is acts as a mediator between the user and kernel. The Functions of Shell.

toyah
Télécharger la présentation

INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING

  2. What is Shell It is the command interpreter which interprets the user commands and convey them to the kernel that executes them. Thus is acts as a mediator between the user and kernel.

  3. The Functions of Shell Command line Interpretation Program Initiation Input-output Redirection Pipeline Connection Substitution of filenames Maintenance of Variables Environment Control Shell Programming

  4. Types of Shell Bourne Shell ,developed by Stephen Bourne Korn Shell ,developed by David Korn C Shell , developed by Bill Joy

  5. Escape Sequence \b Back Space \n New Line \r Carriage Return \t Tab \a Alert (Beep Sound) \\ Back Slash \’ Single Quote \” Double Quote

  6. Command Grouping Syntax:- command1;command2;command3 For Example :- 1. date;pwd;ls II. (date;pwd;ls)>result For Output:- cat result

  7. set $set friends come and go but enemies accumulate $echo $1 $2 $3 $4 $5 $6 $7 O/P:- friends come and go but enemies accumulate $echo $1 $2 $3 $4 O/P:- friends come and go

  8. Positional Parameters $0 – Refers to the name of the command. $1 – Refers to the first argument. $2 – Refers to the second argument. … & so on $* - Refers all the argument. $# - Refers the total no. of arguments. $? - Returns the exit status of last executed command. $! - Returns the Process Identification number (PID) of last background command (command ended with & ) $$ - Returns the PID of the current shell.

  9. $set Do you want credit or results $set A smiling face is always beautiful $echo $1 $2 $3 $4 $5 $6 O/P:- A smiling face is always beautiful $cat lucky O/P:- Give luck a little time $set `cat lucky` $set $1 $2 $3 $4 $5 O/P:-Give luck a little time

  10. $set Do you want credit or results $set A smiling face is always beautiful $echo $1 $2 $3 $4 $5 $6 O/P:- A smiling face is always beautiful $cat lucky O/P:- Give luck a little time $set `cat lucky` $set $1 $2 $3 $4 $5 O/P:-Give luck a little time

  11. Using Shift on Positional Parameters “Set is used to set upto 9 words” $set You have the capacity to learn from mistakes. You will learn a lot in your life. $echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 O/P:- You have the capacity to learn from mistakes. You You0 You1

  12. shift $shift 7 $echo $1 $2 $3 $4 $5 $6 $7 $8 $9 mistakes. You will learn a lot in your life. $set You have the capacity to learn from mistakes. You will learn a lot in your life. $a=$1 $b=$2 $c=$3 $d=$4 $e=$5 $f=$6 $g=$7

  13. $shift 7 $echo $a $b $c $d $e $f $g $1 $2 $3 $4 $5 $6 $7 $8 $9 O/P:- You have the capacity to learn from mistakes. You will learn a lot in your life. OR $echo $*

  14. Write a shell program to perform the positional parameters and shift command #Program of positional parameters and shift command set If you are headed in the wrong direction, God allows U turns. echo $* shift 1 echo $* shift 1 echo $* shift 1 echo $* Shift echo $*

  15. Operators Arithmetic Operator: +,-,\*,/ and % Relation Operator: -lt, -le, -gt, -ge, eq, -ne Logical Operator: -a( AND), -o (OR), !(NOT)

  16. String Operators

  17. Testing for files

  18. Taking Decision • The if-then-fi statement • The if-then-else-fi statement • The if-then-elif-else-if statement • The case-esac statement

  19. if–then–fi statement if [ <condition> ] then statement 1….statement n fi eg- echo “Enter source and destination file names read source destination if [ cp $source $destination ] then echo “ File Copied Successfully” fi

  20. if–then–else-fi statement if <condition> then statement 1 else statement 2 fi eg- echo “Enter source and destination file names read source destination if [ cp $source $destination ] then echo “ File Copied Successfully” else echo “Failed to copy the file fi

  21. if–then–elif–else-fi statement if <condition> then statement 1 elif <condition> statement 2 else statement 3 fi

  22. case-esac statement #Example of case statement echo “Enter the number from 1 to 3 : “ read num case $num in 1) echo You Entered 1 ;; 2) echo You Entered 2 ;; 3) echo You Entered 3 ;; *) echo I said 1 to 3 ;; esac case <value> in choice1) do this and this ;; choice2) do this and this ;; *) do this and this ;; esac

  23. Looping Statement • Using a while statement • Using a until statement • Using a for statement

  24. while statement count=1 while[ $count –le 10 ] do echo $count count=`expr $count + 1` done while [<condition>] do statement1 statement2 done

  25. count=1 while[ $count –lt 10 ] do echo $count count=`expr $count + 1` done until statement until [<condition>] do statement1 statement2 done count=1 until[ $count –gt 10 ] do echo $count count=`expr $count + 1` done

  26. for statement for control_variable in value1 value2…. do statement1 statement2 done for word in High on a hill was a lovely mountain do echo $word done

  27. AWK Programming • The command grep lets you locate words or patterns in a file. Next sed lets you add or delete lines depending upon a pattern. You could replace the words or change the case and so forth. Now we come to the granddaddy of utilities i.e. awk. • awk stands for Aho , Weinberger & Kernighan the creators of this utility. • awk can do pattern matching, print selected records/fields and comparison & computation. • We will take a look at how awk operates. awk operates like sed i.e. It processes the input line by line. Also it has to have a context or reference. The similarity ends here. When it comes to actions to be performed on the selected lines awk provides you a lot more features.

  28. awk operations • awk’s rules of the thumb or Simple awk Filters • Column selection or Splitting line into fields • Extended Regular expression [ERE] • Numerical Operators • The –F & f options • Files • User Defined Variables • Formatting Output • Redirection • BEGING and END Patterns

  29. awk’ rules of the thumb or Simple awk filter Syntax:- $awk [option] ‘selection criteria { print }’ <filename> Eg:- • $awk ‘/sales/’ file1 • $awk ‘/sales/ { print }’ file1 • $awk ‘/sales/ { print $0}’ file1

  30. Column Selection or Splitting line into fields Eg:- • $awk ‘/sales/ { print $1 $2 }’ file1 • $awk ‘/sales/ { print $1,$2 }’ file1 • $awk ‘/sales/ { print $2,$1 }’ file1 • $awk ‘/sales/ { print $3,$1,$2 }’ file1

  31. Extended Regular Expression [ERE] Eg:- • $awk ‘/[a-m]/ { print }’ file1 • $awk ‘/[a-m].*/ { print $1,$2 }’ file1 • $awk ‘/M[CB]A/ { print $0 }’ file1 • $awk ‘/M[!CB]A/ { print $3,$1,$2 }’ file1

  32. Numerical Operations • Assignment Operator :- = • Arithmetic Operators :- + , - , * , / , % • Logical Operator :- || , && , ! • Comparison Operators :- < ,> ,<= ,>= ,== ,!= • String Comparison:- =~ , !~

  33. Examples $awk ‘{ print $1,$2,$3+10,$4}’ file1 $awk ‘{ print $1-5,$2,$3,$4*4}’ file1 $awk ‘$4 >3’ file1 $awk ‘$4 <3’ file1 $awk ‘$4 <=3’ file1 $awk ‘$4 !=3’ file1 $awk ‘$4 ==3’ file1

  34. The –F option $awk –F”|” ‘/sales/{ print $1,$2}’ file1 $awk –F”|” ‘/sales/{ print $1,$2,$3}’ file1 $awk –F”|” ‘/sales/{ print $0}’ file1 $awk –F”|” ‘/sales/{ print }’ file1 $awk –F”|” ‘/sales/’ file1 The –f option :- …PTO

  35. Files File2 101 Ram sales good 102 Shyam Sales Good 103 John Purchase Good 104 Hari Marketing Good $cat>rep.awk /sales/ { print $1,$3} ^d $cat rep O/P:- /sales/ { print $1,$2,$3} $awk –f rep.awk file2 101 Ram sales 102 Shyam Sales

  36. $cat>rep.awk /sales/ { name = $2 dept = $3 print “Myself” name “and department is” dept } ^d $awk –f rep.awk file2 O/P:- Myself Ram and department is sales Myself Shyam and department is sales File2 101 Ram sales good 102 Shyam Sales Good 103 John Purchase Good 104 Hari Marketing Good

  37. User Defined Variables Sundry Pen 110 Pencil 100 Marker 200 Duster 150 $cat>Hike.awk { name = $1 price = $2 inc = 10 new_price = price + inc print name , price , newprice }^d $awk –f Hike.awk Sundry Pen 110 120 Pencil 100 110 Marker 200 210 Duster 150 160

  38. Formatting Output File Name :- Extra.awk /pen/ { name = $1 price = $2 nprice = price + price * 0.325 printf “%20s %10s \n”, name,nprice } $awk –f Extra.awk Sundry

  39. Redirection $awk ‘/sales/ { print }’ file1> Nfile1 $cat Nfile1

  40. BEGIN and END patterns The BEGIN subsection can contain all the steps that have to be worked out before awk begins to accept and work on the input file. This section can be use to print heading or any comment lines. BEGIN The END subsection takes care of any statement to be appended or totals to be printed etc. END

  41. PROBLEM BEGIN { print “THIS IS TRIAL” print “NAME OF OPRICE NPRICE” } /pen/ { name = $1 price = $2 oprice = price nprice = oprice * 1.25 } END { print name oprice nprice } Sundry Pen 110 Pencil 100 Marker 200 Duster 150 File named Bend.awk $awk –f Bend.awk Sundry EFFECT OUTPUT Pen 110 137.5

  42. Built in variables $awk ‘BEGIN { FS = “|” >NF != 6{ print “Record No “, NR , “has” , NF , “fields” }’ emp.txt O/P:- Record No 6 has 4 fields Record No 17 has 5 fields EXAMPLE

  43. Built in Functions

  44. ARRAY • Awk handles one-dimensional arrays. • The index for an array can be virtually anything; It can even be a string. • No array declarations are necessary; • An array is considered to be declared the moment it is used and is automatically initialized to zero, unless initialized explicitly.

More Related