执行一个描述shell命令的字符串数组 [英] Execute an array of string describing shell command

查看:75
本文介绍了执行一个描述shell命令的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力执行一组作为字符串存储在数组中的命令行.我的代码如下:

I'm struggling to execute a set of command lines stored as strings in an array. My code looks like this :

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

当它遍历数组时,该数组大于存储在其中的命令数.好像字符串中的空格将数组拆分为更多元素典型的输出是这样的

When it loops over the array, the array is larger than the number of command stored into it. As if the blank spaces in my strings split the array in more elements A typical ouput is such like

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

这意味着仅从字符串中获取"sudo",而我不明白为什么!

That means only 'sudo' is taken from within the string and I don't understand why!

谢谢

推荐答案

使用 $ {#arr [@]} 获取数组中的项数( $ {arr [@]} 给出字数).使用 eval 或反引号(`)执行命令可以正常工作:

Use ${#arr[@]} to get the number of items in the array (${arr[@]} gives the word count). Using either eval or back-ticks (`) to execute the command works:

[ 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


编辑(回答您的评论):


Edit (to answer your comment):

要扩展"数组,请将原始数组( $ {arr [@]} )用引号引起来,如下所示:

To "extend" the array, put the original array (${arr[@]}) in quotes, like so:

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

这篇关于执行一个描述shell命令的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆