I am not familiar with perl. I am reading an installation guide atm and the following Linux command has come up:
perl -p -i -e "s/enforcing/disabled/" /etc/selinux/config
Run Code Online (Sandbox Code Playgroud)
Now, I am trying to understand this. Here is my understanding so far:
-e 只允许执行以下内容
-p 将我跟在 -e 之后的命令放入一个循环中。现在这对我来说很奇怪,对我来说这个命令似乎是想说:将“s/enforcing/disabled/”写入/etc/selinux/config。再说一次,“写入”命令在哪里?这个 -i(内联)有什么用?
-p 变化
s/enforcing/disabled/
Run Code Online (Sandbox Code Playgroud)
相当于
while (<>) {
s/enforcing/disabled/;
print;
}
Run Code Online (Sandbox Code Playgroud)
这是缩写
while (defined( $_ = <ARGV> )) {
$_ =~ s/enforcing/disabled/;
print($_);
}
Run Code Online (Sandbox Code Playgroud)
这是做什么的:
ARGVinto 中读取一行$_。ARGV是一个特殊的文件句柄,它从指定为参数的每个文件中读取(如果没有提供文件,则为 STDIN)。enforcing用disabled。-i,这是一个与程序当前正在读取的文件同名的新文件的句柄。*例如,
$ cat a
foo
bar enforcing the law
baz
enforcing enforcing
$ perl -pe's/enforcing/disabled/' -i a
$ cat a
foo
bar disabled the law
baz
disabled enforcing
Run Code Online (Sandbox Code Playgroud)
* — 在旧版本的 Perl 中,此时旧文件已经被删除,但只要有一个打开的文件句柄,它仍然可以访问。在非常新版本的 Perl 中,这会写入临时文件,该文件稍后将覆盖程序正在读取的文件。