MIT the missing semester
这门课主要会教授一些课堂上不会讲的东西, 比如debug, shell, vim. 刚好国庆假期将近刷题刷烦了, 来学一下, 毕竟vim和gdb还不太熟练.
Okay, let's get started!!!.
echo hello (in any path), you can see
hello in your terminal, but how does the system get the
echo program working? when
ls in current location, we cannt find a program named
echo. And the answer behind this magic is
echo $PATH, the following will show up:
these are the locations that are registered in the environment variable, when we type
echo hello, the system will go to these locations to find whether there is a program named
echo, if true, will call it.
which means we are using the
echo program under directory /bin/
cd change directory:
`cd ~` change dir to user space; `cd -` change dir to the last location, this is super useful; `cd ..` go to upper directory; `cd /` go to root;
`ls` list stuff under cur directory `ls -l` list stuff using a long listing format including permission, date, owner, group... `ls -a` list stuff (including hidding file) `ll` list all stuff from root to cur directory
cat show the content of a file,
more can do the same thing;
df show free disk;
free show free memory;
top show the usage of computer resources;
sudo do something with root previlege;
sudo su enter root mode, exit with
pwd print working directory;
cp copy file to dest;
mv move file to dest, if dest in the same dir, this will simply change file name;
touch create a file;
rm delete file, directory;
mkdir create a directory;
in the pic above, the first letter
d shows whether this is a directory, then followed by 9 letters, these are permissions, the first three letters are for owner; the second are for group, the third are for all users.
r means read, weighted 4,
w means write, weighted 2,
x means execute, weighted 1. if we dont have
x and try to execute the file, we ll get a
permission denied. Fortunately, we can use
chmod to change permission, as mentioned above,
chmod 777 means open all permissions(read, write, execute) to all types of users;
chmod 755 means only owner can write this file;
| pipe: make the output of left program be the input of right program;
- Create a new directory called
- Look up the
manprogram is your friend.
touchto create a new file called
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.
- 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).
- Run the command by explicitly starting the
shinterpreter, and giving it the file
semesteras the first argument, i.e.
sh semester. Why does this work, while
- Look up the
chmodprogram (e.g. use
chmodto make it possible to run the command
./semesterrather 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.
>to write the “last modified” date output by
semesterinto a file called
last-modified.txtin your home directory.
cd /tmp; mkdir missing
echo '#!/bin/sh' > semester; echo curl --head --silent https://missing.csail.mit.edu > semesteror
- I guess sh have top previlege
sudo chmod 777 ./semester
./semester | grep -i last-modified > ~/last-modified.txt