bash 命令在 linux 终端中有效,但在 shell 脚本中无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12337446/
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
commands work in linux terminal but not in shell script
提问by whomaniac
I am trying to run the following shell script using the command:
我正在尝试使用以下命令运行以下 shell 脚本:
$ bash linuxScript.sh
This is what my script looks like
这就是我的脚本的样子
#!/bin/bash
svnroot=.../resources
FINDBUGS_HOME="~/opt/findbugs-2.0.1"
svn help
svn export --force ".../resources/GUI.jar"
opt=
case $opt in
-normal)
echo "normal mode"
java -jar %FINDBUGS_HOME%\lib\findbugs.jar -textui -onlyAnalyze common.-,ufm.- -output UFMGUI_Normal.html -html GUI.jar;;
...
esac
Now for some reason the command svn helpand svn exportdon't work well (I am calling svn help only to emphasize my point).
现在由于某种原因,该命令svn help并svn export不能很好地工作(我调用 svn help 只是为了强调我的观点)。
They let out the following errors:
他们发出了以下错误:
'unknown command: 'help
Type 'svn help' for usage.
' is not properly URI-encodedaire.com/repos/ufm_ui/branches/UFM_3_7/tools/build/resources/GUI.jar
And if I'm already asking it also shouts on non svn related commands:
如果我已经在问它,它也会对非 svn 相关命令大喊大叫:
:command not founde 7:
'inuxScript.sh: line 8: syntax error near unexpected token 'in
'inuxScript.sh: line 8: 'case $opt in
Now the weird thing is that if I type the svn commands in the terminal everything works fine! I tried to google the solution and I also searched this forum but I couldn't find anything useful.
现在奇怪的是,如果我在终端中输入 svn 命令一切正常!我试图用谷歌搜索解决方案,我也搜索了这个论坛,但我找不到任何有用的东西。
Help ill be greatly appreciated. Thankyou!
非常感谢帮助生病。谢谢!
回答by Fumisky Wells
What is the output of the following command?:
$ which bashDoes this show "/bin/bash" as you expected?
What is the output of the following shell script (assume /tmp/x.sh)?:
$ cat /tmp/x.sh #!/bin/bash svn help $ bash /tmp/x.shIt should be "svn help" output.
FINDBUGS_HOME looks environment variable and it looks wrongly be referred at the following line:
java -jar %FINDBUGS_HOME%\lib\findbugs.jar ...Is %FINDBUGS_HOME% right notation? (It looks like DOS notation...) If it meant shell env-var, isn't it be $FINDBUGS_HOME ?
以下命令的输出是什么?:
$ which bash这是否按您的预期显示“/bin/bash”?
以下 shell 脚本的输出是什么(假设为 /tmp/x.sh)?:
$ cat /tmp/x.sh #!/bin/bash svn help $ bash /tmp/x.sh它应该是“svn help”输出。
FINDBUGS_HOME 看起来是环境变量,它在以下行中被错误地引用:
java -jar %FINDBUGS_HOME%\lib\findbugs.jar ...%FINDBUGS_HOME% 是正确的符号吗?(它看起来像 DOS 符号......)如果它意味着 shell env-var,它不是 $FINDBUGS_HOME 吗?
NOTE: $ above is just bash prompt at the same as your post.
注意:上面的 $ 只是与您的帖子相同的 bash 提示。
回答by ymattw
Looks like your script is saved in DOS format, try open with vim and review file format with
看起来您的脚本以 DOS 格式保存,请尝试使用 vim 打开并查看文件格式
:set ff
I guess it will give you 'dos'
我想它会给你“dos”
To convert it to unix format, use
要将其转换为 unix 格式,请使用
:set ff=unix
:w

