View and vote on the article here: Shell Programming Series (I)
Shell Programming Series (I)| Category | | | Summary | | You may know how to work with the shell interactively at the command line. What you may not have known is that there is a powerful programming language built into the shell. You can use shell programming to do everything from automating a repetitive list |
| | Body | Shell programming is sometimes shunned in favour of Perl or other languages, and thought of as simply ?praised DOS batch programming language,? but this simply is not true. In its simplest form, shell programming is like DOS batch programming in that it can be used to execute a list of commands stored in a file that you could also execute from the command line. But if you take the time to learn hoe to understand the shell, how it interprets commands, you can write highly sophisticated shell programs.
Now let's jump right in and learn how to use this powerful feature of operating systems like FreeBSD, Linux, etc.
A Simple Shell Program
The first rite of passage for any aspiring programmer is to write the ?Hello World!? program. So here is one way to write in the Bourne shell programming language:
<strong>1. #!/bin/sh
2.
3. # The legendary Hello World program
4. # As implemented in the Bourne shell programming language.
5.
6. echo
7. echo ?Hello World!?
8. echo
9. exit 0</strong>
The line numbers in the previous code are for reference in this article only. When you enter the code, do not include the line numbers. Enter the previous text in your favourite text editor, and save it as a file. Next, you will need to make the file executable. You can do this with the following command:
chmod u+x hello
Assuming that you named in file hello, this command will give the owner of the file permission to execute it. Next, execute the file. The following sequence shows the command you type as well as the output of the program:
bash$ ./hello
Hello World!
bash$
Now let's look at each line in the previous program in detail, and see what each line does.
? Line 1: This line contains the magic character sequence #!, which tells the operating system that what follows is a script that should be interpreted by the program named after #!. In this case, it tells the operating system that it should use the Bourne shell interpreter /bin/sh to interpret what follows. If this was a Perl script, we would use /usr/bin/perl in a FreeBSD box.
? Lines 2 and 5: These are simply blank lines. The shell ignores whitespace unless it is quoted. It is a good idea to use whitespace to make your program more readable.
? Lines 3 and 4: These are comments. Comments in a shell program begin with # and extend to the end of the line. They are ignored by the interpreter, and are here for the benefit of users who need to revisit a complicated program they wrote six months ago and try to figure out what it does again.
? Line 6: The echo command takes whatever it receives and echoes it to STDOUT (which is normally the screen). This can be redirected, however, so that echo sends the output somewhere else, like to a file or something. In this case, echo by itself simply prints a blank line on STDOUT.
? Line 7: The echo command here prints the string ?Hello World!? to STDOUT. The quotes tell the interpreter that everything inside the quotes should be interpreted as a single argument. Without the quotes, the shell will interpret the whitespace as an argument separator. In this case, the quotes wouldn't have been strictly necessary .It's a good idea to get into habit of quoting strings such as these.
? Line 8: The echo command here prints a blank line again.
? Line 9: The exit command exits the program and returns an exit status to the invoking program. An exit status of 0 indicates that the program terminated normally. An exit status of anything other than 0 indicates that an error occurred. The exit status can be read by the calling program and used in decision making. The calling program then determines what action it should do next, based on the success or failure of the previous program that was run.
In a program this short, setting the exit status would not have been necessary. If the exit status is not implicitly set, the exit status returned will be the exit status of the last command that was run in the script. It is best to set the exit status, though, since it will become important as you start to write larger and more complex shell programs. Later in these series, you will see how the exit status of a command can be used in a shell script to make automatic decisions about the next action that should be performed. |
|
This article was imported from zZine. (original author: ismail)
There are no replies to this post yet.
|