Advanced Lab 1

A Note on Labs

Labs are graded on completion. Treat this lab as seeds of exploration instead of just a grade.

As this is the first lab, it is intentionally simple and easy to complete. Bash scripting isn’t a particular goal of the DeCal but this lab should introduce you to some fun bash features you may not have encountered before, such as loops and shell expansions.

Workflow

This lab can be done on your own UNIX-like machine, or you can ssh into tsunami.ocf.berkeley.edu using your OCF account to finish the lab there. As always, man and Google will be your friends.

Question 1

Using Bash functions, write a script mkrandom.sh that generates a user-specified number of files of user-specified size filled with random content.

e.g.

$ ./mkrandom.sh 10 100  # create 10 100 byte random files
$ ls -lAh
total 44K
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 1
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 10
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 2
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 3
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 4
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 5
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 6
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 7
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 8
-rw-r--r-- 1 abizer ocf  100 Sep 16 21:57 9
-rwxr-xr-x 1 abizer ocf  147 Sep 16 21:56 mkrandom

You may want to look into dd1 and the iflag=fullblock argument, seq, and /dev/random2.

Question 2

Using Bash functions and shell wildcard expansion, write a shell script rename.sh to batch rename file extensions in a particular directory.

e.g.

$ mkdir tmp && touch tmp/{a..z}.dat
$ ./rename.sh tmp dat txt
renaming tmp/a.dat to tmp/a.txt
...
renaming tmp/z.dat to tmp/z.txt
$ ls -lAh tmp | grep .txt | wc -l
26

for bonus points, instead of using something like sed to affect the rename, use shell parameter expansion.

Question 3

I like Lisp and Scheme, and miss car and cdr in my usual programming tasks.

In bash, implement car and cdr (aka head and tail) such that they operate on file paths.

e.g.

$ ./car /home/a/ab/abizer/some/path
home
$ ./cdr /home/a/ab/abizer/some/path
a/ab/abizer/some/path

There’s no need to use complicated string manipulation for this task. You may assume that only absolute paths will be given.

bonus points: generalize this solution to work for cadr, caddr, etc.

$ ./cadr /home/a/ab/abizer/some/path
a
$ ./cddr /home/a/ab/abizer/some/path
ab/abizer/some/path

Hint: The easiest way to do this is with one very short command.

Submission

Submit your solutions on Gradescope! There’ll be some extra feedback questions as well

  1. dd is a command used to copy files.3 

  2. A curious individual might find the device file /dev/urandom as well. What’s the difference? True randomness is a rather difficult problem for computers, as they’re expected to do the same thing given the same state, so they pull in random data from metrics like internal temperature and mouse movement. Unfortunately, such entropy may not exist in certain machines and gathering entropy may be prohibitively long. Thus, /dev/urandom, or “unlimited random”, is a useful source when such randomness is not cryptographically critical. 

  3. “But wait,” a nearby straw-man asks, “isn’t that what cp does?”4 

  4. They are indeed right, but dd has some useful features such as partial writing and reading that make it handy in weirder scenarios, such as devices. StackOverflow has a good explainer and the ArchWiki has some common examples