This is the note that i took for the Class of the MIT : The Missing Semester of Your CS Education(2020).
https://www.bilibili.com/video/BV1x7411H7wa/?p=1
1.Shell
Whitespace seperates the arguments, if I need an argument as multiple words:
1.Quote those words;
2.Use ‘’\’’+Whitespace as space in this argument;
Path & Some relative handy commands
Veriable $PATH
echo $PATH
This shows all the paths on my machine that the shell will search for programs of files whose name matches the command which i tried to run.
which echo
which
command tells me that if I were to run a program called echo, I would run that one in the directory it shows.
Paths are the way to name the location on your computer.On Linux and MacOS these paths are separated by ‘/’ (forward slash) , while on Windows paths are seperated by ‘\’ (back slash) instead.
-
Absolute path
On Linux and MacOS, everything lives under the root namespace, so all absolute paths start with a slash.
On Windows there is one root for every partition,
C:\
、D:\
. -
Relative path
pwd
Print working directory.
cd /home
Change Directory. .
means current directory, ..
means the parent directory,~
means the home directory. -
means the directory you previously in.
ls
List all files and ditectories(in the directory which i typed in, default is the current directory).
Most programs take what are known as arguments like flags and options. These are things that usually start with a -
. One of the most handy ones is --help
, most programs implement this . This shows how to use this program and all the other flags and options of this program.
ls -l
result example:
total 0
drwx------+ 6 gongfeiyang staff 192 Aug 15 10:07 Desktop
drwx------@ 7 gongfeiyang staff 224 Aug 15 10:33 Documents
drwx------@ 38 gongfeiyang staff 1216 Aug 15 10:34 Downloads
drwx------@ 75 gongfeiyang staff 2400 Aug 8 14:51 Library
drwx------+ 6 gongfeiyang staff 192 May 31 14:27 Movies
drwx------+ 5 gongfeiyang staff 160 May 27 11:48 Music
drwxr-xr-x 3 gongfeiyang staff 96 Jun 9 08:58 Myblog
drwx------@ 18 gongfeiyang staff 576 Aug 10 16:16 OneDrive
drwx------+ 5 gongfeiyang staff 160 May 28 07:07 Pictures
drwxr-xr-x 8 gongfeiyang staff 256 Jul 27 17:53 Programming
d
indicates that something is a directory.l
indicates that something is a link file,-
indicates that it is a normal file;- The following 9 letters after that indicate the permissions that are set for that file:
- r (Read)
- w (Wirte)
- x (eXecute)
The first there of those indicates the permissions of the owner of this file. The next three indicated the permission of the group of this file. Last three indicates other’s permission.
- The following number means the number of the hard links
gongfeiyang
is the owner of this file,staff
is the group of the owner.- The following number indicates the size(Bytes) of the file.
Other handy commands
mv
,cp
,mkdir
Streams
Every program by default has two primary streams: input stream and output stream. By default, the input stream and output stream are terminal.
Shell gives a way to rewire these streams to change where the input and output of a program pointed.
The most straightforward way to do this is using <
(rewire the input to this file) and >
(rewire the output to this file) ,>>
(append to):
Eg:
echo hello > hello.txt
cat < hello.txt > hello2.txt
cat < hello.txt >> hello2.txt
-
Pipe character :
|
Take the output of the program to the left and make the input of the program to the right.
eg:
ls -l / | tail -n1
I want the output of
ls
to be the input of thetail
.
Root User
Root user can do whatever he want to the computer, sort of like a super user.
sudo
Do as super user.
sudo su
Drop into root shell.
Variable
Assign variables and replace them in string:
foo=bar
echo "Value is $foo"
echo 'Value is $foo'
Output:
Value is bar
Value is $foo
-
$0
: The name of the script. -
$1
~$9
: The Xth argument of the command input. -
$_
:The last argument of the previous command. -
$?
:The error code from the previous command. 0 means no error. -
$#
:The number of arguments we are giving to this program. -
$$
: The process ID of this command that is running. -
$@
: The vector of all arguments we are giving.How to use
$?
:false || echo "Oops fail" true || echo "this will not be printed" true && echo "Things went well" false && echo "This will not be printed"
||
: If the$?
of the left command is not 0, bash will try to execute the right one.&&
: If the$?
of the left command is 0, bash will execute the right one.
Getting the output of a command into a variable.
foo=$(pwd)
cat <(ls) <(ls ..)
Globbing & Expand
ls *.sh
ls project?.sh
*
can be replaced by any string.
?
can be replaced by one character.
ls image.png image.jpg
ls image.{
png,jpg}
touch {
foo,bar}/{
a