Course Overview & Shell


Course overview & the Shell

Exercise

  1. Create a new directory called missing under /tmp.

     mkdir /tmp/missing
  2. Look up the touch program. The man program is your friend.

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

     # cd /tmp/missing/ && touch semester
     touch /tmp/missing/semester
  4. Write the following into that file, one line at a time.

     # Use the > redirection symbol and >> to append the second line
     echo '#!/bin/sh' > semester
     echo 'curl --head --silent https://missing.csail.mit.edu' >> semester

    The history expansion character, usually i, must be quoted to prevent history expansion. '' treats the contents inside as literal value, it won’t change anything. "" preserves the literal value of all characters within the quotes, with the exception of $,\,",or newline.See here

  5. Try to execute the file, i.e. type the path to the script (./semester) into your shell and press enter.

     # Shell Prompt:
     # zsh: permission denied: ./semester
     ls -l
     # We can see semester has no excucute permission
  6. 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?

    When we run sh interpreter and give semester as it’s argument, we actually use the sh program to read the semester file’s content and run the sh program instead of running semester script directly.

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

    man chmod
  8. 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?

     chmod +x semester
     # shebang lines including a path to choose which interpreter to use.
  9. Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.

    ./semester | grep -i "last-modified" > /home/last-modified.txt
  10. 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.

    # Actually I am using macOS, and I run the command in WSL for Win10 on my Desktop.
    # It works fine.
    cat /sys/class/power_supply/battery/capacity
    # And the WSL can't fetch temperature, you can see it in /sys/class/thermal

评论
  TOC