2 Bash and scripting

Slides

You can download the slides for this tutorial below.

Exercise 1

Write a shell script that prints Shell Scripting is Fun! on the screen. Now modify the shell script to include a variable. The variable will hold the contents of the message Shell Scripting is Fun!.

Exercise 2

Write a shell script to check to see if the file foobar exists. If it does exist, display "foobar" is definitely there. Next, check to see if you can write to the file. If you can, display You have permissions to edit "foobar". If you cannot, display You do NOT have permissions to edit "foobar".

Exercise 3

Write a shell script that displays man, bear, pig, dog, cat, and sheep on the screen with each appearing on a separate line. Try to do this in as few lines as possible i.e. not repeating commands excessively. Hint: you may have to create a “Bash array” of the words in the for line.

Exercise 4

Write a shell script that takes the name of a file or directory (as an argument) and prints if it is a regular file, a directory, or another type of file on your screen.

Exercise 5

Create a script that searches the files in

/projects/micb405/data/bordetella/* or
/projects/micb405/data/bordetella/Full_Run/*

for the following sequence

GCGCGCCTGGGCCCGGGCCTGCCCGCGATCGGCGCGGCGCACGATCAAGGGCATGGCGACATTGTCCAGCGCCGTGAACTCCGGCAGCAGGTGATGGAACTGGTAGACAAAGCCCAGGCTGCGATTGCGCAAGGCGCTCTTGCGGGATTCGGACAGGCCGTCGGCCGAGGTGCCGTCGACCACGACCGAGCCGCTGCTGGGCACATCCAGCAGGCCCAGGATGTGCAGCAGCGTGCTCTTGCCCGACCCC

and produces the following (or similar) output:

ahauduc_mb20@orca01:~$ bash script.bash
F01_R1.fastq contains the sequence
F01_R1_1M.fastq does not contain the sequence
F01_R2.fastq does not contain the sequence
F01_R2_1M.fastq does not contain the sequence

Additional resources

For more help, check the looping and scripting chapters of Software Carpentry!