又是我。
非常感谢,您所有的绝妙技巧都非常有用!也许您可能想把我的屁股踢过去。
在设置Raspberry的过程中,我想防止通过ssh进行root登录。与往常一样,它是一个“小脚本”(由跑步者调用)。
我的研究告诉我,即使在systemd时代,/ etc / ssh / sshd_config也是要修改的文件。到目前为止,一切都很好。以我的拙劣理解,需要做的是:逐行读取配置文件,匹配“ PermitRootLogin yes”(空格匹配);如果不匹配,则将该行写入另一个文件,如果是,则将其替换为“ PermitRootLogin no”,然后写入另一个文件,最后用新文件替换原始配置文件,然后通过systemd东西重新启动sshd。
在Perl中,我将读取整个文件,替换()行,然后将内容写回到另一个文件中。但是正如年轻的佛教徒所说:“没有Perl!”
请仅Bash(2)。
并且,一如既往,在此先感谢您!
卡斯滕
如果我直接使用单个命令对退出代码进行简单测试:
[user@localhost ~]$ date
Tue Sep 8 14:00:11 CEST 2020
[user@localhost ~]$ echo $?
0
[user@localhost ~]$ dat
bash: dat: command not found...
[user@localhost ~]$ echo $?
127
Run Code Online (Sandbox Code Playgroud)
但是,如果我想将它们放入 if 语句中,我发现对于退出代码 0,输出正常,但是,对于“命令未找到”代码(应该是 127),它返回“1”。
[user@localhost ~]$ date
Tue Sep 8 14:02:18 CEST 2020
[user@localhost ~]$ if [ $? -eq 0 ]; then echo "Success: $?"; else echo "Failure: $?" >&2; fi
Success: 0
[user@localhost ~]$ dat
bash: dat: command not found...
[user@localhost ~]$ if [ $? -eq 0 ]; then echo …Run Code Online (Sandbox Code Playgroud) 我可以在像这样的 bash 语句中使用函数而不是常规模式、字符串等吗case?
#!/bin/bash
var="1"
is_one () {
[$var -eq 1]
}
case $var in
is_one)
"var is one"
;;
*)
"var is not one"
break
;;
esac
Run Code Online (Sandbox Code Playgroud) if后面是thenbash,但我不明白为什么then不能在同一行if [...] then中使用它,因为必须在下一行中使用它。这样可以消除代码中的歧义吗?或bash是这样设计的?其根本原因是什么?
我试着写if,并then在同一行,但它给了以下错误:
./test: line 6: syntax error near unexpected token \`fi'
./test: line 6: \`fi'
Run Code Online (Sandbox Code Playgroud)
代码是:
#!/bin/bash
if [ $1 -gt 0 ] then
echo "$1 is positive"
fi
Run Code Online (Sandbox Code Playgroud)