MIT-Missing-Semester-01

Topic 1 Shell

Points

  1. date
  2. echo
  3. escape
  4. echo $PATH
  5. which
  6. pwd
  7. cd
  8. dot, dot dot
  9. ls
  10. /
  11. - dash: cd -, program -h –help
  12. permission bits rwx user group others
  13. mv
  14. cp
  15. mkdir rmdir
  16. quota “ “
  17. man: manual page
  18. two streams >, <, >>, <<
  19. tail -n1
  20. pipe |
  21. sudo echo 1 > /sys/** right ? Permission denied ?
  22. sudo su
  23. echo 1 | sudo tee /sys/**
  24. find

Exercises

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

    1
    2
    hadoop@wool-virtual-machine:~$ echo $SHELL
    /bin/bash
  1. Create a new directory called missing under /tmp.

    1
    2
    hadoop@wool-virtual-machine:~$ cd /tmp
    hadoop@wool-virtual-machine:/tmp$ mkdir missing
  1. Look up the touch program. The man program is your friend.

    1
    man touch
  1. Use touch to create a new file called semester in missing.

    1
    hadoop@wool-virtual-machine:/tmp$ touch missing/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
    3
    4
    5
    6
    hadoop@wool-virtual-machine:/tmp$ cd missing
    hadoop@wool-virtual-machine:/tmp/missing$ echo \#\!/bin/bash > semester
    hadoop@wool-virtual-machine:/tmp/missing$ echo curl --head --silent https://missing.csail.mit.edu >> semester
    hadoop@wool-virtual-machine:/tmp/missing$ cat semester
    #!/bin/bash
    curl --head --silent https://missing.csail.mit.edu
  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
    3
    4
    hadoop@wool-virtual-machine:/tmp/missing$ ./semester
    bash: ./semester: permission denied
    hadoop@wool-virtual-machine:/tmp/missing$ ls -l semester
    -rw-rw-r-- 1 hadoop hadoop 63 3月 29 21:11 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
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    hadoop@wool-virtual-machine:/tmp/missing$ sh semester
    HTTP/2 200
    server: GitHub.com
    content-type: text/html; charset=utf-8
    last-modified: Fri, 04 Mar 2022 17:03:44 GMT
    access-control-allow-origin: *
    etag: "62224670-1f37"
    expires: Mon, 28 Mar 2022 08:24:18 GMT
    cache-control: max-age=600
    x-proxy-cache: MISS
    x-github-request-id: C87C:7EE2:1B48AE:1F3E2D:62416E5A
    accept-ranges: bytes
    date: Tue, 29 Mar 2022 13:19:08 GMT
    via: 1.1 varnish
    age: 0
    x-served-by: cache-hnd18742-HND
    x-cache: HIT
    x-cache-hits: 1
    x-timer: S1648559948.926671,VS0,VE390
    vary: Accept-Encoding
    x-fastly-request-id: 64c590c0fcb701c17b33cd011d2e48c65d877970
    content-length: 7991

    Reason: the permission bits of the semester indicates that everyone could read it and sh just reads the semester

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

    1
    2
    3
    4
    5
    6
    7
    NAME
    chmod - change file mode bits

    SYNOPSIS
    chmod [OPTION]... MODE[,MODE]... FILE...
    chmod [OPTION]... OCTAL-MODE FILE...
    chmod [OPTION]... --reference=RFILE FILE...
  1. 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
    2
    3
    4
    5
    6
    7
    8
    9
    hadoop@wool-virtual-machine:/tmp/missing$ chmod a+x semester
    hadoop@wool-virtual-machine:/tmp/missing$ ls -l
    total 4
    -rwxrwxr-x 1 hadoop hadoop 63 3月 29 21:11 semester

    hadoop@wool-virtual-machine:/tmp/missing$ ./semester
    HTTP/2 200
    server: GitHub.com
    ........

    Wiki: In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sha-bang, hashbang, pound-bang, or hash-pling.

    When a text file with a shebang is used as if it is an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file’s initial line as an interpreter directive.

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

    1
    2
    hadoop@wool-virtual-machine:/tmp/missing$ ls -l semester | cut --delimiter=' ' -f6,7,8,9 | sudo tee ~/last-modified.txt 
    3月 29 21:47
  1. 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
    i can't find them.