missing_class

bash

Table of Contents

bash

Notes based on https://missing.csail.mit.edu/2020/course-shell/

Bourne again shell

Paths and permissions

Long listing

As an example:

sinkers@DESKTOP-HQ8VENU:~$ ls -l
total 16
-rw-r--r-- 1 root    root      65 Feb  5 17:27 hello_world.c
-rwxrwxrwx 1 sinkers sinkers 8304 Feb  5 17:28 hello_world.out

Move, copy, create

Streams and Redirection

(Simplifying) Programs have two main streams:

Shell gives you ways to redirect those streams:

root user

In /sys is a bunch of parameters for devices on the computer. Say we were in backlight directory:

$ echo 500 > brightness
bash: brightness: Permission denied

But would if we use `sudo

$ sudo echo 500 > brightness
bash: brightness: Permission denied

Here sudo applies to the echo command, so when it gets redirected brightness is not executed with the shell running as root

Exercises

  1. Create a new directory called missing under /tmp.
  2. Look up the touch program. The man program is your friend.
  3. Use touch to create a new file called semester in missing.
  4. 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.

  5. Try to execute the file. Investigate why it doesn’t work with ls.
  6. Look up the chmod program.
  7. Use chmod to make it possible to run the command ./semester.
  8. Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.
  9. 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.

Solutions

  1. $ mkdir /tmp/missing
  2. $ man touch. Changes file timestamps to current time
  3. touch semester
  4.  $ echo '#!/bin/sh' > semester
     $ echo "curl --head --silent https://csail.mit.edu" >> semester
    
  5.  $ ./semester
     -bash: ./semester: Permission denied
     $ ls -l
     total 0
     -rw-rw-rw- 1 sinkers sinkers 96 Feb  7 01:06 semester
    

    There are no execute permissions set for anyone.

  6. $ man chmod: chmod changes file mode bits chmod calculator

  7.  $ chmod u+x semester
     $ ls -l
     total 0
     -rwxrw-rw- 1 sinkers sinkers 96 Feb  7 01:06 semester
    

    Now semester can be executed

  8.  $ ./semester | grep Date > ~/last-modified.txt
    
  9.  $ cat /sys/class/power_supply/battery/capacity
     98
    

Edit this page.