Exercises
Piping, redirect, stdout, and stderr
-
echo 'Hello world!'to a text file using a redirect. Now append'Hi again!'to that same file without overwriting. -
Use
catto echo the file you just created using<. You can also just use the file name. (Or hey, useteewith<. Theman teeto learn what it does.) -
Speaking of
cat, pull up itsmanpage. -
Now use a
|togrepforHiincat’s output for that same file. -
The
module availcommand lists all the modules on Adroit. It is very useful. Try togrepthe output using a pipe (|). You will note it does not work, because it writes tostderr. Using a stream redirect, successfullygrepits 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.
-
Using
find, look for the file calledneedle. You can use any combination of piping +grepOR-inameflags. -
Once you find
needle, now print its contents in one command. (You’ll need to use-inameand-exec.) -
Okay, now let’s do something silly, find all the
haystacks. For extra credit, count how many there are. (Hint: pipefind’s output towc -l) -
There’s a needle hiding in a haystack file. Using
grep -R, find it.
Bash scripting
-
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. -
Write a Bash script that saves the output of a command you’ve run previously to a variable, and then echo that variable out.
-
Write a Bash script to copy the
exercise_filesdirectory to your home, then runcat [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.