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 how I run it (by calling sh, by calling bash, as an executable):

$ 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-windows
-bash: ./hello-windows: /bin/sh^M: bad interpreter: No such file or directory

hello-unix does not have a problem:

$ sh hello-unix
hello

$ bash hello-unix
hello

$ ./hello-unix
hello

WHYYYYY???!!!

Both files are on Mathlab in /courses/courses/cscb09s25/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 how Windows and Unix differ in text files. (If you just answer the 3rd bullet point above, that will suffice.)

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.