shell是什么
shell是一种命令解析器,接收用户输入的文本命令,调用相应的应用程序,调用参数。它也是一种programming language,能够完成分支、循环等控制结构,有变量、参数等等。在shell中,我们可以将文本命令用大量不同的方式进行组合,或者用它来编程实现自动化,从而充分使用计算机,完成许多GUI图形界面完成不了的功能,可视化界面在功能上有一定的局限性。
windows上的shell有powershell
linux上有许多shell,最常见的就是bash
mac os也有bash。
初识shell
打开linux的Terminal,~表示用户主目录
$表示现在的身份是一般用户,对于一般用户~表示/home/(用户名)
#表示现在的身份是root,~表示/root
marco@marco-virtual-machine:~$
我们可以输入date来执行一个输出日期的程序
marco@marco-virtual-machine:~$ date
2023年 02月 07日 星期二 14:09:44 CST
echo程序,它是带参数的。输出参数
marco@marco-virtual-machine:~$ echo hello
hello
空格是用于分隔参数的
如果希望参数中包含空格,可以用单、双引号将其包裹起来,或者使用转义符号\进行处理。
marco@marco-virtual-machine:~$ echo "hello world"
hello world
marco@marco-virtual-machine:~$ echo hello\ world
hello world
marco@marco-virtual-machine:~$ echo 'hello world'
hello world
当我们输入date或者echo是,shell是如何知道这些程序是什么,该做什么的呢?
计算机拥有大量的built-in程序,它们存储在文件系统中,那么如何定位这些程序呢?
存在一种叫做环境变量的东西,可以确定程序所在的位置。
比如PATH,指示shell在哪个目录中搜索可执行文件,比如当输入echo时,shell就会在PATH中寻找可执行文件,然后执行。
marco@marco-virtual-machine:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
通过which,可以确定程序的可执行文件所在的路径
marco@marco-virtual-machine:~$ which echo
/usr/bin/echo
通过shell在文件系统中导航
pwd获取当前工作路径
marco@marco-virtual-machine:~$ pwd
/home/marco
cd切换目录, . 表示的是当前目录,而 .. 表示上级目录
cd~直接到/home
marco@marco-virtual-machine:~$ cd dev/pdos/classes/missing-semester/
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$
通过ls查看指定的目录下有哪些文件和目录
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ ls
food.md foo.md hello2.txt hello.txt ls.txt 'My Photos'
ls可以带有许多参数,我们可以在ls后面加help参数来了解更多可用的参数
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
省略一大堆...
-a, --all do not ignore entries starting with .
一般来说,- 后面接字符形式参数,-- 后面接单词形式参数
通过最常用的就是ls -l了,可以显示更完整的信息。
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ ls -l
total 24
-rw-rw-r-- 1 marco marco 10 2月 6 23:50 food.md
-rw-rw-r-- 1 marco marco 10 2月 6 23:50 foo.md
-rw-rw-r-- 1 marco marco 12 2月 7 00:00 hello2.txt
-rw-rw-r-- 1 marco marco 6 2月 6 23:57 hello.txt
-rw-rw-r-- 1 marco marco 107 2月 7 00:10 ls.txt
drwxrwxr-x 2 marco marco 4096 2月 6 23:52 'My Photos'
我们可以通过这个命令显示的信息来查看文件的权限、链接、所属人、所属群组、文件大小、修改日期、文件名这些信息。
使用touch创建文件、mv重命名或移动文件、cp拷贝文件、mkdir新建目录、rmdir删除目录、rm删除不需要的目录及文件,详细用法自己去查
还可以使用man命令来获取指定的命令的帮助。
man + [要查询的命令]
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ man ls
重定向redirection
所谓的input和output,也就是输入输出,代表了数据的流动方向。
而输入输出重定向就是改变输入输出的方向
输入方向就是数据从哪里流向程序。数据默认从键盘流向程序,如果改变了它的方向,数据就从其它地方流入,这就是输入重定向。
输出方向就是数据从程序流向哪里。数据默认从程序流向显示器,如果改变了它的方向,数据就流向其它地方,这就是输出重定向。
http://c.biancheng.net/view/942.html
what pipe means is take the output of the program to the left and the output of the program to the right
使用管道( pipe ),我们能够更好的利用文件重定向。 | 操作符允许我们将一个程序的输出和另外一个程序的输入连接起来:
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ ls -l | tail -n1
drwxrwxr-x 2 marco marco 4096 2月 6 23:52 My Photos
通过<>|我们可以做一些有意思的事情
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ curl --head --silent baidu.com | grep -i content-length > length.txt
将baidu.com的HTTP头中的Content-Length数据写入length.txt中
通过cat命令将文件内容打印到输出设备上
marco@marco-virtual-machine:~/dev/pdos/classes/missing-semester$ cat length.txt
Content-Length: 81
系统内核参数
在/sys中有许多系统内核参数。这又是一个全新的世界,敬请探索。