Skip to content

MIT Missing Semester 1: Shell

这个是Missing Semester的第一节课。

主要是一些简单的bash指令的介绍。

Course Overview

basic command

  • echo 返回源的内容。echo $PATH给出所有的环境变量。
  • which 输出location of file
  • pwd print working directory
  • --help print out a bunch of information about the command
  • flag & option 有可选项的是option,只有一个-a这样的是flag
  • ls -l more information about a directory
  • mv & cp move & copy
  • rm & rmdir remove, rmdir只能移除僵尸粉空目录
  • man a manual page for a command
  • tee 不仅写入输出流,还写入标准输出流

stream

Default input stream & output stream is the terminal

  • "<" redirect input stream

  • "<" redirect output stream

  • ">>" append instead of overwrite

  • cat print一个文件的内容

  • | pipe管道符,左侧程序的输出作为右侧程序的输入

> ls -l  / | tail -n1

上述命令指将根目录下的文件详细信息的最后一行输出出来

sudo

在linux中我们一般以一个用户的方式访问文件,而非root权限,但是对于一些特等的文件,以当前用户的权限我们不能够访问,所以需要用到sudo命令。

比如在sys/class/backlight目录下,能够调整我们当前的亮度,普通的echo 500 > brightness不能够完成这个操作(permission denied)。

但是我们用sudo echo 500 > brightness也不行,因为这个操作用到了重定向,我们只在echo 500这个命令使用了root权限,但是输出流的时候没有用到root权限,所以仍然permission denied。

所以我们需要使用sudo su登陆root才可以。或者使用# echo ... ,这个指令中的"#"意味着使用root权限执行命令。

Exercise

  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.

    Screenshot 2023-09-13 at 13 18 48

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

  3. Look up the touch program. The man program is your friend. 3

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

  5. Write the following into that file, one line at a time:

#!/bin/sh
curl --head --silent https://missing.csail.mit.edu

5

  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). 6

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

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

  4. Use chmod to make it possible to run the command ./semester rather than having to type sh semester. 9

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

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