2011-10-14 78 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