OO Programming and Data Structures | CS 241

13 Prepare : Reading

Outcomes

At the end of this week, successful students will be able to:

  1. Show fluency in discussing shell programming.

  2. Write scripts that correctly use shell programming to solve problems.

This week we will be studying the Linux shell and how to write bash shell scripts.

Checkpoint A will reinforce basic shell commands, and Checkpoint B will require you to write some basic shell scripts.

Preparation Material

In addition to the sites that the Checkpoints specifically walk you through, please consider: Wooledge: Bash Guide, which is an excellent set of resources for learning Bash shell commands and scripting.

There is also a section of supplementary material at the bottom of this document.

Checkpoint A

For Checkpoint A, refer to: Learning the Shell.

Depending on how familiar you are with each topic, you may be able to skim most of them (especially the first half). Please make sure to read the section on I/O redirection in more detail than the others.

Checkpoint B

For Checkpoint B, refer to: Writing Shell Scripts. Please go through the sections below. FYI, Checkpoint B essentially asks you to follow his tutorial.:

Supplementary Material

We have been working with the Linux file system a little bit throughout the entire semester, so you should have familiarity with many of the basic commands.

Viewing Permissions

To list the files in a directory, you can either type ls to see just the filenames or ls -l to see a more detailed long list:


[burtons@LinuxLab07 linuxCommands]$ ls
demo2.txt  demo.txt  myScript.sh  output.txt

[burtons@LinuxLab07 linuxCommands]$ ls -l
total 12
-rw-r--r--. 1 burtons faculty  0 Dec  4 13:10 demo2.txt
-rw-r-----. 1 burtons faculty 58 Dec  4 13:10 demo.txt
-rwxr--r--. 1 burtons faculty 38 Dec  4 13:24 myScript.sh
-rw-r--r--. 1 burtons faculty 42 Dec  4 13:37 output.txt

Notice the part: "burtons faculty", this says that the owner of these files is burtons and that the group is faculty. Then, notice the first several characters of each line "-rw-r--r--". There are 3 possible permissions for the file, read (r), write (w), and execute (x). This is repeated 3 times, because the first set of them displays the permissions the owner has for this file, the second set has the permissions for the group, and the final set has the permissions for everyone.

With this in mind, you can see the file demo.txt can be read and changed (written to) by burtons, it can be accessed (read) by anyone in the faculty group, but cannot be accessed at all by anyone else.

Changing permissions

To change the permissions of a file, we can use the chmod utility and specify it directly, such as removing the write rights for everyone:


chmod o-w myFile.txt

or adding execute rights to the group:


chmod g+x myFile.txt

But another way is to consider the permissions as either on or off:


rw-r-----

is:

rwx rwx rwx
110 100 000

Then we can represent each group of 3 values as a binary number:


rwx rwx rwx
110 100 000

is:

6 4 0

Then this binary number can be given to the chmod utility to specify all the permissions at once. This is very common in Linux administration:


chmod 755 myFile.txt        # This sets it to -rwxr-xr-x
chmod 640 myFile.txt        # This sets it to -rw-r-----
chmod 600 myFile.txt        # This sets it to -rw-------
Running a script

As you begin working with scripts you might create a script testScript.sh and try to run it, but get an error:


[burtons@LinuxLab07 linuxCommands]$ ls -l
total 12
-rw-r--r--. 1 burtons faculty  0 Dec  4 13:10 demo2.txt
-rw-r-----. 1 burtons faculty 58 Dec  4 13:10 demo.txt
-rwxr--r--. 1 burtons faculty 38 Dec  4 13:24 myScript.sh
-rw-r--r--. 1 burtons faculty 42 Dec  4 13:37 output.txt
-rw-r--r--. 1 burtons faculty  0 Dec  6 11:09 testScript.sh

[burtons@LinuxLab07 linuxCommands]$ ./testScript.sh
-bash: ./testScript.sh: Permission denied

What do you mean, "permission denied,"" it's my file! Well, the problem here is that I don't have execute rights to this file, only read and write. So I need to change the permissions to add the execute permission. Then I can run the script.