In order to successfully complete this assignment you need to participate both individually and in groups during class. Have one of the instructors check your notebook and sign you out before leaving class.


ICA 5: BASH#

Simple logo for the bourne again shell

Agenda for today’s class (70 minutes)#

  1. (20 minutes) Pre class Review

  2. (20 minutes) BASH Tour

  3. (30 minutes) Challenges


1. Pre class Review#

Discuss with your group and summarize the key topics learned in the PCA.

Write your summary here


2. BASH Tour#

QUESTION: What are the key components of a programming language? What are the minimally useful set of commands you need to do something interesting in a programming?

  • Variables

    • input arguments

    • Return variables

  • Process IDs

  • Block code

    • If statements

    • Loops

  • piping and redirection

  • Common BASH Commands

    • ls

    • pwd

    • find

    • cat, more or less

    • grep

    • sed

    • cut

    • wc

EXAMPLE: Run the following command in the BCCD Directory.

echo $PATH | sed -e "s/:/\n/g"

QUESTION: What did the above code do?

EXAMPLE: Run the following command in one of the BCCD Directories with a Makefile.

cat Makefile | grep cd | grep MAKE | cut -d "d" -f2- | cut -d ";" -f 1 | sort | uniq

QUESTION: What did the above code do? Explain each part of the code?


3. Challenges#

Challenge 1: Write a BASH script to loop though a list of usernames (ex ./BASH/usernames.txt) and print out any jobs each person may have running using squeue -l -u $USERNAME. Hint: use the who command to find a list of active users.

# put your bash script code her

Challenge 2: Write a bash script that finds all of the files in the current directory of the form slurm-#######.out and prints out only the jobid (i.e #######). For an extra challenge, use the squeue command to check and see if the job is still running and print “RUNNING” or “COMPLETE” next to the job number.

Challenge 3: Write a script that takes a command as an input and runs the command 10 times and prints the time to the screen. For an extra challenge also print the average time.

Challenge 4: Write a bash script that renames all of the files in a directory with the out extension to all uppercase.

Challenge 5: Write a bash script to start a program and make sure it only runs for 1 minute. If the program takes longer than a minute to run it should be “killed” an an error message printed to the screen. For simplicity, use the sleep 10 command as the “test” program. For an extra challenge allow the long running command to be passed into the script as an input variable.


Congratulations, we’re done!#

Have one of the instructors check your notebook and sign you out before leaving class.

Written by Dr. Dirk Colbry, Michigan State University (Updated by Dr. Nathan Haut in Spring 2025) Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.