Lecture1: The shell
所有不加exercise的盲目啃理论都是耍流氓!
Topic1: The Shell
What is the shell?
现在随着计算机的高度发展,user interface的种类各式各样,graphical interface, voice interfaces, and even AR/VR.
shell也是interface的一种,是一种textual interface;是用户和计算机之间通信(communicate)连接的桥梁。
常见的shell搭载平台: terminal
shell prompt: execute/ type in arguments;
$User:\rightleftharpoons:PC. $ (中间就是搭载shell)这个桥梁
Using the shell
~$: date
~$: echo hello
我们所输入的这些命令(cmd),某些有参数(arguments),某些有flag(–version / -t)
空格在shell中的影响很大, 如果我们输入的一个参数它是由空格组成的话,我们可以’’或’将我们的参数引用起来。我觉得那种斜杠或者反斜杠的方式很容易记混淆
How does shell know how to find the date
or echo
programs?
shell is a programming environment, just like Python or Ruby, and so it has variables, conditionals, loops, functions.
当你在shell 中运行你所输入的cmd的是欧,你实际是write a small bit of code that your shell interprets
如果你所输入的一个命令,即你ask the shell to execute the cmd that doesn’t match any of its programming keywords, 翻译来说,如果你所输入的cmd,不是shell 的built-in cmd,此时shell就会consult an environment variable called $PATH
$PATH
这个环境变量,path the way (in which directories) the shell should search for programs when it is given a cmd;
这个很重要,在L2的lecture notes中也有所体现,c.f.shebang的意义。
1 | ~$: echo $PATH |
which
: find out which file is executed for a given program.
by pass $PATH
by giving the path to the file we want to execute;
1 | ~$: /bin/echo $PATH |
Navigating in the shell
In general, when we run a program, it will operate in the current directory unless we tell it otherwise.
当某些程序加载出问题的时候,debug的思路可以转变为看看有没有运行权限。
** When you get permission denied errors, it is usually because you need to do something as root. **
Connecting programs
c.f. L4 data wranggling
Normally, a program’s input and output are both your terminal. That is, your keyboard as input and your screen as output.
The simplest form of redirection is < file
and > file
. These let you rewire the input and output streams of a program to a file respectively:
You can also use >>
to append to a file. Where this kind of input/output redirection really shines is in the use of pipes.
The |
operator lets you “chain” programs such that the output of one is the input of another
A versatile and powerful tool
Operations like
|
,>
, and<
are done by the shell, not by the individual program.echo
and friends do not “know” about|
.
|
and>
are set up by the shell
比如我想更改sys/backlight/brightness
这个文件,
我可能在命令行中输入:
echo 500 > brightness
get permission denied;
然后我可能就会尝试:
sudo echo 500 > brightness
still get permission denied;
出现这个still get permission denied的原因就在于,
我输入sudo echo 500 > brightness
是为了执行echo这个程序使用sudo权限,然后500 作为echo程序的arguments,然后把echo程序的输出重定向为brightness这个文件的输入。
既然想brightness这个文件的输入,shell 就要打开这个文件,但是open the brightness file也需要sudo的权限。
解决方式:
在命令之前加入
#
prompt;当prompt 从
$
到#
改变的时候,意味着su的权限;同样也有不直接进入root用户的解决方法,因为进入root用户有的时候可能随机更改了一些问题。
echo 1600 | sudo tee brightness
echo 1600 send it to sudo tee cmd, tee cmd take the 1600(input) send to a standard out put(这里是一个file)
One thing you need to be root in order to do is writing to the sysfs
file system mounted under /sys
.
sysfs
exposes a number of kernel parameters as files, so that you can easily reconfigure the kernel on the fly without specialized tools.
Note that sysfs does not exist on Windows or macOS.
Exercises and Review
|
v.s>
第六题中,Run the command by explicitly starting the
sh
interpreter, and giving it the filesemester
as the first argument, i.e.sh semester
. Why does this work, while./semester
didn’t?是跟根目录有关吗?
不是呀,是跟 执行权限有关系,昨天才把chmod命令反应过来今天就忘了真的是…
-rwxr--r-- 1 chixinning staff 61 3 5 14:12 semster
其实这个就是一个9+2位的permission bits,前3位是文件所有者,中3位是同组,后3位是其他用户。剩下2位设置组标识位和用户标示位。
chmod(“filename”,0644)就是对应的是permission bits.
6的2进制表示是110即对应rw-(1可以0不可以的逻辑嘛)
这样的话我可以用chmod命令更改一下执行权限,这也是昨天在exercise中遇到的permission bits!
视频真心常看常新!
复习的工作总是辛苦的!