Download - Shell Scripts
![Page 1: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/1.jpg)
2006-Jan-23 1
Shell Scripts
Jacob Morzinski [email protected]
http://sipb.mit.edu/iap/2006/shell/http://web.mit.edu/sipb-iap/www/2006/shell/
![Page 2: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/2.jpg)
2006-Jan-23 3
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:)'
![Page 3: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/3.jpg)
2006-Jan-23 4
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 )
![Page 4: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/4.jpg)
2006-Jan-23 5
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.
![Page 5: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/5.jpg)
2006-Jan-23 6
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 )
![Page 6: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/6.jpg)
2006-Jan-23 7
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
[email protected]: kvno = 20
![Page 7: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/7.jpg)
2006-Jan-23 8
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
[email protected]: kvno = 78
![Page 8: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/8.jpg)
2006-Jan-23 9
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").
![Page 9: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/9.jpg)
2006-Jan-23 10
What the shell really does
You: type a command The shell does:
Variable substitution (interpolation)* File name substitution (globbing)* Parses the line into arguments* Finally, runs the command
* Can be influenced by quoting.
![Page 10: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/10.jpg)
2006-Jan-23 11
Runs the command
Searches through $PATH to find the executable file, if not an absolute path.
You can run a sequence of commands if you separate them with semicolons ( ; )
Commands can return an exit status. Status = 0 means success Status != 0 means some sort of failure
You can use “&&” and “||” to chain commands in an “and” or “or” fashion.
![Page 11: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/11.jpg)
2006-Jan-23 12
Parses the line into arguments The shell splits the command line on white
space. The first word is the command name The rest of the words are “arguments,” and
are passed to the command.
![Page 12: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/12.jpg)
2006-Jan-23 13
File globs
If files match your pattern, the file names are filled into the command line.
Pattern Meaning
* Match zero or more characters
? Matches just one character
[abc] [x-z] Match just one character, from a range
[!0-9] Match one character, that is not in the range
![Page 13: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/13.jpg)
2006-Jan-23 14
Variable substitution
A variable can be a word, a number, or a special symbol: Some symbols: $$, $?, $*, “$@” Numbers 0, and 1-9: $0, $1, …, $9 Words: $PATH, $var, $longer_name
Note that variable substitution happens before file substitution and before the commandline is split into words.
![Page 14: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/14.jpg)
2006-Jan-23 15
Quoting
Single quote: '...' The shell ignores all enclosed characters
Double quote: "..." The shell ignores most enclosed charactecters The shell does interpret $, `, and \
Backtick: `...` Run a command, insert the output
Backslash: \ Special treatment for the following character.
![Page 15: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/15.jpg)
2006-Jan-23 16
Input/Output redirection
Commands usually print to standard output, sometimes print to standard error.
Commands read from standard input.
Stdin is 0 Stdout is 1 Stderr is 2 Higher numbers are possible
![Page 16: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/16.jpg)
2006-Jan-23 17
Table of I/O redirection
cmd1 | cmd2 pipe output from cmd1 into cmd2
commad > file put text into file, truncate file
command >> file append text to file
command < file read from file
command 2>&1 send stderr to where stdout is
command 1>&2 send stdout to where stderr is
cmd1 `cmd2` capture output of cmd2 for command-line of cmd1
![Page 17: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/17.jpg)
2006-Jan-23 18
Setting Variables
Setting variables var=value ENVVAR=value export ENVVAR
Note: variables don’t affect parent shells only environmental variables affect child shells
![Page 18: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/18.jpg)
2006-Jan-23 19
Using variables
$* - list of all command-line parameters. Bad. “$@” – Good list of all command-line
parameters. “$1”, “$2”, …, “$9” – individual command line
parameters shift – remove “$1”, shift all other variables
down one set – set new “$1”, “$2”, etc variables
![Page 19: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/19.jpg)
2006-Jan-23 20
Utilities – test
test – runs a test, returns true or false Good for if statements, or
while loops also called “[”, but in that
case it must be used with a matching “]”
-f is a file
-d is a directory
-z is zero length
-n is non-zero length
=, != compare strings
-a, -o and, or
! negate next part
-gt, -lt-ge, -le
compare numbers
![Page 20: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/20.jpg)
2006-Jan-23 21
Utilities – grep
Pattern searching Uses regular expressions Powerful, but hard to
learn
a letter “a”
. any one char
* zero or more…
[abc][^abc]
char in a range,or inverted
^ start of line
$ end of line
\(z\)\1
save a match,
re-use a match
![Page 21: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/21.jpg)
2006-Jan-23 22
Shell Loops - if
if expr ; then cmd1 ; else cmd2 ; fi cmd1 and cmd2 can be complex the else can be omitted
if [ x“$var” = x“yes” ] ; then echo good ; fi
![Page 22: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/22.jpg)
2006-Jan-23 23
Shell Loops – while, for
while expr ; do cmd ; done
for var in a b c d e ; do cmd ; done
![Page 23: Shell Scripts](https://reader036.vdocuments.us/reader036/viewer/2022062518/568144af550346895db177e0/html5/thumbnails/23.jpg)
2006-Jan-23 24
Shell functions
name () { cmd1 ; cmd2 “$@” ; }
Act like mini-shell scripts. Can set variables in the current shell.