Running a shell script

After you've created a script in your editor and saved the script file, your next step is to run it, which means to execute every command in the script in the order provided. (Yes, you did this in the last section, but we'll expand on it here.) As Figure 10.2 shows, you do this using the sh command (or the name of the shell you're using), followed by the name of the shell script you want to run.

Figure 10.2. Running a script is as easy as typing sh plus the file name of the script.


To run a script:

  • sh myscript

    At the shell prompt, type sh (or the name of the shell, like ksh or csh, you want to run the script), followed by the name of the script. In this case, you're really just telling sh to run and to use the list of commands in the myscript file. You'll see the results of the script—in this case, words appear onscreen as shown in Figure 10.2.

Tip

Note that in this example, you're explicitly telling UNIX the name of the script to run (myscript). When you do so, the #! /bin/sh line at the top of the script in the previous section is technically superfluous. It's only essential when the script is executable, as in the following section.


Code Listing 10.2. After a little one-time preparation, you can run executable scripts by typing the script name at the shell prompt.
[ejr@hobbes scripting]$ head -2 myscript
#! /bin/sh
# This is my first shell script
[ejr@hobbes scripting]$ chmod u+x myscript
[ejr@hobbes scripting]$ pwd ; echo $PATH
/home/ejr/scripting
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
   /usr/local/games:/home/ejr/bin:/home/ejr/
   scripting
[ejr@hobbes scripting]$ myscript
friendsjustfriends

        
       standing
       good
[ejr@hobbes scripting]$

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.33.41