【问题标题】:Execute an array of string describing shell command执行描述shell命令的字符串数组
【发布时间】:2011-10-14 21:25:13
【问题描述】:

我正在努力执行一组以字符串形式存储在数组中的命令行。 我的代码如下所示:

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

这意味着只有“sudo”是从字符串中取出的,我不明白为什么!

谢谢

【问题讨论】:

  • 你能说明loc_com是如何设置/使用的吗?
  • 正如 chown 所说,你能在调用它之前放一个 echo ${loc_com[$i]} 吗?
  • 您能否发布一个无需编辑也能正常工作的测试用例?
  • @chown 抱歉,我弄错了,它是 'arr' 而不是 'loc_com',我也错过了循环中的一个 '#'

标签: bash


【解决方案1】:

使用${#arr[@]} 获取数组中的项目数(${arr[@]} 给出字数)。使用 eval 或反引号 (`) 执行命令有效:

[ 15:20 jon@host ~ ]$ 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 jon@host ~ ]$ ./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 jon@host ~ ]$ 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 jon@host ~ ]$ ./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

【讨论】:

  • 对,这很奇怪,因为它适用于arr=("sudo apt-get update" "sudo apt-get upgrade"),但不适用于arr=(); arr=(${arr[@]} "sudo ln -s $HOME/blabla /usr/bin/blabla")。总是一样的:字符串被拆分,shell 对每个单词进行 eval...
  • @user996170 你需要把它放在引号里,像这样:arr=(); arr=("${arr[@]}" "sudo ln -s $HOME/blabla /usr/bin/blabla").
  • 要追加到一个数组,使用+=操作符:arr+=( "another command" )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多