意外'然后'bash脚本 [英] Unexpected `then' bash script

查看:93
本文介绍了意外'然后'bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决这个问题?我看不出什么毛病我的语法。

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!"
}

我知道这是一个非常基本的脚本,但它是一个更大的一部分。发生在当时的声明中的错误。

I know this is a very basic script, but it's part of a bigger one. The error happens on the then statement.

推荐答案

shell脚本,往往比你可能被用来如果你来自其他编程语言中的很多空白敏感(读:C)。你的如果线有问题。您可能正在寻找:

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" ]

这里要记住的一点是, [不是什么特别的如果语法的一部分 - 这是程序的名称的(有时一个shell内建)。如果你觉得它的样子,你可以看到在命令行解析器怎么需要它来进行分离。同样, [命令(或内置)预计结束] 来从它的其他参数分开,所以你需要一个空间之前这一点。

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.

这篇关于意外'然后'bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆