0%

[Missing Semester in CS] Shell

The first class introduces the terminal shell, and they mainly focus on Bash since it is the default in most platform.

Brief

It is perfect for the beginners without any knowledge of shell. Starting from terminal basics to some basic commands in bash such as echo and ls, the class provides enough information for anyone to start to explore the shell system (I guess man is enough to start …).

After going through the video and texts, we still need to figure out how to use more commands in order to grasp the basics of temrinal. I guess the most important experience for me is to search only when you need it. There are tons of commands to know if we want them all, and each commands has tons of options to explore. It turns to be extremely boring for me if I learn by reading A-Z exhausitive reference of shell commands. If you want to know how to find a file, then you learn find, and if you need to change a batch of files’ names, you get to know for loop and probably sed.

Another thing that might be helpful is, there are always many ways to finish one target. If you don’t like the top answer from StackOverflow, maybe the second one or some comments might attract you.

Exercise

  1. Create a new directory called missing under /tmp.
1
2
$ cd /tmp
$ mkdir missing
  1. Look up the touch program. The man program is your friend.
1
$ man touch

Here is a link about discussion
of how to use touch. Except create a new file, it can change the
timestep to accomplish interesting task.

  1. Use touch to create a new file called semester in missing.
1
$ touch semester
  1. Write the following into that file, one line at a time:
    1
    2
    #!/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
2
$ echo \#\!/bin/sh  > semester
$ echo "curl --head --silent https://missing.csail.mit.edu" >> semester

What if we want to add these line at the beginning of the file? Here are
some solutions.

  1. 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
2
$ ls -l semester
-rw-r--r-- 1 hcwang hcwang 51 Apr 1 23:35 semester
  1. 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
2
3
4
5
6
$ sh semester
HTTP/2 200
server: GitHub.com
content-type: text/html; charset=utf-8
......
content-length: 6845

Why is that? When use sh, the system will call dash interpreter to read the file semester, where the reading is permitted. While ./semester directly execute the file which does not has execution permission.

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

  2. 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.

1
$ chmod u+x semester
  1. Use | and > to write the “last modified” date output by
    semester into a file called last-modified.txt in your home
    directory.

    1
    $ ./semester | grep "last modified" > last-modified.txt
  2. 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
2
3
4
5
6
## for energy
$ cat /sys/class/power_supply/BAT0/energy_now
$ cat /sys/class/power_supply/BAT0/energy_full_design
$ cat /sys/class/power_supply/BAT0/energy_full
## for cpu temperature
$ cat /sys/devices/platform/coretemp.0/hwmon/hwmon?/temp*