Advanced Lab 1

Lecture: Thursday 2018-09-13 Released: Monday 2018-09-17 Due: Monday 2018-10-01

Grading note

Labs are graded on completion. Treat this lab as seeds of exploration instead of just a grade. If you don’t pass on the first submission, you can have it checked off in-person by a decal facilitator. As this is the first lab, it is intentionally simple and easy to complete, but 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 that generates a user-specified number of files of user-specified size filled with random content.

e.g.

$ ./mkrandom 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 dd and the iflag=fullblock argument, seq, and /dev/random.

Question 2

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

e.g.

$ mkdir tmp && touch tmp/{a..z}.dat
$ ./rename 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

Submission

Fill out the following Google form.