oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

An Introduction to Tiger Terminal, Part 4
Pages: 1, 2, 3, 4, 5

OK, but what does all that content in the script mean? A nice habit to get into when scripting is to add comments describing what you're trying to accomplish and what some of the command lines are aiming to do. You can comment out lines using the # symbol. The shell ignores these lines. As you create more and more scripts, you should include a header that describes the function of the script, your name as author, and the creation and modification dates. Here's what the script would look like with comments:

Figure 11. Adding comments

The #!/bin/bash line at the beginning tells the bash shell program to translate this script, even if your default shell is a different one. Although this line begins with a #, it's not commented out, and it's not ignored by the shell. The #! (called "shebang" for "hash bang" or "sharp bang") is magic: when the OS runs the file, it reads the first few bytes of the file (the "magic number") to find out what kind of file it is. The #! byte pattern means it's a shell script, and the rest of the line (in this case, /bin/bash) indicates which binary should run the script file. For example, if you're using the tcsh shell, the shebang line would be #!/bin/tcsh; if you're using perl, it would be #!/bin/perl.

Let's try running our script:

Figure 12. run

Uh-oh. "Permission denied." If we run ls -l against the file to check permissions, we see this:

Figure 13. Permissions check

Ten points to Gryffindor if you see the solution! In part two, I illustrated the information we get by running the ls -l command. The series of letters (-rw-r--r--) before the file give us the file type (the first character) and the file mode code corresponding to permissions for owner, group, and everyone else (the following nine characters):

  1. The first character (-) is the file type; in this case, the dash means "plain file;" it could also be d for directory, or l for symbolic link.
  2. The next three characters (rw-) are the owner's file permissions: norburym can read (r) and write (w) but not execute (-).
  3. The next three characters (r--) are the group file permissions: group norburym can read (r) but can't write (-) and can't execute.
  4. The final three characters (r--) are file permissions for "everyone else:" they can read (r) but can't write (-) and can't execute (-).

We need to make the file executable! We do that by using the chmod (change mode) command with the +x option (adding permission to execute) and then we check the permissions again:

Figure 14. chmod +x

We see that the permissions have changed to -rwxr-xr-x, which means that we have added the x, or executable, switch to the user, group, and other permissions. Once you've made this permissions change with chmod, your text editor will preserve this permission, even if you make subsequent changes to your script.

Now we're ready to try again:

Figure 15. Success!

The shell script runs successfully. Let's take a closer look at what is actually going on: when the bash shell reads the first line of our shell script, #!/bin/bash, it actually generates a new bash shell that reads and executes the commands from our script. At the completion of command execution, the new shell terminates automatically and we're left at our original shell.

We took some pains to create our PATH statement in our .bash_profile file. If we hadn't done that, and we had tried to execute the shell script from our default terminal window (which opens with our location set to our home directory, not our /Documents/scripts directory), then we would have gotten the error "command not found." We would have needed to tell bash where our script is located with:

norburym15:~ norburym$ documents/scripts/

With our PATH statement telling bash where to look for our default scripts directory, we can type our command from any directory and bash knows where to look for our script: in the default scripts folder in our /Documents folder.

Pages: 1, 2, 3, 4, 5

Next Pagearrow