https://missing.csail.mit.edu/2020/course-shell/
2. Create a new directory called
missingunder/tmp.
cd /tmp
mkdir missing3. Look up the
touchprogram. Themanprogram is your friend.
man touchThe touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions.
4. Use
touchto create a new file calledsemesterinmissing.
cd missing
touch semester5. 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.
echo '#!/bin/sh' > semester
echo "curl --head --silent https://missing.csail.mit.edu" >> semesterFrom the bash documentation:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of
$,`,\, and, when history expansion is enabled,!.
NOTE: Use single quotes if there are $, `, \, or ! characters inside of the string.
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 ofls(hint: look at the permission bits of the file).
./semester
# permission denied: ./semester
ls -l semester
# -rw-r--r-- 1 henrikh wheel 61 Jan 28 12:53 semesterThe script is not executable, as the x (execute) bits are missing.
7. 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?
- with
sh, you're running a program that will interpret the lines in your script just as if you would have typed them on the interactive prompt of the terminal. The script doesn't need to be executable. - with
./you're making a shortcut assuming that the script is just right here in the current directory you're sitting in AND it will be executable.
Source: https://askubuntu.com/a/22948
8. Look up the
chmodprogram (e.g. useman chmod).
The chmod utility modifies the file mode bits of the listed files as specified by the mode operand. It may also be used to modify the Access Control Lists (ACLs) associated with the listed files.
9. 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 +x semester
ls -l semester
# -rwxr-xr-x 1 henrikh wheel 61 Jan 28 12:53 semester#!/bin/sh (shebang line) tells the shell to interpret the script using sh.
10. Use
|and>to write the "last modified" date output bysemesterinto a file calledlast-modified.txtin your home directory.
./semester | grep "last-modified" | cut -d ' ' -f 2- > ~/last-modified.txtGet the request header, pluck the "last-modified" line from it, split it on space, take all fields starting from the second one, and write the output of the previous command to ~/last-modified.txt.