执行 if...then...fi 时出现 Cygwin 错误 - Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11489982/
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 02:46:10  来源:igfitidea点击:

Cygwin error when doing if...then...fi - Bash script

linuxbashshellif-statementcygwin

提问by Igor Chubin

I just started bash shell programming. I have installed Cygwin on my Windows 7 computer. I started writing a script using this tutorial. My script requires an argument to be passed to it, if it doesn't it should display an error message. This is the code directly from the tutorial and the same one in my code:

我刚开始 bash shell 编程。我已经在我的 Windows 7 计算机上安装了 Cygwin。我开始使用本教程编写脚本。我的脚本需要一个参数传递给它,如果没有,它应该显示一条错误消息。这是直接来自教程的代码,与我的代码中的代码相同:

[ $# -eq 0 ] && { echo "usage: someArgument";  exit 1; }

When I do the following:

当我执行以下操作时:

sh ./myscript.sh

it gives me this error:

它给了我这个错误:

-bash: /home/Me/myscript.sh: line 5: syntax error: unexpected end of file

"Unexpected end of file" means I have missed a fior done, etc. somewhere.

“意外的文件结尾”意味着我在某处错过了fidone等。

I double-checked my code, but found nothing. Plus, when I comment out the line the script works perfectly fine.

我仔细检查了我的代码,但一无所获。另外,当我注释掉该行时,脚本运行良好。

I tried using a if...then...fi:

我尝试使用if...then...fi

if [ $# -eq 0 ]
then
    echo "usage: someArgument"
    exit 1;
fi

but to no avail!

但无济于事!

Please help, it's been stumping me.

请帮忙,它一直困扰着我。

回答by Igor Chubin

Probably you have \ror some other invisible symbols in the script. Please add set -xto the beginning of the script to switch debug on and run script once again.

\r脚本中可能有或其他一些不可见的符号。请添加set -x到脚本的开头以打开调试并再次运行脚本。

回答by Igor Chubin

Sorry for the trouble. I did some research on the invisible symbols mentioned by Igor. You have to run dos2unixin Cygwin which solved it for me. Dos-type files have \r\ninstead of \nand some other stuff which makes Cygwin confused.

抱歉,添麻烦了。我对伊戈尔提到的隐形符号做了一些研究。你必须dos2unix在 Cygwin 中运行,它为我解决了这个问题。Dos 类型的文件有\r\n而不是\n和其他一些让 Cygwin 感到困惑的东西。

This:

这个:

$ dos2unix ./myscript.sh
$ sh ./myscript.sh

works correctly.

工作正常。

回答by chepner

As an alternative, you can use bashparameter expansion to test for an unset variable. With no positional parameters, $1will be unset:

作为替代方案,您可以使用bash参数扩展来测试未设置的变量。如果没有位置参数,$1将取消设置:

: ${1?"usage: someArgument"}

The :command is a null-op, serving as a hook to expand its argument, which is $1. If $1is unset, the word following the ?will undergo some expansion and be printed to standard error, after which a non-interactive shell will exit.

:命令是一个空操作,用作扩展其参数的钩子,即$1. 如果$1未设置,则后面的单词?将进行一些扩展并打印为标准错误,之后非交互式 shell 将退出。

(This isn't a solution to your DOS line-ending problem, but just another approach to enforcing that a variable is set.)

(这不是解决 DOS 行结束问题的方法,而只是强制设置变量的另一种方法。)