【问题标题】:Error in simple shell script简单的 shell 脚本中的错误
【发布时间】:2019-01-28 16:22:18
【问题描述】:

我是 shell 脚本的初学者,并在脚本下面写了,但它不断返回“完成”不是识别标记。

我知道这是一个超级简单的脚本,但我太新手无法调试它.. 谁能解释为什么这不起作用?谢谢!

  #!/bin/sh

    for file in `ls`
    do
    if [ -f $file ]
            echo $file + " exists"
    fi
    done

评论:添加了fi,但仍然有错误:意外标记'fi'附近的语法错误

【问题讨论】:

  • 但是shell脚本不需要分号。
  • 另外,我不认为$file + " exists" 会输出你所期望的,因为它会打印一个文字“+”号。您可以简单地写:echo $file exists。或者,如果您想让消息更明确:echo "$file exists"
  • @dsetton 是的,谢谢!
  • @pandagrammer 你可能会发现shellcheck 很有用。它试图为这些事情提供更多有用的错误消息。

标签: linux bash shell


【解决方案1】:

您错过了then(并且您的echo 语句可能格式不正确)。试试:

#!/bin/sh

for file in `ls`
do
  if [ -f $file ]
  then
    echo "$file exists"
  fi
done

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多