意外的“then"bash 脚本 [英] Unexpected `then' bash script
问题描述
我该如何解决这个错误?我看不出我的语法有什么问题.
How do I fix this error? I can't see anything wrong with my syntax.
ipcheck() {
echolog "[INFO] Enabling IP Forwarding..."
sudo echo 1 > /proc/sys/net/ipv4/ip_forward
if[$(cat /proc/sys/net/ipv4/ip_forward) == "0"]
then
echolog "[CRITICAL] Could not enable IP Forwarding!"
exit 0
fi
echolog "[INFO] IP Forwarding successfully enabled!"
}
我知道这是一个非常基本的脚本,但它是更大脚本的一部分.错误发生在 then 语句上.
I know this is a very basic script, but it's part of a bigger one. The error happens on the then statement.
推荐答案
如果您来自其他编程语言(阅读:C),Shell 脚本往往比您可能习惯的对空格更加敏感.您的 if
行有问题.您可能正在寻找:
Shell scripting tends to be a lot more whitespace sensitive than you might be used to if you've come from other programming languages (read: C). Your if
line has the problems. You are probably looking for:
if [ $(cat /proc/sys/net/ipv4/ip_forward) == "0" ]
这里要记住的是 [
不是任何特殊 if
语法的一部分 - 它是程序的名称(有时是外壳内置).如果你这样想,你就可以看到命令行解析器是如何需要将它分开的.类似地,[
命令(或内置命令)希望结束的 ]
与其其他参数分开,因此您也需要在它之前留一个空格.
The thing to remember here is that [
is not part of any special if
syntax - it's the name of a program (sometimes a shell builtin). If you think of it like that, you can see how the command line parser needs it to be separated. Similarly, the [
command (or builtin) expects the closing ]
to be separated from its other arguments, so you need a space before it, too.
这篇关于意外的“then"bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!