Shell Scripts - MIT

[Pages:24]Shell Scripts

Jacob Morzinski jmorzins@mit.edu



2006-Jan-23

1

Why use shell scripts?

They simplify your life. Store complicated commands in a script, to

save effort and reduce typing errors.

athrun ops rdesktop -ujmorzins -f ?N \ -a16 vash.mit.edu:4884

ldapsearch -u -LLL -h ldap.mit.edu \ -b dc=mit,dc=edu uid=jmorzins 2>&1 | \ egrep -v '^(SASL[ /]|objectClass:)'

2006-Jan-23

3

Basic: setting up a script

1. Edit the file.

Emacs, vi, jedit, gedit, whatever you prefer. Run with: bash /path/to/file

2. (Make Unix happy.)

Make sure the file begins with #!/bin/bash chmod a+rx file Make sure file is in your $PATH (or $path).

3. Run the script.

file (or /path/to/file )

2006-Jan-23

4

Basic: making Unix happy

The #! line tells Unix what program to run the file through. We want to use bash.

The chmod command tells Unix to make the file readable and executable.

Your $PATH is a list of directories that Unix looks through, when trying to find a program whose name matches the word you just typed at the prompt.

2006-Jan-23

5

Basic: setting the PATH

If your shell is sh-based (like bash):

PATH="$PATH":/mit/jmorzins/bin export PATH

Sometimes you can combine these:

export PATH="$PATH":/mit/jmorzins/bin

If your shell is csh-based (like tcsh):

set path = ( $path:q /mit/jmorzins/bin )

2006-Jan-23

6

Basic: fixed-text example

$ cat user1 #!/bin/sh vos exa user.jmorzins kvno jmorzins $ ./user1 user.jmorzins

APHRODITE.MIT.EDU /vicepb Creation Wed Aug 30 09:50:43 1995 jmorzins@ATHENA.MIT.EDU: kvno = 20

2006-Jan-23

7

Basic: variables allow flexebility

$ cat user2 #!/bin/sh vos exa user."$1" kvno "$1" $ ./user2 boojum user.boojum

COCYTUS.MIT.EDU /vicepb Creation Sun Aug 10 00:08:35 1997 boojum@ATHENA.MIT.EDU: kvno = 78

2006-Jan-23

8

Review of basic knowledge

At this point, you know:

How to set up simple shell scripts How to pass one word into the script, to have the

script run commands using that word ("$1").

2006-Jan-23

9

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

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

Google Online Preview   Download