Upload
vunguyet
View
224
Download
0
Embed Size (px)
Citation preview
Quotation Exercise
1. Printoutyour$LOGNAME2. Printdate3. Print`whoami`4. Printyourcurrentdirectory
Basic Shell Scripting 2
Quotation Exercise
#!/bin/bash
echo "Hello, $LOGNAME"echo "Current date is `date`"echo "User is `who i am`"echo "Current directory `pwd`”
Basic Shell Scripting 3
Number Exercise
1. a=5.66;b=8.672. Printoutsumofa+b3. z=54. Printoutresultofz+5
Basic Shell Scripting 4
Number Exercise
#!/bin/bash
a=5.66b=8.67c=`echo $a + $b | bc`echo "$a + $b = $c"
z=5z=`expr $z + 3`echo "z=$z"z=$(($z+5))echo "z=$z”
5 Basic Shell Scripting
Loop Exercise
1. Loopthroughalistofplanets(MercuryVenusEarthMarsJupiterSaturnUranusNeptunePluto)
2. Printoutlistelementonebyone
Basic Shell Scripting 6
Loop Exercise
#!/bin/bash
for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Plutodo echo $planetdone
7 Basic Shell Scripting
Function Exercise
1. Createa“hello”funcVonrequiringanameasparameterandprintout“helloname”
2. CallthefuncVontwicewithdifferentparameters
Basic Shell Scripting 10
Function Exercise
#!/bin/bash# Passing arguments to a functionhello () { echo Hello $1}hello John hello James
11 Basic Shell Scripting
grep & egrep • grep: Unix utility that searches through either information piped to it
or files. • Usage: grep <options> <search pattern> <files> • Options:
-i ignore case during search -r,-R search recursively -v invert match i.e. match everything except pattern -l list files that match pattern -L list files that do not match pattern -n prefix each line of output with the line number within its input file. -A num print num lines of trailing context after matching lines. -B num print num lines of leading context before matching lines.
Basic Shell Scripting 12
grep Examples • Search files containing the word bash in current directory
• Repeat above search using a case insensitive pattern match and print line number that matches the search pattern
grep bash *
grep -in bash *
• Search files NOT containing the word bash in current directory grep -v bash *
Basic Shell Scripting 13
grep Examples
• grep OR : find people either manager or in sales dept
100ThomasManagerSales$5,000200JasonDeveloperTechnology$5,500300RajSysadminTechnology$7,000500RandyManagerSales$6,000
grep ‘Manager\|Sales’ employee.txt -> 100ThomasManagerSales$5,000
500RandyManagerSales$6,000
• grep AND: find people who is both sysadmin and in Tech dept
grep –i ‘sysadmin.*Technology’ employee.txt -> 100300RajSysadminTechnology$7,000
Basic Shell Scripting 14
sed commands and flags Flags Operation Command Operation -e combine multiple
commands s substitution
-f read commands from file g global replacement -h print help info p print -n disable print i ignore case -V print version info d delete -r use extended regex G add newline
w write to file x exchange pattern with hold
buffer h copy pattern to hold buffer ; separate commands
Basic Shell Scripting 15
sed Examples (1)
• Add flag -e to carry out multiple matches. • Replace bash with tcsh and Frist with Second
• Alternate form with ; instead of -e
• The default delimiter is slash (/), try : in place of /
cat hello.sh | sed -e ’s/bash/tcsh/g’ -e ’s/First/Second/g’
sed ’s/bash/tcsh/g; s/First/Second/g’ hello.sh
sed ’s:/bin/bash:/bin/tcsh:g’ hello.sh
Basic Shell Scripting 17
sed Examples (2)
• Delete blank lines from a file
• Delete line n through m in a file
sed ’/^$/d’ hello.sh
sed ’2,4d’ hello.sh
Basic Shell Scripting 18
awk Syntax awk pattern {action} pattern decides when action is performed Actions:• Most common action: print • Print file dosum.sh:
awk ’{print $0}’ dosum.sh
• Print line matching files in all .sh files in current directory:
awk ’/bash/{print $0}’ *.sh • $0 Print the entire line, use. • NR #records (lines)
• NF #fields or columns in the current line. • By default the field delimiter is space or tab. To change the field
delimiter use the -F<delimiter> command.
Basic Shell Scripting 19
uptime 11:18am up 14 days 0:40, 5 users, load average: 0.15, 0.11, 0.17 Use awk to print out the entire fields of uptime results uptime | awk ’{print $0}’ Print out current time and #fields uptime | awk ’{print $1,NF}’ 11:18am 12 Print out # of users uptime | awk –F, ’{print $1}’ 5 users
Basic Shell Scripting 20
AwkExamples