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!
Use the
which
command to determine the absolute path of thebash
executable file.which bash
Based on the output of
which
, usestat
orls -l
to confirm thatbash
has execute permission. For example, you might use the following command, replacing/path/to/bash
with the output ofwhich
:stat /path/to/bash
By convention, the
.sh
file extension is used for shell scripts. Create a regular text file calledcs1302-script.sh
and set the first line so that it conforms to the aforementioned requirements for an interpreter script. Replaceinterpreter
with the output ofwhich
, and exclude the optional arguments for now. It should look this:#!/bin/bash
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:
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):javac -d bin src/cs1302/test/MyStuff.java java -cp bin cs1302.MyStuff
From the terminal shell itself, enable execute permission (
+x
) for user (u
) who owns thecs1302-script.sh
file using thechmod
command:chmod u+x cs1302-script.sh
Verify that the permissions are changed using
ls -l
orstat
.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.Notice the output. It executed both commands!
Now remove user execute permission (
u-x
) usingchmod
, then attempt to execute the script again and observe the error message that appears. To resolve the error, enable user execute permission again.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
.