• Save
Advanced Shell Scripting
Upcoming SlideShare
Loading in...5
×
 

Advanced Shell Scripting

on

  • 2,623 views

Presentation for my speech at Li

Presentation for my speech at Li

Statistics

Views

Total Views
2,623
Views on SlideShare
2,169
Embed Views
454

Actions

Likes
3
Downloads
0
Comments
0

9 Embeds 454

http://www.n0on3.net 438
https://www.n0on3.net 5
http://www.slideshare.net 4
http://www.linkedin.com 2
http://www.lmodules.com 1
http://static.slidesharecdn.com 1
http://translate.googleusercontent.com 1
http://cache.baidu.com 1
https://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Advanced Shell Scripting Advanced Shell Scripting Presentation Transcript

  • Advanced Shell Scripting recalls, sed, awk and training LUG Roma 3 Alessandro Manfredi http://www.lugroma3.org
  • Shell ? sh, bash, ksh, csh, zsh, ...
  • How this works YOU will propose tasks for training ( So, start thinking about them! )
  • Recalls Should be familiar ... $PS1 : pipes | and | filters
  • ‘ test ‘ conditions -a, -o, -n : ***, **, *** -eq, -ne : ******, *** ****** -gt, -lt, -ge, -le : ******* ****, ***** **** -nt, -ot : ***** ****, ***** **** -e, -f, -d : ******, ** ****, ** * ********* -b, -c : ******, ********* -r -w -x : ** ********, ** ********, ** ********** -s : **** ******* **** ****
  • Control structures if [ condition ]; then action; else action; fi for i in {1..n}; do action $i; done for ((c=0;c<10;c++)); do echo $c; done while read line; do action $line ; done < infile case $var in 1|3|5) echo 1 ;; *) echo 0 ;; esac Meanings $?, $$, $#, $@, $1, $2, $3
  • Quoting Double quotes (weak): “ $this is going to be interpreted ” Single quotes (strong): ‘ this is not going to be interpreted ‘ Backquotes ( or backticks ): ` /this/is/going/to/be/executed/with/its args ` Variables expansion: “ ${ /the/result/may/be/quoted! } ”
  • Order Brace Expansion e.g.> ./run_Script_{1..5}.sh Tilde Expansion e.g.> echo ~ ~root ~xyz Variable Expansion e.g.> echo $var{1..5} Arithmetic Expansion e.g.> $(( $value+3*4/2 )) Command Substitution e.g.> echo “Today is `date` " Word Splitting e.g.> command arg1 arg2 Pathname Expansion e.g.> ls -lah | grep *.avi
  • Redirections > and >> ( without descriptors STDOUT is assumed ) 1 is STDOUT, 2 is STDERR e.g.: command 1> stdout.txt 2> stderr.txt 2>&1 means “send STDERR to STDOUT” 1>&2 means “send STDOUT to STDERR” >& and &> without descriptors redirect both 1 and 2
  • Vars var=”whatever” length=${#var} substring=${var:from:length} ... I/O read var, read -p “Prompt : “ var cat echo $whatever ...
  • Functions $ function greet { > echo “Hello $1” >} $ greet “ Mr.`whoami` ”
  • and another endless tools list... (e)grep [...] uniq tail / head tee tr, tr -d wc cut bc getopt(s) wget sort nc ...
  • sed non-interactive stream editor
  • Sed in pills sed ‘3d’ - delete the 3rd line of the stream sed ‘/^$/d’ - delete blank lines sed -n ‘/pattern/p/ - print all pattern-matching lines (by default all lines are also echoed, -n to suppress) sed ‘s/find/replace/g’ - substitute all matching with a replacement sed ‘s/quote/”&”/’ - use & to copy the matched pattern echo "ff66" | sed 's/([a-z]*)(.*)/21/' - use 1 2 etc to copy portions of the matched pattern
  • How to use sed cat file | sed ‘s/search/replace/g’ sed -e ‘cmd’ -i .ext file ( In place editing. If .ext is given, original files will be moved to file.ext ) sed ‘cmd’ <old_file >new_file
  • Exercise What is this ? find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' ( hint: ; may be used as separators, like / or _ )
  • AWK
  • AWK cat file | awk [opt] ‘pattern {action} [pattern {action}]’ input is parsed in records foreach record foreach pattern if record match pattern -> action default action : print $0 $0 is the entire record $1 is the 1st field, $2 the 2nd, etc.
  • AWK Patterns: BEGIN, END, /regex/, /regex/ in ( &&, ||, ! ) “” , $3==”value” , $4!=”value” , VAR==”value” Actions: {var=0}, {print “Field2=”$2}, {print 3*2}, {var+=1}, {action1 ; action2} { for(i=1; i<=NF; i++){printf "-%s- ", $i } print "X" } sub(/regex/,”replace”) , gsub(/regex/,”replace”,var) Options: -v VAR=”value”, -F “FS” Default variables: FS, RS, NR, NF
  • Training ... your turn !