introduction to shell scripts - microsoft...

Post on 29-Mar-2020

15 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Copyright 2006 Stewart WeissCopyright 2009 Stewart Weiss

Scripting

More Shell Scripts Loops

Adapted from Practical Unix and ProgrammingHunter College

2 Comp 190 Scripting Languages

Loops: for

The for script version 1:

1 #!/bin/bash 2 for i in 1 2 3 3 do 4 echo $i 5 done

Line numbers are part of the editor, not the script!

3 Comp 190 Scripting Languages

Loops: for

The for script version 2:

1 #!/bin/bash 2 for file in temp1.txt temp2.txt temp3.txt 3 do 4 more $file 5 done

Write a script that removes all lines containing the word "October" from the three files.

Comp 190 Scripting Languages

Loops: for

The for script version 2:

1 #!/bin/bash 2 for file in temp1.txt temp2.txt temp3.txt 3 do 4 grep -v "October" $file > tempfile 5 mv tempfile $file 6 done

4 Comp 190 Scripting Languages

Loops: for

The for script version 2:

1 #!/bin/bash 2 for file in temp1.txt temp2.txt temp3.txt 3 do 4 grep -v "October" $file > tempfile 5 mv tempfile $file 6 done

Change your script to read the search word from the command line.

5 Comp 190 Scripting Languages

Loops: for

The for script version 3 (using regex):

1 #!/bin/bash 2 for file in temp[1-3].txt 3 do 4 grep -v "October" $file > tempfile 5 mv tempfile $file 6 done

The shell permits filename substitution

6 Comp 190 Scripting Languages

Loops: for

The for script version 4:

1 #!/bin/bash 2 for file in * 3 do 4 grep -v "October" $file > tempfile 5 mv tempfile $file 6 done

This would remove the word "October" from ALL files in the directory.

7 Comp 190 Scripting Languages

Loops: for

The for script version 5:

1 #!/bin/bash 2 files=$(cat fileList) 3 for file in $files 4 do 5 grep -v "October" $file > tempfile 6 mv tempfile $file 7 done

This would remove the word "October" from the files whose name is contained in the file "fileList".

8 Comp 190 Scripting Languages

Loops: for

The for script version 6: 1 #!/bin/bash 2 # delete "October" lines from all files on the command line 3 # $* contains all command line parameters 4 for file in $* 5 do 6 grep -v "October" $file > tempfile 7 mv tempfile $file 8 done

This would remove the word "October" from ALL files listed on the command line.

9 Comp 190 Scripting Languages

Loops: for

The for script version 7: 1 #!/bin/bash 2 # echo comand line args 3 # $* contains all command line parameters 4 for arg in $* 5 do 6 echo $arg 7 done

Try this with command line args:args a b c # where 'args' is the script name args 'a b' c # $* is replaced by the shell with

# a b c and the quotes are lost!

10 Comp 190 Scripting Languages

Loops: for

The for script version 8: 1 #!/bin/bash 2 # echo comand line args 3 # $@ contains all command line parameters 4 for arg in $@ 5 do 6 echo $arg 7 done

Try this with command line args:args a b c # where 'args' is the script name args 'a b' c # $@ quotes each arg, so "a b" "c"

11 Comp 190 Scripting Languages

Loops

breaking out of loops: 1 while true 2 do 3 cmd=$(getcmd) 4 5 if [ "$cmd" = quit ] 6 then 7 break 8 else 9 echo "$cmd" 10 fi 11 done

break n will break out of the n innermost loops

12 Comp 190 Scripting Languages

Loops

continuing loops:

for file in $* do

if [ ! -e "$file" ]then

echo "$file not found!"continue

fi# process file

13 Comp 190 Scripting Languages

Loops

I/O redirection on a loop:

for i in 1 2 3 4do echo $idone > loopout

puts the output of the loop into the file "loopout"

14 Comp 190 Scripting Languages

Loops

Overridding I/O redirection on a loop:

for i in 1 2 3 4do echo $i > /dev/ttydone > loopout

/dev/tty is the terminal. This script explicitly sends output to the terminal.

15 Comp 190 Scripting Languages

Loops

I/O redirection on standard error:

for i in 1 2 3 4do echo $idone 2> errors

2> redirects stderr not stdout

16 Comp 190 Scripting Languages

Loops

piping:

for i in 1 2 3 4do echo $idone | wc -l

pipe output to the wordcount programThis prints out "4"

17 Comp 190 Scripting Languages

&& and || constructs in the shell

&&:

command1 && command2

command1 is always executedif command1 returns an exit status of zero (success) then command2 is executed.otherwise, command2 is NOT executed

sort bigdata > /tmp/sortout && mv /tmp/sortout bigdata

[ -z "$EDITOR" ] && EDITOR=/bin/ed

tests the value of the variable EDITOR. If it's null, /bin/ed is assigned to it.

only executes the mv command if the sort was successful

18 Comp 190 Scripting Languages

&& and || constructs in the shell

||:

command1 || command2

command1 is always executedif command1 returns an exit status of nonzero (failure) then command2 is executed.otherwise, command2 is NOT executed

grep "$name" phonebook || echo "Couldn't find $name"only do the echo if grep doesn't find the name in the phonebook or can't open phonebook

19 Comp 190 Scripting Languages

&& and || constructs in the shell

complex commands:

command1 || command2

You can use a complex sequence of commands on either or both left or right side of a && or || construct.

who | grep "^$name > /dev/null || echo "$name's not logged on"

causes exxecution of the echo if the grep fails

see the textbook for more complex examples

20 Comp 190 Scripting Languages

Things to try

Try creating a few simple scripts of your own. It will give you practice using gedit if you are at a UNIX console, or vi or nano if you are not.

Read about the test command and learn its tricky syntax.

Play around with > to store the output of various commands.

top related