Link to this document's Jupyter Notebook

In order to successfully complete this assignment you must do the required reading, watch the provided videos and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on Tuesday February 2. Students must come to class the next day prepared to discuss the material covered in this assignment.


Pre-Class Assignment: BASH Scripting

Urban Penguin

software carpentry

Goals for today's pre-class assignment

  1. BASH Whitespace sensitivity
  2. Running BASH Scripts
  3. Executable files in Linux
  4. Explore Online Resources
  5. Assignment wrap up

1. BASH Whitespace sensitivity

WARNING Bash is highly "Whitespace sensitive" (Much worse than Python and Python is bad enough). This means that adding spaces and tabs can change how the programming language works. A common example is variable assignment. Consider the following command which creates a variable called num_of_files which is the number of (non-hidden) files in the current directory:

num_of_files=`la -la * | wc -l`

If you add a few spaces to make it "easier" to read you can break the command. For example adding spaces around the = will cause a "Command not found error":

num_of_files = `la -la * | wc -l`

This sensitivity to whitespace can be quite annoying. For this reason, pay close attention when you copy/paste examples from the internet.


2. Running BASH Scripts

There are multiple ways to run a BASH script.

Option 1: Run bash directly

In this option you run a new bash command inside of your bash terminal and then pass the bash command the name of your script file. For example:

bash ./myscript.sh

Option 2: Source the Bash File

You can also use the source command to run your script:

source ./myscript.sh

The difference between the source command and the bash command is that source runs your script inside your current instance of bash as if you typed the commands on the command line. While, bash starts a new bash interpreter, runs the commands in that new interpreter and then returns control back to your bash command. The source command can be extreamly useful if your bash script changes some enviornment variables and you want to keep the variables.

If you use the first, bash option, the bash command creates a new interpreter, your script would set some variables and then the interpreter would be closed before returning to yout main bash prompt.

If you use the second, source option, the source command will execute in your current bash environment and change the variables in the scope of that environment.

Option 3: Make your script an executable

For each file in Linux there is a "flag" indicating if the file is an executable. If a file's executable flag is set (see below on how to set a files executable bit), you can run the file just by type the file name. For Example:

./myscript.sh

If the file is in your PATH, then you can just type the name without the writing the path to the file. For example:

myscript.sh

NOTE: Since we are running our scripts form inside BASH the Linux operating system assumes they are BASH scripts. However, we may not want to count on this assumption. The first line of a executable script can be used to indicate what program Linux should use to run the script. For example:

#!/bin/bash

This line tells Linux to run this ASCII file inside of the /bin/bash program.


3. Executable files in Linux

You can check if a file's executable bit is set by running the "ls -la" command which has output similar to the following:

>ls -la
total 48K
drwxrwxr-x  2 colbrydi cmse    5 Jan  8 09:29 .
drwxrwxr-x 14 colbrydi cmse   35 Jan 24 08:34 ..
-rwxrwxr-x  1 colbrydi cmse 6.8K Jan  8 09:29 example
-rw-rw-r--  1 colbrydi cmse  747 Jan  8 09:29 example_calc_e.c
-rw-rw-r--  1 colbrydi cmse  209 Jan  8 09:29 README

The first cryptic looking string with 10 letters is the properties string for each file and looks something like that looks like drwxrwxr-x. The first character is the directory "bit which has the following meaning:

The first set of three letters (after the directory bit) are the permissions for the user of the file (in the above file the owner is colbrydi). The second set of three letters are the permissions for the group (in the above example the group is cmse). The final set of three letters are permissions for everyone or all.

Each set of three letters have the following meanings:

NOTE: if the file is a directory (i.e. the directory bit is set to d) then the executable bit indicates the category of users that can change into that directory.

QUESTION: What category of users can execute a file with -rwxrwxr-- permissions?

Put your answer to the above question here.

You can use the chmod (aka change mode) command to change permission bits. For example, the following command takes the file myfile and sets the (o)wner's e(x)ecutable flag to true:

chmod u+x myfile

You can also "turn off" the (o)wners e(x)ecutable flag by using the following command:

chmod u-x myfile

The format of the chmod command is quite simple. You type chmod followed by the user catigory you want to change ( user, goup, all). Then use a + to add that permission or - to remove the permission. Then the combination of letters rwx that you want to change.

QUESTION: What command would you write to change the permissions or the myscript.sh file in your current directory so that the owner (i.e. user) has all of the permission bits set (read, write and execute).

Put your answer to the above question here.


4. Explore Online Resources

BASH has been around for many decades. There are hundreds of tutorials available on line to help you learn how to use BASH effectively. For example, the above images have links too two tutorials that you may find helpful.

BASH is a programming language in it's own right. So far in class we have learned:

In class, we will be building more complex BASH scripts. To do more complex stuff in bash we need more complex commands. Specifically you need to know how to build BASH "if" statements and loops.

DO THIS: Explore the web to teach yourself how to use BASH if statements and for loops. Hint, use the keyword BASH when doing searches. This will narrow the search down to examples you can use. Test what you learn by writing a submission script with an if example and a for example.

QUESTION: What is the keyword used to end an if statement in bash?

Put your answer to the above question here.

QUESTION: Did you find any additional and helpful BASH tutorials not mentioned above? Please provide a URL link to that tutorial.

Put your answer to the above question here.


5. Assignment wrap up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credits for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

Assignment-Specific QUESTION: Did you find any additional and helpful BASH tutorials not mentioned above? Please provide a URL link to that tutorial.

Put your answer to the above question here

QUESTION: Summarize what you did in this assignment.

Put your answer to the above question here

QUESTION: What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

QUESTION: How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

QUESTION: What was the most challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What was the least challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

QUESTION: Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

QUESTION: Approximately how long did this pre-class assignment take?

Put your answer to the above question here


Congratulations, we're done!

To get credit for this assignment you must fill out and submit the above survey from on or before the assignment due date.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.