230 likes | 383 Vues
Shell Scripts. Jacob Morzinski jmorzins@mit.edu http://sipb.mit.edu/iap/2006/shell/ http://web.mit.edu/sipb-iap/www/2006/shell/. Why use shell scripts?. They simplify your life. Store complicated commands in a script, to save effort and reduce typing errors.
E N D
Shell Scripts Jacob Morzinski jmorzins@mit.edu http://sipb.mit.edu/iap/2006/shell/ http://web.mit.edu/sipb-iap/www/2006/shell/
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:)'
Basic: setting up a script • Edit the file. • Emacs, vi, jedit, gedit, whatever you prefer. • Run with: bash /path/to/file • (Make Unix happy.) • Make sure the file begins with #!/bin/bash • chmod a+rx file • Make sure file is in your $PATH (or $path). • Run the script. • file (or /path/to/file )
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.
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 )
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
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
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").
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.
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.
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.
File globs • If files match your pattern, the file names are filled into the command line.
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.
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.
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
Setting Variables • Setting variables • var=value • ENVVAR=value • export ENVVAR • Note: • variables don’t affect parent shells • only environmental variables affect child shells
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
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 “]”
Utilities – grep • Pattern searching • Uses regular expressions • Powerful, but hard to learn
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
Shell Loops – while, for • while expr ; do cmd ; done • for var in a b c d e ; do cmd ; done
Shell functions • name () { cmd1 ; cmd2 “$@” ; } • Act like mini-shell scripts. • Can set variables in the current shell.