Shell Programming - Western University

Shell Programming

Shell Scripts (1)

u Basically, a shell script is a text file with Unix commands in it.

u Shell scripts usually begin with a #! and a shell name

? For example: #!/bin/sh ? If they do not, the user's current shell will be used

u Any Unix command can go in a shell script

? Commands are executed in order or in the flow determined by control statements.

u Different shells have different control structures

? The #! line is very important ? We will write shell scripts with the Bourne shell (sh)

Shell Scripts (2)

uWhy write shell scripts?

? To avoid repetition: vIf you do a sequence of steps with standard Unix commands over and over, why not do it all with just one command?

? To automate difficult tasks: vMany commands have subtle and difficult options that you don't want to figure out or remember every time.

A Simple Example (1)

u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg < file1 > file2

? "encrypts" file1 into file2

u Record this command into shell script files:

? myencrypt #!/bin/sh tr abcdefghijklmnopqrstuvwxyz \

thequickbrownfxjmpsvalzydg ? mydecrypt #!/bin/sh tr thequickbrownfxjmpsvalzydg \

abcdefghijklmnopqrstuvwxyz

A Simple Example (2)

uchmod the files to be executable; otherwise, you couldn't run the scripts

obelix[3] > chmod u+x myencrypt mydecrypt

uRun them as normal commands:

obelix[4] > ./myencrypt < file1 > file2 obelix[5] > ./mydecrypt < file2 > file3 obelix[6] > diff file1 file3

Remember: This is needed when "." is not in the path

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download