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/bash
or/usr/bin/zsh
, that means you’re running the right program.$ echo $SHELL /bin/zsh
-
Create a new directory called
missing
under/tmp
.$ mkdir -p /tmp/missing
-
Look up the
touch
program. Theman
program is your friend.$ man touch
-
Use
touch
to create a new file calledsemester
inmissing
.$ touch /tmp/missing/semester
-
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.$ 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 semester
The
semester
is lack ofx
permission, so it cannot be executed. -
Run the command by explicitly starting the
sh
interpreter, and giving it the filesemester
as the first argument, i.e.sh semester
. Why does this work, while./semester
didn’t?$ sh semester HTTP/2 200 ...
-
Look up the
chmod
program (e.g. useman chmod
).$ man chmod
-
Use
chmod
to make it possible to run the command./semester
rather 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 bysemester
into a file calledlast-modified.txt
in 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 GMT
sed
andawk
, 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 :)