The Bash shell is a way for the user to execute commands to the system. Using the shell can be very powerful and is similar in concept to the command line interpreter found in Microsoft Windows, cmd.exe. The Bash shell provides a scripting language that can support automation of tasks and is more sophisticated than the cmd.exe.
- Creating a bash file
To create a bash script, you simply need to start out by creating a text file with .sh extension. This extension tells the user that this file is a bash script.
While using the HPC cluster, SLURM will rely on the bash script to queue up jobs. All bash scripts should be using the shebang construct as the first line: #!/bin/bash The shebang is the combination of the # (pound key) and ! (exclamation mark). This character combination has a special meaning when it is used in the very first line of the script. It is used to specify the interpreter /bin/bash with which the given script will be run by default.
After you have created your script, now you need to add some lines of code for use with SLURM. Check out the SLURM page for more information on SLURM. Now, when you are done filling in the text file, you will need to save it and make it executable to be able to run on the cluster.
- Making file executable
To make the file executable, use the chmod command which is used to change the file’s read/write/execute permissions. To make the script executable, you can use the following command: $chmod +x filename.sh
Notice the change in the color of the file “pytest.sh”. This is because now the file is executable. The +x is used to allow the user to change the file from being a text document to an executable bash script.
When writing Bash scripts, it’s always a good practice to make your code as clean and understandable. Comments are used to improve the readability of your code. A comment is a human-readable explanation or annotation that’s written in the shell script. Hash mark (#) is used to write comments in bash scripts. Bash ignores everything written on the line after the #. The only exception to this rule is when the first line of the script starts with the #! characters (shebang).
- Run a bash script
After you have created your script and have made it an executable file, now you can run it. To run the script, you need to type the following command in your terminal: $./filename.sh