1 / 23

Shell Scripts

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.

dugan
Download Presentation

Shell Scripts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Scripts Jacob Morzinski jmorzins@mit.edu http://sipb.mit.edu/iap/2006/shell/ http://web.mit.edu/sipb-iap/www/2006/shell/

  2. 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:)'

  3. 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 )

  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.

  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 )

  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

  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

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

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

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

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

  12. File globs • If files match your pattern, the file names are filled into the command line.

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

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

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

  16. Table of I/O redirection

  17. Setting Variables • Setting variables • var=value • ENVVAR=value • export ENVVAR • Note: • variables don’t affect parent shells • only environmental variables affect child shells

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

  19. 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 “]”

  20. Utilities – grep • Pattern searching • Uses regular expressions • Powerful, but hard to learn

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

  22. Shell Loops – while, for • while expr ; do cmd ; done • for var in a b c d e ; do cmd ; done

  23. Shell functions • name () { cmd1 ; cmd2 “$@” ; } • Act like mini-shell scripts. • Can set variables in the current shell.

More Related