Presentation is loading. Please wait.

Presentation is loading. Please wait.

2006-Jan-231 Shell Scripts Jacob Morzinski

Similar presentations


Presentation on theme: "2006-Jan-231 Shell Scripts Jacob Morzinski"— Presentation transcript:

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

2 2006-Jan-232 How did you hear about the class? 1. Anne Hunter's list posting 2. the slideshow in the Infinite 3. the goldenrod paper guide

3 2006-Jan-233 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:)'

4 2006-Jan-234 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 )

5 2006-Jan-235 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.

6 2006-Jan-236 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 )

7 2006-Jan-237 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

8 2006-Jan-238 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

9 2006-Jan-239 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" ).

10 2006-Jan-2310 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.

11 2006-Jan-2311 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.

12 2006-Jan-2312 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.

13 2006-Jan-2313 File globs If files match your pattern, the file names are filled into the command line. PatternMeaning *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

14 2006-Jan-2314 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.

15 2006-Jan-2315 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.

16 2006-Jan-2316 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

17 2006-Jan-2317 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

18 2006-Jan-2318 Setting Variables Setting variables  var=value  ENVVAR=value  export ENVVAR Note:  variables don’t affect parent shells  only environmental variables affect child shells

19 2006-Jan-2319 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

20 2006-Jan-2320 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 “]” -fis a file -dis a directory -zis zero length -nis non-zero length =, !=compare strings -a, -oand, or !negate next part -gt, -lt -ge, -le compare numbers

21 2006-Jan-2321 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

22 2006-Jan-2322 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

23 2006-Jan-2323 Shell Loops – while, for while expr ; do cmd ; done for var in a b c d e ; do cmd ; done

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


Download ppt "2006-Jan-231 Shell Scripts Jacob Morzinski"

Similar presentations


Ads by Google