View on GitHub

Commandline Tools Workshop

Course site for PiCSciE/RC bootcamp workshop

Exercises

Piping, redirect, stdout, and stderr

  1. echo 'Hello world!' to a text file using a redirect. Now append 'Hi again!' to that same file without overwriting.

  2. Use cat to echo the file you just created using <. You can also just use the file name. (Or hey, use tee with <. The man tee to learn what it does.)

  3. Speaking of cat, pull up its man page.

  4. Now use a | to grep for Hi in cat’s output for that same file.

  5. The module avail command lists all the modules on Adroit. It is very useful. Try to grep the output using a pipe (|). You will note it does not work, because it writes to stderr. Using a stream redirect, successfully grep its output.

Finding files

There is a folder in your instructor’s home directory on Adroit ~bhicks/exercise_files/ that is open to everyone to look at (but not to edit!).

These exercises will use find ~bhicks/exercise_files/ and grep to hunt for things.

  1. Using find, look for the file called needle. You can use any combination of piping + grep OR -iname flags.

  2. Once you find needle, now print its contents in one command. (You’ll need to use -iname and -exec.)

  3. Okay, now let’s do something silly, find all the haystacks. For extra credit, count how many there are. (Hint: pipe find’s output to wc -l)

  4. There’s a needle hiding in a haystack file. Using grep -R, find it.

Bash scripting

  1. Write and run a Bash script that prints Hello world! Yes, you can copy my code but try to understand exactly why it does what it does.

  2. Write a Bash script that saves the output of a command you’ve run previously to a variable, and then echo that variable out.

  3. Write a Bash script to copy the exercise_files directory to your home, then run cat [needle_files] where you replace [needle_files] with both files that had needles in them (or their name).

As a hint, you could use find to locate the named file, and grep with -l flag in addition to print jut file names for the file with needle in it. Store the output to a variable.