DOC PREVIEW
FSU COP 4342 - Shell Programming Topics

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fall 2006 Shell programmingShell Programming Topics☞ Creating Shell Scripts☞ Globbing☞ Aliases, Variables/Arguments, and ExpressionsCOP 4342Fall 2006 Shell programmingShell Programming Topics☞ Shells, data, and debugging☞ Structuring control flow☞ Exit statusCOP 4342Fall 2006 Shell programmingShell Programming Topics☞ Not (just) globbing: regular expressions➳ grep, awk, perl all use regular expressionsCOP 4342Fall 2006 Shell programmingAdvantages of shell scripts☞ Can very easily automate a group of tasks, especiallythose with i/o that are related☞ Can very easily leverage powerful Unix toolsCOP 4342Fall 2006 Shell programmingDisadvantages of shell scripts➳ Shell scripts execute slowly.➳ Advanced programming techniques aren’t a feature ofshell programming. Abstraction and encapsulation arepoorly supported.COP 4342Fall 2006 Shell programmingWhat shell to use☞ For programming, most people have preferred sh andits derivatives such as bash.☞ We will use bash for programming, although we w illalso talk about csh when appropriate in command shells.COP 4342Fall 2006 Shell programmingWhat shell to use☞ In the past, many people have preferred csh and tcshas command line shells; however, it appears that bashis now preferred since its support for command lineediting is quite strong and it also is quite useful for shellprogramming.COP 4342Fall 2006 Shell programmingWhat shell to use☞ There is also program busybox which is also worthknowing about. It is a shell — and a lot more.The binary itse lf includes many other programs such ashead, tail, ps, top, find, crontab, and tar asbuilt-ins.COP 4342Fall 2006 Shell programmingFinding more information☞ man bash☞ man {alias, bg, bind, break, builtin, cd, command,compgen, ...}☞ info bash☞ Google bashCOP 4342Fall 2006 Shell programmingCreating a script☞ By convention, we use an extension of .sh for shellscripts.☞ The first line needs to be#!/bin/bash#!/bin/sh#!/bin/csh#!/sbin/bashCOP 4342Fall 2006 Shell programmingCreating a script☞ Now you should put some comments:# 2006 09 06 -- original version by rdl# 2006 09 07 -- updated ‘‘text’’ by rdl## this shell program is used to confabulate the obfuscated#COP 4342Fall 2006 Shell programmingUsing echo☞ The program (and builtin) echo is useful for sending agiven string or strings to stdout.[langley@sophie 2006-Fall]$ echo a b ca b c[langley@sophie 2006-Fall]$ echo "a b c"a b c[langley@sophie 2006-Fall]$ echo "$SHELL a b c"/bin/bash a b c[langley@sophie 2006-Fall]$ echo $SHELL a b c/bin/bash a b c[langley@sophie 2006-Fall]$ echo ’$SHELL a b c’$SHELL a b cCOP 4342Fall 2006 Shell programmingShell variables☞ Do not have to be declared: just use them. (If youwant to, you can declare them with declare; generallyonly useful to make variables read-only.)☞ Can be assigned a value, or can just have a blank value☞ Can dereferenced with a “$”COP 4342Fall 2006 Shell programmingShell variablesExamples:[langley@sophie 2006-Fall]$ a=b[langley@sophie 2006-Fall]$ b=$a[langley@sophie 2006-Fall]$ echo "a = $a , b = $b"a = b , b = bCOP 4342Fall 2006 Shell programmingreading values from the command lineFrom the man page for bash:‘‘One line is read from the standard input, . . . and thefirst word is assigned to the first name, the second word to thesecond name, and so on, with leftover words and their interven-ing separators assigned to the last name. If there are fewerwords read from the input stream than names, the remaining namesare assigned empty values. The characters in IFS are used tosplit the line into words.’’COP 4342Fall 2006 Shell programmingread example[langley@sophie 2006-Fall]$ read a b c d e fapple beta cherry delta eta figs and more[langley@sophie 2006-Fall]$ echo "$a -- $b -- $c -- $d -- $e -- $f"apple -- beta -- cherry -- delta -- eta -- figs and moreCOP 4342Fall 2006 Shell programmingread exampleIt is also good to note that you can also specify thatitems are to go into an array rather than just individuallynamed variables with the -a ARRAYNAME option.For example:[langley@sophie 2006-Fall]$ read -a arra b c d e f g h[langley@sophie 2006-Fall]$ for i in 0 1 2 3 4 5 6 7> do> echo ${arr[$i]} # note the odd syntax to deref!> doneaCOP 4342Fall 2006 Shell programmingbcdefghCOP 4342Fall 2006 Shell programmingCommand line parameters☞ When you call a shell script, comm and line parame tersare automatically setup with $1, $2, etc...[langley@sophie 2006-Fall]$ ./Script1.sh abc def ghifirst 3 args: ’abc’ ’def’ ’ghi’☞ $0 refers to the name of the command (the first ite m)COP 4342Fall 2006 Shell programmingMore on command line arguments☞ $# refers to the number of com mand line arguments.☞ $@ refers to the all of the command lines arguments inone string.Example:[langley@sophie 2006-Fall]$ ./Script2.sh abc def ghi jklThere are 4 arguments: abc def ghi jklCOP 4342Fall 2006 Shell programmingDebugging tips☞ The options -x and -v are very helpful. You can eitheradd them to the initial #! line, or you can call the shellat the command line:☞ bash -xv Script1.sh abc defExample:[langley@sophie 2006-Fall]$ bash -xv Script1.sh ls asd asdf asdf#!/bin/bashCOP 4342Fall 2006 Shell programming# 2006 09 06 -- Small test scriptecho "first 3 args: ’$1’ ’$2’ ’$3’"+ echo ’first 3 args: ’\’’ls’\’’ ’\’’asd’\’’ ’\’’asdf’\’’’first 3 args: ’ls’ ’asd’ ’asdf’echo "cmd: ’$0’"+ echo ’cmd: ’\’’Script1.sh’\’’’cmd: ’Script1.sh’[langley@sophie 2006-Fall]$ bash -x Script1.sh ls asd asdf asdf+ echo ’first 3 args: ’\’’ls’\’’ ’\’’asd’\’’ ’\’’asdf’\’’’first 3 args: ’ls’ ’asd’ ’asdf’+ echo ’cmd: ’\’’Script1.sh’\’’’cmd: ’Script1.sh’COP 4342Fall 2006 Shell programmingTesting☞ You can test with square brackets:$ [ $ -e /etc/hosts $ ] $☞ You can also test with test:test -e /etc/hostsCOP 4342Fall 2006 Shell programmingTestingExample:[langley@sophie 2006-Fall]$ if test -e /etc/hosts> then> echo exists> fiexists[langley@sophie 2006-Fall]$ if [ -e /etc/hosts ]> then> echo exists> fiexistsCOP 4342Fall 2006 Shell programmingFile testing conditionsYou can readily check various file status ite ms :[ -d DIR ] # True if directory DIR exists.[ -e SOMETHING ] # True if file or directory SOMETHING exists.[ -f FILE ] # True if regular file FILE exists.[ -r SOMETHING ] # True if file or directory SOMETHING exists and


View Full Document

FSU COP 4342 - Shell Programming Topics

Download Shell Programming Topics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Shell Programming Topics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shell Programming Topics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?