A shell script is nothing more than a list of commands for Unix to execute. To write a shell script, you'll
Open your favorite editor and start a script file.
Start the shell script with #!/bin/sh.
Add the shell script code one line at a time. This code will look strangely familiar it's similar to code you've already used in this book.
Save and close the file.
In the following steps, we'll show you how to try out this process by writing a script that prints three lines onscreen (Figure 10.1). Yeah, we knowwhoopee!but you have to start somewhere, and you can apply the same principles to other shell scripts you create.
Figure 10.1. You create shell scripts in an editor one line at a time.
To Create a Shell Script:
For starters, access the editor of your choice and start a new file. In this case, we call it myscript.
On the first line of the script, enter #!/bin/sh, which specifies the complete path to the shell that should run the script.
# this is my first shell script
On the next line, type a # (to indicate a comment), and then add any other notes you want to make. It's always a good idea to use extensive comments in your scripts to help you see what's going on. Remember, comments are for your reference only and won't show up onscreen or do anything.
On the next line of the script, type echo followed by the text you want to see onscreen. Here, echo tells Unix to display friendsjustfriends onscreena message just between friends.
Code Listing 10.1. Using echo options, you can get good under standing. Or, perhaps, a good understanding just between friends.
[ejr@hobbes scripting]$ sh myscript friendsjustfriends standing good
Add another line with echo and nothing else to display a blank line.
Add another echo command. Note that if you use leading spaces or tabs, as we've done here, you must use quotes, as Figure 10.1 shows.
echo -e "\tgood"
Using the -e flag plus \t, you can insert a tab character. See Getting Fancy with echo for more echo options.
Save and close your script.
Check Chapter 4 if you need help saving a file and closing your editor.
Use sh myscript to run your new script. In doing so, you get good under standing (literally), as shown in Code Listing 10.1. Ta-daaaaa! You just wrote your first shell script! (See the following section for more information and details on running scripts.)
Getting Fancy with echo
In addition to the basic print-to-screen function that echo offers, you can also use these formatting flags with echo -e (the -e enables these special formatting characteristics):
\b moves the cursor back one space.
\c forces the following text to appear on the same line as the current text.
\f forces the following text to appear on the next line at a specified horizontal location.
\n forces the following text to appear on a new line.
\t indents the following text output by one tab.
For example, echo -e "\tGreetings! \c" would move "Greetings!" one tab space to the right (as specified by the \t) and not insert a new line for any text following it (as specified by the \c).