Course overview + the shell #
Lecture source: https://missing.csail.mit.edu/2020/course-shell/
Back to the prior directory:
$ cd -
Execises #
-
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. To make sure you’re running an appropriate shell, you can try the command
echo $SHELL. If it says something like/bin/bashor/usr/bin/zsh, that means you’re running the right program.$ echo $SHELL /bin/zsh -
Create a new directory called
missingunder/tmp.$ mkdir -p /tmp/missing -
Look up the
touchprogram. Themanprogram is your friend.$ man touch -
Use
touchto create a new file calledsemesterinmissing.$ touch /tmp/missing/semester -
Write the following into that file, one line at a time:
#!/bin/sh curl --head --silent https://missing.csail.mit.eduThe 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.$ cat << EOF > /tmp/missing/semester #!/bin/sh curl --head --silent https://missing.csail.mit.edu EOF -
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 ofls(hint: look at the permission bits of the file).$ cd /tmp/missing $ ./semester zsh: permission denied: ./semester $ ls -l semester -rw-r--r-- 1 triplez staff 0 2 5 14:44 semesterThe
semesteris lack ofxpermission, so it cannot be executed. -
Run the command by explicitly starting the
shinterpreter, and giving it the filesemesteras the first argument, i.e.sh semester. Why does this work, while./semesterdidn’t?$ sh semester HTTP/2 200 ... -
Look up the
chmodprogram (e.g. useman chmod).$ man chmod -
Use
chmodto make it possible to run the command./semesterrather than having to typesh semester. How does your shell know that the file is supposed to be interpreted usingsh? See this page on the shebang line for more information.$ chmod a+x semester -
Use
|and>to write the “last modified” date output bysemesterinto a file calledlast-modified.txtin your home directory.$ ./semester | grep last-modified | awk -F ': ' '{ print $2 }' > last-modified.txt $ cat last-modified.txt Tue, 11 Jan 2022 16:37:26 GMTsedandawk, together we can change anything!-- Brian Wilson Kernighan -
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.I’m a macOS user, skipped for this task :)