B09 Lab week 4

I teach several courses, and I have taught and will teach them over several years and terms. Should I organize my course directories by years and then by courses?

2017/B63
2018/B63
2018/C24
2019/B09
2019/C24

Or should I organize them by courses and then by years?

B09/2019
B63/2017
B63/2018
C24/2018
C24/2019

Every couple of years, I change my mind and switch from one scheme to the other! Help me automate this. Write a shell script ‘transpose.sh’ to:

  1. Take 2 command line arguments. They are assumed to be directory names. I'll refer to them as src and tgt respectively below.

    If the user gives fewer than 2 arguments, write to stderr an error/reminder message (exact message up to you), and exit with exit code 1.

  2. Attempt to create directory tgt.

    If mkdir fails (likely because tgt already exists), write to stderr an error message (exact message up to you), and exit with exit code 2.

  3. Copy src content to tgt, but switching the scheme along the way. For example, if I have

    src/2017/B63
    src/2018/B63
    src/2018/C24
    src/2019/B09
    src/2019/C24
    
    then the copying should result in
    tgt/B09/2019
    tgt/B63/2017
    tgt/B63/2018
    tgt/C24/2018
    tgt/C24/2019
    

    Reminder: ‘cp -R’ can copy a whole directory.

  4. You may not create/write any other file/directory, and you shouldn't need to. Automarking will happen in a docker container where only tgt (and under) is writable.

You may assume:

You may get good help from a program called ‘basename’. By way of example, ‘basename foo/bar/quartz’ outputs ‘quartz’ to stdout. (Now how do you capture that into your own variable so you can use it later?)

I have prepared a sample tree (YC.zip); also on Mathlab at

/courses/courses/cscb09s23/laialber/l04/YC

and you may test with, for example

sh transpose.sh /courses/courses/cscb09s23/laialber/l04/YC mytgt
and then see if you get good results under ‘mytgt’, e.g., compare with my CY directory (also as CY.zip):
diff -ru mytgt /courses/courses/cscb09s23/laialber/l04/CY

What to hand in: transpose.sh