linux bash:第 18 行:意外标记“fi”附近的语法错误 Q2.sh:第 18 行:“fi”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16697317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:29:50  来源:igfitidea点击:

linux bash: line 18: syntax error near unexpected token `fi' Q2.sh: line 18: `fi'

linuxbash

提问by Joe E

I'm trying to compile this code but I am getting the following error:

我正在尝试编译此代码,但出现以下错误:

Q2.sh: line 18: syntax error near unexpected token `fi'
Q2.sh: line 18: `fi'

at line 15, I'm trying to verify if the parameter numerois bigger than 0. Any help is appreciated

在第 15 行,我试图验证参数numero是否大于 0。任何帮助表示赞赏

Thanks

谢谢

#!bin/bash

numero=

if test $# -eq 0; then
    echo "Argument Manquants"
    exit 1
fi

if ! [[ "$numero" =~ ^[0-9]+$ ]] ; then
    exec >&2; echo "Le parametre doit etre un digit";
    exit 1
fi

if [ $numero -le 0 ]
$$  echo "Le parametre doit etre plus grand que 0"  
    exit 1
fi

cat /etc/passwd grep "$numero"

回答by elyase

You forgot the ; thenpart in your ifclause:

您忘记了条款中的; then部分if

if [ "$numero" -le 0 ]; then
    ...
fi

回答by Ian Knight

It looks like you've missed the thenstatement at the end of your if, and you've only used single square brackets. Try replacing that block for this:

看起来您错过了then末尾的语句if,并且您只使用了单个方括号。尝试为此替换该块:

if [[ $numero -le 0 ]]; then
    echo "Le parametre doit etre plus grand que 0"  
    exit 1
fi

回答by ShweT

I have a similar issue, when i copied the script edited on a windows to unix system. On debut run, I get the same error.

我有一个类似的问题,当我将在 Windows 上编辑的脚本复制到 unix 系统时。在首次运行时,我遇到了同样的错误。

When i edited the file once within the editor on destination m/c, the script ran without issues.

当我在目标 m/c 的编辑器中编辑文件一次时,脚本运行没有问题。

To me it seems to be character encoding issue. But i am still searching for solution for overcoming it in my case.

对我来说,这似乎是字符编码问题。但在我的情况下,我仍在寻找克服它的解决方案。

May be it helps a bit.

可能会有点帮助。