6.2. Bash Scripts

One of the easiest examples to understand is a shell script. When the shell being used is Bash, we usually call it a bash script. In a Bash script, you can include a sequence of commands that you would normally type into the terminal shell.

Let’s create one!

  1. Use the which command to determine the absolute path of the bash executable file.

    which bash
    
  2. Based on the output of which, use stat or ls -l to confirm that bash has execute permission. For example, you might use the following command, replacing /path/to/bash with the output of which:

    stat /path/to/bash
    
  3. By convention, the .sh file extension is used for shell scripts. Create a regular text file called cs1302-script.sh and set the first line so that it conforms to the aforementioned requirements for an interpreter script. Replace interpreter with the output of which, and exclude the optional arguments for now. It should look this:

    This is the first line in your .sh file. Don’t execute on the command line.
    #!/bin/bash
    
  4. In cs1302-script.sh, add some lines containing commands that you would normally type at the terminal shell. Any terminal commands will work!

    For example, you might include the following:

    Add these lines to your .sh file. Don’t execute them on the command line.
    echo "my bash script is running"
    ls -alh
    

    or, if you have a class with an FQN of cs1302.test.Mystuff, you could script the compilation and execution of this class by putting the following terminal commands in a script (assuming your present working directory is the top-level project directory):

    example (hypothetical) compilation lines that could be added to a script.
    javac -d bin src/cs1302/test/MyStuff.java
    java -cp bin cs1302.MyStuff
    
  5. From the terminal shell itself, enable execute permission (+x) for user (u) who owns the cs1302-script.sh file using the chmod command:

    Allows you to execute your script. Should be run from the command line.
    chmod u+x cs1302-script.sh
    
  6. Verify that the permissions are changed using ls -l or stat.

  7. Execute the script. To do this, simply provide an absolute path to the script name or an abbreviated absolute path using the . directory. For example, you might try one of the following, the first of which will need to be modified slightly based on the absolute path of your script:

    /path/to/cs1302-script.sh
    
    ./cs1302-script.sh
    

    Since . is an alias for the current directory, the second example will only work if the script is in the current directory.

  8. Notice the output. It executed both commands!

  9. Now remove user execute permission (u-x) using chmod, then attempt to execute the script again and observe the error message that appears. To resolve the error, enable user execute permission again.

  10. That’s it! You now know the basics of Bash scripts. There is a lot more than can be done, but we recommend that you look those things up when you need them. For further reading, refer to the nicely written and free BASH Programming book [MIKKEY2000] as well as the Bash Reference Manual [BASHREF].

6.2.1. Display Commands in Bash Script Execution

If you would like your bash script to display the commands that it executes, then add -x as an [optional-arg] in the shebang or add set -x as the first command in your script.

#!/bin/bash -x
#!/bin/bash

set -x

6.2.2. Stop on Error in Bash Script Execution

If you would like your bash script to stop executing the commands if one of them terminates with an error, then add -e as an [optional-arg] in the shebang or add set -e as the first command in your script.

#!/bin/bash -e
#!/bin/bash

set -e

You can combine -e and -x as -ex.