关于linux:计算机教育中缺失的一课-MIT-L1-课程概览与-shell

3次阅读

共计 843 个字符,预计需要花费 3 分钟才能阅读完成。

https://missing.csail.mit.edu/
https://missing-semester-cn.g…
https://www.bilibili.com/vide…

笔记

对于重定向和 cat

$ echo hello > hello.txt
$ cat hello.txt
hello
$ cat < hello.txt
hello
$ cat < hello.txt > hello2.txt
$ cat hello2.txt
hello

本认为 cat < hello.txt 会报错 cat: hello: No such file or directory。猜测正确工作的起因是“参数”和“输出”的区别(未经验证或查找材料):cat 程序将输出打印在屏幕上,cat hello.txt 中的 hello.txt 是参数,将该文件的内容作为输出;而 cat < hello.txt 是输出重定向,意思也是将文件中的内容作为程序的输出,而不是将文件的内容作为参数,因而二者成果雷同。

tee 的小用途

$ cd /sys/class/backlight/thinkpad_screen
$ sudo echo 3 > brightness
An error occurred while redirecting file 'brightness'
open: Permission denied

出其不意的是,咱们还是失去了一个错误信息。毕竟,咱们曾经应用了 sudo 命令!对于 shell,有件事咱们必须要晓得。|>、和 < 是通过 shell 执行的,而不是被各个程序独自执行。echo 等程序并不知道 | 的存在,它们只晓得从本人的输入输出流中进行读写。对于下面这种状况,shell (权限为您的以后用户) 在设置 sudo echo 前尝试关上 brightness 文件并写入,然而零碎回绝了 shell 的操作因为此时 shell 不是根用户。

明确这一点后,咱们能够这样操作:

$ echo 3 | sudo tee brightness 

因为关上 /sys 文件的是 tee 这个程序,并且该程序以 root 权限在运行,因而操作能够进行。

课后练习

正文完
 0