Front | Info | Lists | Newsfeeds | Study Guide | What is BSD? |
|
This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at http://bsdwiki.reedmedia.net/wiki/Create_a_simple_Bourne_shell_script.html. Create a simple Bourne shell scriptConceptMost system administration tasks can be automated with shell scripts. Be aware of the advantages and disadvantages of using a Bourne shell script rather than a csh(1) or bash(1) shell script. Be able to recognize a shebang, comments, positional parameters and special parameters, wildcards, the proper use of quotes and backslashes and: for, while, if, case, and exec. In addition, know how to make a script executable and how to troubleshoot a script. IntroductionChoosing a shell interpreter: the Shebang thingA shell script is a kind of executable that has to be interpreted. Even if computers may be seen as more and more clever, they cannot (for now) deduce from the content of a file set as executable the language of the script, nor can they deduce the type of the shell needed (sh syntax is slightly different from csh syntax). That's why you must declare the interpreter your script will use. To do that, your script must always begin with the line :
For example :
or
But you may also encounter awk, python, perl, ... In free software, choice matters. You may think bash is a killer app that everybody needs, but it is a fact that it is not shipped with all BSD (at least FreeBSD).
So what happens when you launch a script? You always launch a script from the command line, so you're basically using your shell interpretor (say, sh). It will look at the file, to guess if it is a well-known executable. If not, it will parse the first line of your script, thinking that it may find the shebang and the interpretor needed. The shebang is always the first line. Why you would want to use sh? (and the drawbacks of such a decision)The bourne shell was written by Stephen R. Bourne. It was designed to replace the Thompson Shell. Why would you want to use this shell?
What are the drawbacks ? what are the disadvantages against a csh or a bash script ? (Actually, I don't see any. Maybe less functionnalities that bash. But bash has less functionnality than zsh. Csh/tcsh also may seem more easy to learn, as it is close to C, but full of bugs) Core shell programmingParametersParameters are really easy to get in shell scripting. Arguments are numbered beginning from 0, and are refered by $n, $0 being the script name, and $1 the first argument. As an example, consider this script:
Guess what the print is ;) Other possibilities you may want to know are $#, which print the number of parameters and $*, which will list all parameters VariablesParameters are only a special type of variables, in the sense that they are defined at the very beginning, thanks to command line. So you may guess that variables will have the same syntax than parameters: $something is a variable named "something". However, to affect a variable, the syntax is slightly different: something=foo without spaces before and after the sign equals. Be aware that writing "=foo" will affect the value "foo" to $something. However, if you want the value of $something to be the value of foo, you will have to put $foo. Also remember that it is not a pointer: affecting 2 to $foo, then $foo to $something and then 3 to $foo will give the following result: $foo = 3 and $something = 2. You may sometimes need to make some formatted output: for example, if you have $a = var, and you want to write "varb", writing $ab won't do it... In this case, you'll have to write ${a}b. You may have to void some characters, like if you want to print "\n" without making a return to new line. This is the same as all other languages: \ will void next character, "..." will void everything but \ $ and Finally, two last special variables may be useful: $$ is the PID of the last command, and $? is its status. Testing a valueBefore learning more on conditional branching and loops, you may want to know more on tests. For example, how do we know that a specific file exists? This is done by using the command "test expr" or "[ expr ]" (be careful: the spaces are needed), where expr can be:
Shell script also provides "non" (!), logical and (-a) and logical or (-o) Usually, true is 0... Conditional branchingSh has 2 kinds of conditionnal branching: if and case Here is the structure of if...
If you're familiar with programming languages, be careful here. The "parameter" for if is a command, and this has a few implications: first, usually, a success of the command would yield a "true", but you have to make sure of that by reading the man. Second, putting "!cmd" won't be true if the command fails: the ! is for the result of the command, not it's status. The "if" branching begins with keyword "if", and ends with keyword "fi". Here is an example of the case:
where You may want to catch every uncatch possibility by adding a choice "*" (the wildcard). Also, do not forget the esac at the end. LoopsSh has three kinds of loops: for, while, until. Here is the syntax of for:
If a list is provided, the parameter will take the values in the list. Else, it will use the command-line parameters as the list. The while syntax is:
and loop while command status is true. Finally, the until loop:
FunctionsDefining a function is done by specifying a name, ending with parenthesis, and putting the code of the function between { and }. For example:
or
Now, you can call your function by putting its name (without $ and ()). Parameters for functions work exactly as parameters for scripts:
and calling
would say hello to the person whose name is the second argument in the command line. Miscwildcards: the star . It will replace any number of any characters, e.g. ".txt" will match any file whose name ends by the substring ".txt". For example, to copy all the .sh files from /tmp to the /root/ directory : "cp /tmp/*.sh /root/" comments: comments are the lines beginning with the symbol # (exception is made of the shebang, in the first line) If you have to do some maths, expr will make them for you. Syntax :
For example: expr $a + $b There is a few op possible, like \*, /, %, =, \>... It is also possible to do other things:
Practice Exercises
Will print "Hello World" Now, you may want to try to write a script which says "hello bill" if the first argument is bill, else say hello world. (hint: think of case) You may want to make a program that will print the number of word of each file in a directory (remember that More informationsh(1), chmod(1) http://steve-parker.org/sh/sh.shtml http://www.grymoire.com/Unix/Sh.html
|