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.
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:
sudo apt updateNote: package update won't be mentionned from now on
sudo apt install zsh -yNote: -y flag so you don't have to type y to accept install.
zsh --versionNote: installation verification won't be mentioned from now on.
chsh -s $(which zsh)
echo $SHELL
Create a new directory called missing under /tmp
mkdir /tmp/missingLook up the touch program. The man program is your friend.
man touchUse touch to create a new file called semester in missing.
touch /tmp/missing/semesterWrite 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.
echo '#!/bin/sh' > semester
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.
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).
./semester
Terminal output:
zsh: permission denied: ./semesterls -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-).
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?
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.
Look up the chmod program (e.g. use man chmod).
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.
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.
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
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.
cat /sys/class/power_supply/BAT1/capacity
Will return a number from 0 to 100
cat /sys/class/thermal/thermal_zone*/temp
type of different thermal zones to find the one associated to the cpu and change the * for the appropriate number.