2011-10-14 41 views
6

我正在努力执行一组存储为数组中字符串的命令行。 我的代码看起来是这样的:执行描述shell命令的字符串数组

arr=("sudo apt-get update" "sudo apt-get install xxx") 
... 
arr=(${arr[@]} "ln -s /path1 /path2") 
etc... 

# Then I loop on the array of command 
for ((i = 0 ; i < ${#arr[@]} ; i++)) 
do 
    eval ${arr[$i]} 
done 

当它循环在阵列上,所述阵列是比存储到它的命令数多。仿佛在我的琴弦的空格中多个元素的数组拆分 一个典型的输出中的诸如此类

usage: sudo -h | -K | -k | -L | -V 

这意味着只有“须藤”从字符串内拍摄的,我不明白为什么!

感谢

+0

你能说明'loc_com'是如何设置/使用的吗? – chown

+0

正如chown所说,你可以在调用之前放置一个echo $ {loc_com [$ i]}。 – aayoubi

+0

您可以发布一个测试案例,可以在没有编辑的情况下工作吗? –

回答

7

使用${#arr[@]}得到(${arr[@]}给人的字数)数组中的项目数。无论是使用eval或反单引号(`)执行命令的工作:

[ 15:20 [email protected] ~ ]$ cat run_yum_test.sh 
#!/bin/bash 

declare -a arr=("sudo yum search zsh" "sudo yum list zsh") 

for ((i = 0; i < ${#arr[@]} ; i++)); do 
    printf "\n**** Running: ${arr[$i]} *****\n\n" 

    # Run each command in array 
    eval "${arr[$i]}" 

    ### using back-ticks works also 
    #RESULT=`${arr[$i]}` 
    ### Check if the command gave any output 
    #if [ -n "$RESULT" ]; then 
    # echo "$RESULT" 
    #fi 
done 

[ 15:20 [email protected] ~ ]$ ./run_yum_test.sh 

**** Running: sudo yum search zsh ***** 

[sudo] password for jon: 
Loaded plugins: presto, refresh-packagekit 
=========================================================================== Matched: zsh =========================================================================== 
zsh-html.i686 : Zsh shell manual in html format 
autojump-zsh.noarch : Autojump for zsh 
fatrat-czshare.i686 : FatRat plugin enabling CZShare.com downloads and uploads 
gromacs-zsh.noarch : GROMACS zsh support 
python-twisted-core-zsh.i686 : Tab completion for Zsh and Twisted Core 
zsh.i686 : A powerful interactive shell 
environment-modules.i686 : Provides dynamic modification of a user's environment 
plowshare.noarch : CLI downloader/uploader for some of the most popular file-sharing websites 

**** Running: sudo yum list zsh ***** 

Loaded plugins: presto, refresh-packagekit 
Available Packages 
zsh.i686                 4.3.10-6.fc13                 updates 

编辑(回答你的评论):

为了 “扩展” 的阵列,把原来的在引号阵列(${arr[@]}),像这样:

arr=("sudo yum list zsh" "sudo yum search zsh") 
arr=("${arr[@]}" "echo 'TEST'") 

这在动作:

[ 16:06 [email protected] ~ ]$ cat run_yum_test.sh 
#!/bin/bash 

arr=("sudo yum list zsh" "sudo yum search zsh") 
arr=("${arr[@]}" "echo 'TEST'") 

for ((i = 0; i < ${#arr[@]} ; i++)); do 
    printf "\n**** Running: ${arr[$i]} *****\n\n" 
    eval "${arr[$i]}" 
done 


[ 16:06 [email protected] ~ ]$ ./run_yum_test.sh 

**** Running: sudo yum list zsh ***** 
[sudo] password for jon: 
Loaded plugins: presto, refresh-packagekit 
Available Packages 
zsh.i686                 4.3.10-6.fc13                 updates 

**** Running: sudo yum search zsh ***** 

Loaded plugins: presto, refresh-packagekit 
=========================================================================== Matched: zsh =========================================================================== 
zsh-html.i686 : Zsh shell manual in html format 
autojump-zsh.noarch : Autojump for zsh 
fatrat-czshare.i686 : FatRat plugin enabling CZShare.com downloads and uploads 
gromacs-zsh.noarch : GROMACS zsh support 
python-twisted-core-zsh.i686 : Tab completion for Zsh and Twisted Core 
zsh.i686 : A powerful interactive shell 
environment-modules.i686 : Provides dynamic modification of a user's environment 
plowshare.noarch : CLI downloader/uploader for some of the most popular file-sharing websites 

**** Running: echo 'TEST' ***** 

TEST 
+0

这很奇怪,因为它适用于'arr =(“sudo apt-get update “”sudo apt-get upgrade“)'但不适用于'arr =(); arr =($ {arr [@]}“sudo ln -s $ HOME/blabla/usr/bin/blabla”)'。它总是一样的:字符串被分割,并且shell对每个单词进行评估...... – renard

+2

@ user996170您需要将它放在引号中,如下所示:'arr =(); arr =(“$ {arr [@]}”“sudo ln -s $ HOME/blabla/usr/bin/blabla”)'。 – chown

+0

非常感谢!有用! – renard