B09 Lab week 3

This lab consists of two questions.

Windows Text Files (WTF) (2 marks)

I have two shell scripts that are “the same” (just you wait), but I created them on different platforms: hello-unix was created on Linux, hello-windows was created on Windows. When I test both on Mathlab, hello-windows has a problem, and it doesn't matter whether I use sh or bash:

$ sh hello-windows
hello-windows: 5: Syntax error: end of file unexpected (expecting "then")

$ bash hello-windows
hello-windows: line 5: syntax error: unexpected end of file

hello-unix does not have a problem:

$ sh hello-unix
hello

$ bash hello-unix
hello

WHYYYYY???!!!

Both files are on Mathlab in /courses/courses/cscb09s26/laialber/l03 so you can (and should) see for yourself.

Some ways to start investigating:

What to hand in: wtf.txt containing your short answer for the difference between my two files.

P.S. Now that you know, you have no excuse for handing in a shell script that breaks on Mathlab!

Basic Shell Scripting (6 marks)

This is a small exercise in basic shell scripting.

The shell script you will write will be called “check”. Its job, in short, is to check existence and/or directory-ness of user-provided paths. In detail:

  1. For each command line argument p, we treat it as a path, and:

  2. At the end, print out the number of paths found and the number of paths checked, in this format:

    n of m paths found.
    

Example: sh check /usr/bin/date "I don't exist" /usr/bin
The output should be:

/usr/bin/date exists.
I don't exist does not exist.
/usr/bin is a directory.
2 of 3 paths found.

The expected output is provided as sample-output.txt. Tell the computer to compare for you so it catches typos humans don't see, e.g.:

sh check /usr/bin/date "I don't exist" /usr/bin > actual.txt
diff -u sample-output.txt actual.txt

diff finds and prints line-by-line differences between two text files. If there is no difference, it outputs nothing, and the exit code is 0.