Lecture 1

  1. For this course, you need to be using a Unix shell like Bash or ZSH. If you are on Linux or macOS, you don’t have to do anything special. If you are on Windows, you need to make sure you are not running cmd.exe or PowerShell; you can use Windows Subsystem for Linux or a Linux virtual machine to use Unix-style command-line tools.

    If you are using Windows you can download wsl directly from powershell using:
    wsl --install

    Note: this is the only time you will be using powershell

    To make sure you’re running an appropriate shell, you can try the command echo $SHELL. If it says something like /bin/bash or /usr/bin/zsh, that means you’re running the right program.

    I recommend using zsh as your default shell. You can install it with the following commands:

    1. To update your package repository:
      sudo apt update
    2. Note: package update won't be mentionned from now on

    3. To install zsh:
      sudo apt install zsh -y

      Note: -y flag so you don't have to type y to accept install.

    4. To verify install:
      zsh --version

      Note: installation verification won't be mentioned from now on.

    5. To define zsh as your default shell:
      chsh -s $(which zsh)
    6. To verify it worked:
      echo $SHELL
  2. Create a new directory called missing under /tmp

    mkdir /tmp/missing
  3. Look up the touch program. The man program is your friend.

    man touch
  4. Use touch to create a new file called semester in missing.

    touch /tmp/missing/semester
  5. Write the following into that file, one line at a time:

    #!/bin/sh
    curl --head --silent https://missing.csail.mit.edu

    The first line might be tricky to get working. It’s helpful to know that # starts a comment in Bash, and ! has a special meaning even within double-quoted (") strings. Bash treats single-quoted strings (') differently: they will do the trick in this case. See the Bash quoting manual page for more information.

    1. echo '#!/bin/sh' > semester
    2. echo 'curl --head --silent https://missing.csail.mit.edu' >> semester

      Make sure you are in the correct directory. Otherwise, naviguate to the correct directory with cd /tmp/missing/semester or use an absolute reference for the code above. ex: echo '#!/bin/sh' > /tmp/missing/semester.

      Note: > rewrites the file while >> add the string to the end of the file.

  6. Try to execute the file, i.e. type the path to the script (./semester) into your shell and press enter. Understand why it doesn’t work by consulting the output of ls (hint: look at the permission bits of the file).

    1. ./semester

      Terminal output:

      zsh: permission denied: ./semester
    2. ls -l

      Terminal outupt:

      -rw-r--r-- 1 root root 61 Sep 21 00:11 semester

      Here we can see that the root user has read and write permissions but not execute permissions (-rw-).

  7. Run the command by explicitly starting the sh interpreter, and giving it the file semester as the first argument, i.e. sh semester. Why does this work, while ./semester didn’t?

    1. sh semester

      Terminal output:

      HTTP/2 200
      server: GitHub.com
      content-type: text/html; charset=utf-8
      last-modified: Thu, 08 Aug 2024 20:16:01 GMT
      access-control-allow-origin: *
      etag: "66b52781-205d"
      expires: Sat, 21 Sep 2024 05:11:22 GMT
      cache-control: max-age=600
      x-proxy-cache: MISS
      x-github-request-id: AD57:16DB:91AA4E:A2E8A7:66EE5322
      accept-ranges: bytes
      age: 0
      date: Sat, 21 Sep 2024 05:01:22 GMT
      via: 1.1 varnish
      x-served-by: cache-yul1970032-YUL
      x-cache: MISS
      x-cache-hits: 0
      x-timer: S1726894882.484443,VS0,VE27
      vary: Accept-Encoding
      x-fastly-request-id: 65ccdb2c25c249620614e7e7db2df9a2b545f6ea
      content-length: 8285

      The sh command is used to invoke a shell. It then reads the content of the file and executes it. The shell invoked is already executable and running so the script only needs to have read permissions.

  8. Look up the chmod program (e.g. use man chmod).

  9. Use chmod to make it possible to run the command ./semester rather than having to type sh semester. How does your shell know that the file is supposed to be interpreted using sh? See this page on the shebang line for more information.

    To add the execution bit to all users:
    chmod +x semester

    or

    To add it only for the root:
    chmod 744 semester

    The shell knows how to interpret the file because of the shebang specified at the top of the file (#!/bin/sh).
    Note: you don't need a shebang if you invoke the shell manually using the sh command although it is not recommanded to do so.

  10. Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.

    ./semester | head -n4 | tail -n1 > last-modified.txt

  11. Write a command that reads out your laptop battery’s power level or your desktop machine’s CPU temperature from /sys. Note: if you’re a macOS user, your OS doesn’t have sysfs, so you can skip this exercise.

    1. To read battery power: You might find this guide useful.
      cat /sys/class/power_supply/BAT1/capacity

      Will return a number from 0 to 100

    2. Wsl doesn't have cpu temperature in sysfs. However, if you are on linux,
      cat /sys/class/thermal/thermal_zone*/temp
      should work. You have to look at the type of different thermal zones to find the one associated to the cpu and change the * for the appropriate number.
      This guide might help.