introduction to shell scripts - microsoft...

20
Copyright 2006 Stewart Weiss Copyright 2009 Stewart Weiss Scripting More Shell Scripts Loops Adapted from Practical Unix and Programming Hunter College

Upload: others

Post on 29-Mar-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

Copyright 2006 Stewart WeissCopyright 2009 Stewart Weiss

Scripting

More Shell Scripts Loops

Adapted from Practical Unix and ProgrammingHunter College

Page 2: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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!

Page 3: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.

Page 4: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.

Page 5: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 6: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.

Page 7: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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".

Page 8: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.

Page 9: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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!

Page 10: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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"

Page 11: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 12: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

12 Comp 190 Scripting Languages

Loops

continuing loops:

for file in $* do

if [ ! -e "$file" ]then

echo "$file not found!"continue

fi# process file

Page 13: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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"

Page 14: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.

Page 15: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 16: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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"

Page 17: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 18: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 19: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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

Page 20: Introduction to Shell Scripts - Microsoft Azureclasses.eastus.cloudapp.azure.com/~barr/classes/comp190/...Try creating a few simple scripts of your own. It will give you practice using

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.