在shell脚本中使用循环连接参数以形成命令
我在这个网站和其他地方尝试过不同的建议,但都没有效果。我需要将一些参数传递给shell脚本,然后将它们连接到字符串上,然后将其作为命令启动。所以我这么做在shell脚本中使用循环连接参数以形成命令,shell,loops,arguments,concatenation,Shell,Loops,Arguments,Concatenation,我在这个网站和其他地方尝试过不同的建议,但都没有效果。我需要将一些参数传递给shell脚本,然后将它们连接到字符串上,然后将其作为命令启动。所以我这么做 command="perl perl_script.pl" for arg do command+="$arg " done eval $command 但是我得到了这个错误 bowtie_script_simple.sh: 43: bowtie_script_simple.sh: comando+=-n : not found bo
command="perl perl_script.pl"
for arg
do
command+="$arg "
done
eval $command
但是我得到了这个错误
bowtie_script_simple.sh: 43: bowtie_script_simple.sh: comando+=-n : not found
bowtie_script_simple.sh: 43: bowtie_script_simple.sh: comando+=3 : not found
根据我看到的其他线索,这应该是可行的。有什么想法吗
谢谢
您的arg是否有(或可能有)空格?为了安全起见,你可能应该进一步引用
您的arg是否有(或可能有)空格?为了安全起见,您可能应该进一步引用。我认为这应该有效:
command="perl perl_script.pl"
for arg in $@
do
command="$command $arg"
done
$command
我认为这应该奏效:
command="perl perl_script.pl"
for arg in $@
do
command="$command $arg"
done
$command
当处理带有空格的参数时,作为字符串连接会带来困难。对于bash/zsh/ksh,使用数组效果更好:
command=(perl perl_script.pl)
for arg; do
command+=("$arg")
done
# or more simply
# command=(perl perl_script.pl "$@")
# now, execute the command with each arg properly quoted.
"${command[@]}"
从您的错误消息中,看起来您正在使用/bin/sh—该shell没有
var+=string
构造—必须使用具有更多功能的shell。将字符串连接为字符串会在处理带有空格的参数时带来困难。对于bash/zsh/ksh,使用数组效果更好:
command=(perl perl_script.pl)
for arg; do
command+=("$arg")
done
# or more simply
# command=(perl perl_script.pl "$@")
# now, execute the command with each arg properly quoted.
"${command[@]}"
从您的错误消息中,看起来您正在使用/bin/sh——该shell没有
var+=string
构造——必须使用具有更多功能的shell。有趣的结果,因为给出的示例代码显示“command”,错误显示“comando+=-n”,这与此不同。。。。你确定这就是可能导致错误的代码吗?哦,是的,对不起,我在这里用英语写的,但我的代码上是西班牙语的。但是代码是一样的,它给出了errortry command=“perl perl_script.pl”有趣的结果,因为给出的示例代码显示“command”,错误显示“comando+=-n”,这是不同的。。。。你确定这就是可能导致错误的代码吗?哦,是的,对不起,我在这里用英语写的,但我的代码上是西班牙语的。但是代码是一样的,它给出了errortry命令=“perl perl_script.pl”是的,有些参数像这样-n3,但我认为shell会将它们作为单独的参数来读取。是的,有些参数像这样-n3,但我认为shell会将它们作为单独的参数来读取。