bash 意外标记“do”附近的语法错误

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

syntax error near unexpected token `do'

bashshellbatch-file

提问by Sumit Patel

I am trying to convert a .bat file into .sh file here is my lines of code

我正在尝试将 .bat 文件转换为 .sh 文件,这是我的代码行

#!/bin/sh
./setenv.sh
export _LIBJARS=for i in [$XYZ_HOME$/lib/*.jar];do source $XYZ_HOME$/bin/append.bat i
export CLASSPATH=$ANT_HOME$/lib/ant.jar;$_LIBJARS$$ANT_HOME$/bin/ant -buildfile $XYZ_HOME$/build.xml    

After writing these line I wrote chmod 755 ./build.shthen I wrote ./build.sh test-deploy

写完这些行后我写了chmod 755 ./build.sh然后我写了./build.sh test-deploy

I am getting the following error:

我收到以下错误:

./build.sh: line 3: syntax error near unexpected token `do'
./build.sh: line 3: `export _LIBJARS=for i in [$XYZ_HOME$/lib/*.jar];do source $XYZ_HOME$/bin/append.bat i'

My .Bat file is like

我的 .Bat 文件就像

call setenv.bat

set _LIBJARS=
for %%i in (%XYZ_HOME%\lib\*.jar) do call %XYZ_HOME%\bin\append.bat %%i

set CLASSPATH=%ANT_HOME%\lib\ant.jar;%_LIBJARS%

%ANT_HOME%\bin\ant -buildfile %XYZ_HOME%\build.xml %1 %2 %3 %4

Or any tutorial to understand the lines of code or related to shell and bash file please suggest me.

或者任何了解代码行或与 shell 和 bash 文件相关的教程,请给我建议。

回答by David W.

Bash Shell and Batch are two completely different programming languages with their own syntax and the way they work.

Bash Shell 和 Batch 是两种完全不同的编程语言,它们有自己的语法和工作方式。

For example:

例如:

call setenv.bat

Will run setenv.batin your script and return control to your current script. However:

setenv.bat在您的脚本中运行并将控制权返回给您当前的脚本。然而:

./setenv.sh

Will spawn a new shell, set the environment in that shell, and then return to the parent shell where those environment variables are not set. To do the equivalent in shell, you must sourcein setenv.sh:

将生成一个新的 shell,在该 shell 中设置环境,然后返回到未设置这些环境变量的父 shell。要在 shell 中执行等效操作,您必须在以下位置获取源代码setenv.sh

. ./setenv.sh

OR

或者

source ./setenv.sh

The first is compatible to all Bourne Styleshells. The last is more readable, but may not be compatible with different shells.

第一个兼容所有Bourne Styleshell。最后一个更具可读性,但可能与不同的 shell 不兼容。

set _LIBJARS=
for %%i in (%XYZ_HOME%\lib\*.jar) do call %XYZ_HOME%\bin\append.bat %%i

set CLASSPATH=%ANT_HOME%\lib\ant.jar;%_LIBJARS%

In Shell, you can do this:

在壳牌中,你可以这样做:

export CLASSPATH=%ANT_HOME%\lib\ant.jar
for jar in ($XYZ_HOME/lib/*.jar)
do
    CLASSPATH="$CLASSPATH:$jar"
done

Remember, this is only taking place INSIDE this shell script. Unlike Batch, it's not affect the parent shell, so using $CLASSPATHis fine.

请记住,这仅在此 shell 脚本中发生。与 Batch 不同的是,它不会影响父 shell,因此可以使用$CLASSPATH

Finally:

最后:

$ANT_HOME/bin/ant -buildfile $XYZ_HOME/build.xml $*

The $*will work no matter how many target are passed.

$*会工作,不管有多少目标传递。

回答by konsolebox

This might be how you want it to do:

这可能是您希望它执行的操作:

#!/bin/sh

. ./setenv.sh  # Must 'source' or else you won't get the values.
_LIBJARS=

for I in "$XYZ_HOME/lib/"*.jar; do
    . "$XYZ_HOME/bin/append.sh" "$I"
done

export CLASSPATH=$ANT_HOME/lib/ant.jar:$_LIBJARS
"$ANT_HOME/bin/ant" -buildfile "$XYZ_HOME/build.xml" "" "" "" ""  ## Or perhaps it should simply be "$@"?