Your SlideShare is downloading. ×
0
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Formation sh
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Formation sh

200

Published on

Introduction au scripting SH

Introduction au scripting SH

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
200
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Syntax VariablesBuilt-in Commands Awk, Sed et Co. Scripting with Sh Nicolas Ledez 15 septembre 2008 Nicolas Ledez Scripting with Sh
  • 2. Syntax Variables Built-in Commands Awk, Sed et Co.Outline 1 Syntax 2 Variables 3 Built-in Commands 4 Awk, Sed et Co. Nicolas Ledez Scripting with Sh
  • 3. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsFilename Metacharacters String Meaning * Match any string of zero or more characters ? Match any single character [abc...] Match any one of the enclosed characters ; a hyphen can specify a range (e.g., a–z, A–Z, 0–9) [!abc...] Match any character not enclosed as above Nicolas Ledez Scripting with Sh
  • 4. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsFilename Metacharacters - examples String Meaning new* Match new new.1 ch? Match ch9 but not ch10 [D-R]* Match files that begin with uppercase D through R Nicolas Ledez Scripting with Sh
  • 5. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsQuoting Character Meaning ; Command separator & Background execution ( ) Command grouping | Pipe < > & Redirection symbols * ? [ ] ~ + - @ ! Filename metacharacters " ’ Used in quoting other characters ‘ Command substitution $ Variable substitution space tab newline Word separators Nicolas Ledez Scripting with Sh
  • 6. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsQuoting - examples $ echo ’Single quotes "protect" double quotes’ Single quotes "protect" double quotes $ echo "Well, isn’t that "special" ?" Well, isn’t that "special" ? $ echo "You have ‘ls | wc -l‘ files in ‘pwd‘" You have 43 files in /home/bob Nicolas Ledez Scripting with Sh
  • 7. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsCommand Forms 1/2 cmd & Execute cmd in background cmd1 ; cmd2 Command sequence ; execute multiple cmds on the same line { cmd1 ; cmd2 ; } Execute commands as a group in the current shell (cmd1 ; cmd2) Execute commands as a group in a subshell cmd1 | cmd2 Pipe ; use output from cmd1 as input to cmd2 cmd1 ‘cmd2‘ Command substitution ; use cmd2 output as arguments to cmd1 Nicolas Ledez Scripting with Sh
  • 8. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsCommand Forms 2/2 cmd1 && cmd2 AND ; execute cmd1 and then (if cmd1 succeeds) cmd2. This is a “short-circuit” operation ; cmd2 is never executed if cmd1 fails cmd1 || cmd2 OR ; execute either cmd1 or (if cmd1 fails) cmd2. This is a “short-circuit” operation ; cmd2 is never executed if cmd1 suc- ceeds Nicolas Ledez Scripting with Sh
  • 9. Command Forms - examples $ nroff file > file.txt & Format in the background $ cd ; ls Execute sequentially $ (date ; who ; pwd) > logfile All output is redirected $ sort file | pr -3 | lp Sort file, page output, then print $ vi ‘grep -l ifdef *.c‘ Edit files found by grep $ egrep ’(yes|no)’ ‘cat list‘ Specify a list of files to search $ grep XX file && lp file Print file if it contains the pattern $ grep XX file || echo "XX not found" otherwise, echo an error message
  • 10. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsRedirection Forms File De- Name Common Typical Default scriptor Abbreviation 0 Standard input stdin Keyboard 1 Standard output stdout Terminal 2 Standard error stderr Terminal Nicolas Ledez Scripting with Sh
  • 11. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsRedirection Forms - Simple redirection $ cmd > file Send output of cmd to file (overwrite) $ cmd » file Send output of cmd to file (append) $ cmd < file Take input for cmd from file $ cmd « text The contents of the shell script up to a line identical to text become the standard input for cmd (text can be stored in a shell variable) Nicolas Ledez Scripting with Sh
  • 12. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsRedirection Forms - Redirection using file descriptors cmd >&n Send cmd output to file descriptor n cmd m>&n Same, except that output that would normally go to file descriptor m is sent to file descriptor n instead cmd >&- Close standard output cmd <&n Take input for cmd from file descriptor n cmd m<&n Same, except that input that would normally come from file descriptor m comes from file descriptor n instead cmd <&- Close standard input Nicolas Ledez Scripting with Sh
  • 13. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsRedirection Forms - examples 1/2 $ cat part1 > book $ cat part2 part3 >> book $ mail tim < report $ sed ’s/^/XX /g’ << "END_ARCHIVE" > echo toto $titi > END_ARCHIVE XX echo toto $titi $ export titi=qsdf $ sed ’s/^/XX /g’ << END_ARCHIVE > echo toto $titi > END_ARCHIVE XX echo toto qsdf Nicolas Ledez Scripting with Sh
  • 14. Syntax Filename Metacharacters Variables Quoting Built-in Commands Command Forms Awk, Sed et Co. Redirection FormsRedirection Forms - examples 2/2 $ echo "Usage error: see administrator" 1>&2 To redirect standard output to standard error $ find / -print > filelist 2>no_access The following command sends output (files found) to filelist and error messages (inaccessible files) to file no_access Nicolas Ledez Scripting with Sh
  • 15. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Variable Substitution String Meaning var=value Set each variable var to a value $var ${var} Use value of var ${var:-value} Use var if set ; otherwise, use value ${var:=value} Use var if set ; otherwise, use value and assign value to var ${var:?value} se var if set ; otherwise, print value and exit (if not interactive). If value isn’t supplied, print the phrase "parameter null or not set." ${var:+value} Use value if var is set ; otherwise, use nothing Nicolas Ledez Scripting with Sh
  • 16. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Variable Substitution 1/2 $ u=up d=down blank= Assign values to three variables (last is null) $ echo ${u}root uproot Braces are needed here $ echo ${u-$d} up Display value of u or d ; since u is set, it’s printed $ echo ${tmp-‘date‘} Thu Feb 4 15:03:46 EST 1993 If tmp is not set, the date command is executed Nicolas Ledez Scripting with Sh
  • 17. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Variable Substitution 1/2 $ u=up d=down blank= $ echo ${blank="no data"} blank is set, so it is printed (a blank line) $ echo ${blank:="no data"} no data blank is set but null, so the string is printed $ echo $blank no data blank now has a new value Nicolas Ledez Scripting with Sh
  • 18. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Built-in Shell Variables 1/2 String Meaning $# Number of command-line arguments $- Options currently in effect (arguments supplied to sh or to set) $? Exit value of last executed command $$ Process number of current process $! Process number of last background command Nicolas Ledez Scripting with Sh
  • 19. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Built-in Shell Variables 2/2 String Meaning $0 First word ; that is, command name. This will have the full path name if it was found via a PATH search $n Individual arguments on command line (positional parameters, n = 1–9). $*, $@ All arguments on command line ($1 $2 ...) "$*" All arguments on command line as one string ("$1 $2...") "$@" All arguments on command line, indi- vidually quoted ("$1" "$2" ...) Nicolas Ledez Scripting with Sh
  • 20. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Other Shell Variables 1/2 String Meaning CDPATH=dirs Directories searched by cd ; allows shortcuts in changing directories ; un- set by default HOME=dir Home directory ; set by login (from /etc/passwd file) IFS=’chars’ Input field separators ; default is space, tab, and newline LANG=dir Directory to use for certain language- dependent programs MAIL=file Default file in which to receive mail ; set by login Nicolas Ledez Scripting with Sh
  • 21. Syntax Variable Substitution Variables Built-in Shell Variables Built-in Commands Other Shell Variables Awk, Sed et Co.Other Shell Variables 2/2 String Meaning MAILCHECK=n Number of seconds between mail checks ; default is 600 (10 minutes) PATH=dirlist One or more pathnames, delimited by colons, in which to search for com- mands to execute PS1=string Primary prompt string ; default is $ PS2=string Secondary prompt (used in multiline commands) ; default is > SHELL=file Name of default shell (e.g., /bin/sh) TERM=string Terminal type Nicolas Ledez Scripting with Sh
  • 22. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptshell-bang # : # ! / b i n / sh # comment ... Nicolas Ledez Scripting with Sh
  • 23. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Script!: i f who | grep r o o t > / dev / n u l l then echo r o o t i s c u r r e n t l y logged on fi i f ! who | grep r o o t > / dev / n u l l then echo r o o t i s n o t c u r r e n t l y logged on fi i f who | grep r o o t > / dev / n u l l then : # Do n o t h i n g i f p a t t e r n i s found else echo r o o t i s n o t c u r r e n t l y logged on fi Nicolas Ledez Scripting with Sh
  • 24. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptcd pwd kill sleep time $ pwd / home / admin $ cd / tmp / $ pwd / tmp $ k i l l 19524 $ date && s l e e p 2 && date Mon Aug 13 1 6 : 3 0 : 2 9 CEST 2007 Mon Aug 13 1 6 : 3 0 : 3 1 CEST 2007 $ t i m e ( f i n d / tmp >/ dev / n u l l 2>&1) real 0m0.002 s user 0m0.000 s sys 0m0.004 s Nicolas Ledez Scripting with Sh
  • 25. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttype ulimit umask $ t y p e mv read mv i s / b i n / mv read i s a s h e l l b u i l t i n # Turn o f f w r i t e p e r m i s s i o n f o r o t h e r s : # Produces f i l e p e r m i s s i o n −rw−rw−r−− $ umask 002 # Turn o f f a l l p e r m i s s i o n s f o r group & o t h e r s : # Produces f i l e p e r m i s s i o n −rw−−−−−−− $ umask 077 $ man u l i m i t Nicolas Ledez Scripting with Sh
  • 26. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptif i f [ $ c o u n t e r − l t 10 ] then number=0 $ c o u n t e r else number= $ c o u n t e r fi i f [ ! −d $ d i r ] ; then mkdir $ d i r chmod 775 $ d i r fi Nicolas Ledez Scripting with Sh
  • 27. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptfor f i l e _ l i s t =" i n s t a l l _ p e a r l _ h o s t . t x t " HOST_OK= ‘ awk ’ / [ X ] / { p r i n t $2 } ’ $ f i l e _ l i s t ‘ HOST_KO= ‘ awk ’ / [ . ] / { p r i n t $2 } ’ $ f i l e _ l i s t echo " h o s t OK" f o r h o s t i n $HOST_OK ; do echo $host check_tcp −H $host − t 1 −p 10001 done echo " h o s t KO" f o r h o s t i n $HOST_KO ; do echo $host check_tcp −H $host − t 1 −p 10001 done Nicolas Ledez Scripting with Sh
  • 28. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptwhile w h i l e [ − f t o t o ] ; do echo Wait sleep 1 done Nicolas Ledez Scripting with Sh
  • 29. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptcontinue f o r i i n ‘ seq 7 ‘ ; do i f [ $ i −eq 5 ] ; then continue fi echo $ i done 1 2 3 4 6 7 Nicolas Ledez Scripting with Sh
  • 30. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptbreak f o r i i n ‘ seq 7 ‘ ; do i f [ $ i −eq 5 ] ; then break fi echo $ i done 1 2 3 4 Nicolas Ledez Scripting with Sh
  • 31. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptuntil u n t i l [ − f t o t o ] ; do echo Wait sleep 1 done Nicolas Ledez Scripting with Sh
  • 32. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptcase case $1 i n # Match t h e f i r s t arg no | yes ) response = 1 ; ; −[ t T ] ) t a b l e =TRUE ; ; ∗) echo " unknown o p t i o n " ; e x i t 1 ; ; esac Nicolas Ledez Scripting with Sh
  • 33. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptname() count ( ) { l s | wc − l } count 13 Nicolas Ledez Scripting with Sh
  • 34. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptalias a l i a s l l = ’ l s −l a t r ’ a l i a s l s = ’ l s −−c o l o r =auto ’ a l i a s omega= ’ cd ~ / p r o j e c t s / omega ’ Nicolas Ledez Scripting with Sh
  • 35. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptexit i f [ $# −eq 0 ] then echo " Usage : $0 [−c ] [−d ] f i l e ( s ) " 1>&2 exit 1 # Error status fi Nicolas Ledez Scripting with Sh
  • 36. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptgetopts w h i l e g e t o p t s " hzgc : " Option ; do case $Option i n h ) usage ; e x i t 0 ; ; c ) COPY=1 ; HOST=$OPTARG ; ; g ) GENERATE=1 ; ; ∗ ) echo " Unimplemented o p t i o n chosen . " exit 3 ;; esac done shift ‘ expr $OPTIND − 1 ‘ Nicolas Ledez Scripting with Sh
  • 37. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptecho user@myhost : ~ $ echo $LC_ALL en_US . UTF−8 user@myhost : ~ $ echo " $LC_ALL " en_US . UTF−8 user@myhost : ~ $ echo ’ $LC_ALL ’ $LC_ALL Nicolas Ledez Scripting with Sh
  • 38. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptexport et eval $ command=echo $ e x p o r t command $ parameters =" d i f f e r e n t parameters " $ e x p o r t parameters $ e v a l $command $parameters d i f f e r e n t parameters Nicolas Ledez Scripting with Sh
  • 39. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptnohup nohup - run a command immune to hangups, with output to a non-tty $ nohup l s nohup : appending o u t p u t t o ‘ nohup . out ’ $ c a t nohup . o u t ssh−GKxbwc9627 ssh−kXMoX18526 ssh−tEcuR24939 Nicolas Ledez Scripting with Sh
  • 40. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptread $ read f i r s t l a s t address Sarah C a l d w e l l 123 Main S t r e e t $ echo " $ l a s t , $ f i r s t n$address " C a l d w e l l , Sarah 123 Main S t r e e t Nicolas Ledez Scripting with Sh
  • 41. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptreturn Exit the function with status n or with the exit status of the previously executed command. return Nicolas Ledez Scripting with Sh
  • 42. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptset s e t −vx # Read each command l i n e ; # show i t ; execute i t ; # show i t again # ( w i t h arguments ) s e t +x # Stop command t r a c i n g s e t −o n o c l ob b e r # Prevent f i l e o v e r w r i t i n g s e t +o n o c l o b b e r # Allow f i l e o v e r w r i t i n g # again Nicolas Ledez Scripting with Sh
  • 43. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptshift Shift positional arguments (e.g., 2becomes1). If n is given, shift to the left n places. Used in while loops to iterate through command-line arguments. $ shift $ shift 2 Nicolas Ledez Scripting with Sh
  • 44. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - File Conditions 1/2 −b f i l e file e x i s t s and i s a b l o c k s p e c i a l f i l e . −c f i l e file e x i s t s and i s a c h a r a c t e r s p e c i a l f i l e . −d f i l e file e x i s t s and i s a d i r e c t o r y . −f f i l e file e x i s t s and i s a r e g u l a r f i l e . −g f i l e file e x i s t s , and i t s set−group−i d b i t i s s e t . −k f i l e file e x i s t s , and i t s s t i c k y b i t i s s e t . Nicolas Ledez Scripting with Sh
  • 45. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - File Conditions 2/2 −p f i l e f i l e e x i s t s and i s a named p i p e ( f i f o ) . −r f i l e f i l e e x i s t s and i s r e a d a b l e . −s f i l e f i l e e x i s t s and has a s i z e g r e a t e r than zero . −t [ n ] The open f i l e d e s c r i p t o r n i s a s s o c i a t e d −u f i l e f i l e e x i s t s , and i t s set−user−i d b i t i s s e t . −w f i l e f i l e e x i s t s and i s w r i t a b l e . −x f i l e f i l e e x i s t s and i s e x e c u t a b l e . Nicolas Ledez Scripting with Sh
  • 46. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - String Conditions string s t r i n g i s not n u l l . −n s1 S t r i n g s1 has nonzero l e n g t h . −z s1 S t r i n g s1 has zero l e n g t h . s1 = s2 S t r i n g s s1 and s2 are i d e n t i c a l . s1 ! = s2 S t r i n g s s1 and s2 are n o t i d e n t i c a l . Nicolas Ledez Scripting with Sh
  • 47. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - Integer Comparisons n1 −eq n2 n1 equals n2 . n1 −ge n2 n1 i s g r e a t e r than o r equal t o n2 . n1 −g t n2 n1 i s g r e a t e r than n2 . n1 −l e n2 n1 i s l e s s than o r equal t o n2 . n1 − l t n2 n1 i s l e s s than n2 . n1 −ne n2 n1 does n o t equal n2 . Nicolas Ledez Scripting with Sh
  • 48. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - Combined Forms ! condition True i f c o n d i t i o n i s f a l s e . c o n d i t i o n 1 −a c o n d i t i o n 2 True i f both c o n d i t i o n s are t r u e . c o n d i t i o n 1 −o c o n d i t i o n 2 True i f e i t h e r c o n d i t i o n i s t r u e . Nicolas Ledez Scripting with Sh
  • 49. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttest - Examples # While t h e r e are arguments . . . w h i l e t e s t $# −g t 0 # While t h e r e are nonempty arguments . . . w h i l e [ −n " $1 " ] # I f $count i s l e s s than 1 0 . . . i f [ $count − l t 10 ] # I f t h e RCS d i r e c t o r y e x i s t s . . . i f [ −d RCS ] # I f t h e answer i s n o t y . . . i f [ " $answer " ! = " y " ] # I f t h e f i r s t argument i s n o t a # readable f i l e or a r e g u l a r f i l e . . . i f [ ! −r " $1 " −o ! − f " $1 " ] Nicolas Ledez Scripting with Sh
  • 50. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scripttrap Remove a $tmp file when the shell program exits, or if the user logs out, presses CTRL-C, or does a kill : t r a p " rm − f $tmp ; e x i t " 0 1 2 15 Nicolas Ledez Scripting with Sh
  • 51. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptwait ( / b i n / d f / path / n f s ) & cmdpid=$ ! t i m e o u t =10 ( sleep $timeout echo " d f $1 check f a i l e d " ( k i l l −9 $cmdpid ) > / dev / n u l l 2>&1 ) & watchdogpid=$ ! w a i t $cmdpid k i l l $watchdogpid >/ dev / n u l l 2>&1 Nicolas Ledez Scripting with Sh
  • 52. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptunset $ echo $ t o t o $ t o t o =qsdf $ echo $ t o t o qsdf $ unset t o t o $ echo $ t o t o $ Nicolas Ledez Scripting with Sh
  • 53. Syntax Base Variables Boucle Built-in Commands Fonction Awk, Sed et Co. Scriptfilename Put filename in script to launch it Nicolas Ledez Scripting with Sh
  • 54. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Awk awk ’ { p r i n t $1 } ’ a− f i l e awk ’ { p r i n t $1 " ; " $4 } a− f i l e awk ’ { FS = " ; " } / ^ ’ $ {HOST } ’ / { p r i n t $2 } ’ a− f i l e awk −F ’ | ’ ’ { p r i n t $2 } ’ a− f i l e awk ’ / [ $ {PATTERN } ] / { p r i n t $2 } ’ a− f i l e awk ’ { i f ( $1 == t b s p ) { p r i n t $2 } } ’ t b s p =" $ {TBSP } " a− f i l e awk ’ { FS = " : " } / ^ [ ^ # ∗ ] / { p r i n t $1 } ’ a− f i l e awk ’ BEGIN { FS = " : " } $6 ~ / ^ / $ / { p r i n t " I n / : " , $1 } ’ / e t c / passwd awk ’ $3 ~ / : / { p r i n t $1 } ’ a− f i l e Nicolas Ledez Scripting with Sh
  • 55. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Sed sed ’ s / ^ / XX / g ’ sed − f $ {MEGASED} $ { f i l e } sed " s / #ENV# / $ { env } / g " sed ’ s / ^KO / / ; s / KO$ / / ’ Nicolas Ledez Scripting with Sh
  • 56. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Autres 1/X c a t << "EOF" | ssh $1 / b i n / sh − ps −e d f −o comm, args | grep [ h ] t t p d | s o r t −u $ {ORACLE_HOME } / b i n / s q l p l u s " / as sysdba " << EOF s p o o l $ {ORACLE_BASE } / admin / $ { ORACLE_SID } / c r e a t e / s c o EOF c a t << EOF | ssh $1 / b i n / sh − chown −R $ {ADMCTS_NAME } : $ {ADMCTS_GROUP} $ {HOMEDIR} EOF Nicolas Ledez Scripting with Sh
  • 57. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Autres 2/X expect << EOF spawn ssh − t $1 passwd $ {ADMCTS_NAME} expect "New Password : " send " $ {ADMCTS_PASSWD } r " expect " Re−e n t e r new Password : " send " $ {ADMCTS_PASSWD } r " expect e o f EOF Nicolas Ledez Scripting with Sh
  • 58. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Autres 3/X c a t << "EOF" | ssh $1 / b i n / bash − SITES = / s i t e s i f [ −d $SITES ] ; then cd $SITES f o r s i t e i n ∗ ; do NB_PROC= ‘ ps −e d f | grep $ s i t e | grep −vc grep ‘ i f [ $NB_PROC −eq 0 ] ; then echo " $ s i t e m i s s i n g " fi done fi Nicolas Ledez Scripting with Sh
  • 59. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Autres 4/X c a t << EOF > $ { SED_FILE } s %172.30.47.11.∗ sapome01 . ∗ # Front −End%172.30.156.142 sapome01% s %172.30.47.14.∗ sapome04 . ∗ # Front −End%172.30.156.144 sapome04% EOF c a t << EOF | ssh $1 / b i n / bash − | t e e r e p o r t / $1 echo ’ uname −a ’ uname −a echo EOF Nicolas Ledez Scripting with Sh
  • 60. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Conclusion Conclusion Nicolas Ledez Scripting with Sh
  • 61. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Bibliography UNIX in a Nutshell Ed. O’Reilly & Associates Nicolas Ledez Scripting with Sh
  • 62. Syntax Awk Variables Sed Built-in Commands Autres Awk, Sed et Co.Questions Questions ? Nicolas Ledez Scripting with Sh

×