Shell
命令行界面(CLI,Command Line Interface)
- they allow you to run programs, give them input, and inspect their output in a semi-structured way.
- Bourne Again SHell, or “bash”
终端:能显示Shell的文本窗口
简介
命令行提示符(shell prompt):打开shell就有的带有$的那行
- 包括用户名、机器名和当前所在的path
- 可以执行一个程序,或者带参数运行一个程序(一种修改程序行为的方法),用空格分隔参数
shell事实上是一种编程环境,具有变量、函数等。在shell中运行命令的时候,事实上已经是写了一些shell可以interpret的代码。
环境变量
Environmental variable
是shell已经设定好的无论何时启动shell都无需重新设置的变量。
$PATH
dongdeiMac:~ dong$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
dongdeiMac:~ dong$ which echo
/bin/echo
dongdeiMac:~ dong$ /bin/echo $PATH
/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
第一条命令显示了shell寻找程序时所查找的目录,第二条命令表示echo程序处在那个目录里,在macos和linux里斜杠是所有文件的根目录,windows里是各个分区+反斜杠。
常见指令
pwd:print working dir,打印当前所在的目录路径
.:当前目录
…:上一层目录
/:根目录
~:用户目录
-:上次访问的目录
mv:重命名文件/把文件移动到不同目录,两个参数from to
cp:copy
cat:打印一个文件的内容
rm:remove一个文件
rmdir:remove一个空文件夹
mkdir:创建一个文件夹
open:以合适的方式打开一个文件(xdg-open
for linux)
ls -l:以列表的形式展示文件内容
dongdeiMac:/ dong$ ls -l
total 45
drwxrwxr-x+ 69 root admin 2346 6 20 19:17 Applications
drwxr-xr-x+ 63 root wheel 2142 6 3 11:13 Library
drwxr-xr-x@ 2 root wheel 68 1 5 2017 Network
drwxr-xr-x@ 4 root wheel 136 10 26 2017 System
drwxr-xr-x 5 root admin 170 1 5 2017 Users
drwxr-xr-x@ 3 root wheel 102 6 21 14:01 Volumes
drwxr-xr-x@ 38 root wheel 1292 6 6 23:36 bin
drwxrwxr-t@ 2 root admin 68 1 5 2017 cores
dr-xr-xr-x 3 root wheel 4297 6 21 11:01 dev
lrwxr-xr-x@ 1 root wheel 11 1 5 2017 etc -> private/etc
dr-xr-xr-x 2 root wheel 1 6 21 14:17 home
-rw-r--r--@ 1 root wheel 313 9 1 2017 installer.failurerequests
dr-xr-xr-x 2 root wheel 1 6 21 14:17 net
drwxr-xr-x@ 6 root wheel 204 1 5 2017 private
drwxr-xr-x@ 63 root wheel 2142 6 6 23:36 sbin
lrwxr-xr-x@ 1 root wheel 11 1 5 2017 tmp -> private/tmp
drwxr-xr-x@ 9 root wheel 306 10 26 2017 usr
lrwxr-xr-x@ 1 root wheel 11 1 5 2017 var -> private/var
-
第一项,以d开头为目录,后面九个字母(read write execute)三个为一组,
- 前三个为给所有者的权限,此处所有者为root
- 中三个为给用户的权限,此处用户为admin/wheel等
- 后三个为给其他用户的权限
如果想要访问/读写等某个文件,必须要有其所有父目录和它本身的执行权限(x)
ctrl+L:清空终端(没有真正清空,鼠标往上滑都能看到)并让光标回到顶部
man ls:按q退出
连接程序
重定向
shell中的程序有两个主要的流(stream):输入流和输出流。程序从输入流中读取输入数据,将输出数据打印到输出流中。默认地,终端同时是输入流和输出流。shell提供了重定向这些流的方法。
< file1 > file
小于号表示重定向输入流为file1的内容,大于号表示重定向输出流为输入到file2中。
例如
dongdeiMac:missingsem dong$ echo hello >hello.md
dongdeiMac:missingsem dong$ cat hello.md
hello
会创建hello.md文件,并将hello输出到这里。注意此时终端中并没有打印hello。
当cat没有获得参数时,它会将输入流的内容直接打印到输出流,
dongdeiMac:missingsem dong$ cat <hello.md
hello
也就是将输入流hello.md的内容打印到了输出流terminal(此时没有重定向)
需要注意的是,
#
在Bash中表示注释,而!
即使被双引号("
)包裹也具有特殊的含义。 单引号('
)则不一样,可以转化成一个字符串。
>>:append
使用符号>>
可以append而不overwrite文件
dongdeiMac:missingsem dong$ cat <hello.md >hello2.md
dongdeiMac:missingsem dong$ cat hello2.md
hello
dongdeiMac:missingsem dong$ echo "this is a test" >> hello2.md
dongdeiMac:missingsem dong$ cat hello2.md
hello
this is a test
|:pipe
使左侧程序的输出成为右侧程序的输入
例如想要输出ls -l
的最后一行,其中tail
打印输出最后的n行,
dongdeiMac:/ dong$ ls -l / | tail -n1
lrwxr-xr-x@ 1 root wheel 11 1 5 2017 var -> private/var
ls的输出作为tail的输入,tail将内容输出到终端
sudo
macos和linux中的root类似于windows中的administer,用户id=0,可以在系统上做任意行为
想以root身份执行的时候(修改系统层面的信息),通常情况下使用sudo
(do as super user)
在句首用#是以root身份运行这条指令的意思,平常的$符号表示现在没有以root的身份运行,可以用
sudo su
指令进入root身份(用exit
指令退出)
考察如下指令:
[...]$ sudo echo 500 > brightness
[...]$ echo 500 > sudo tee brightness
[...]$ # echo 500 > brightness
[...]$ sudo su
...
brightness
是一项内核参数,第一条不能成功赋值。意思是将500写入brightness,而访问brightness需要root身份。
tee
读取输入,写入一个文件同时写入标准输出流