Bash 执行描述shell命令的字符串数组

Bash 执行描述shell命令的字符串数组,bash,Bash,我正在努力执行一组以字符串形式存储在数组中的命令行。 我的代码如下所示: 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]}

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

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
这意味着只有‘sudo’是从字符串中提取的,我不明白为什么

感谢使用
${#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
这就是它的作用:

[ 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
[16:06]jon@host~]$cat run\u yum\u test.sh
#!/bin/bash
arr=(“sudo-yum-list-zsh”“sudo-yum-search-zsh”)
arr=(“${arr[@]}”“echo‘TEST’”)
对于((i=0;i<${arr[@]};i++);做
printf“\n****正在运行:${arr[$i]}******\n\n”
评估“${arr[$i]}”
完成
[ 16:06 jon@host~]$/运行\u yum\u test.sh
****正在运行:sudo yum list zsh*****
jon的[sudo]密码:
加载的插件:presto,刷新packagekit
可用软件包
zsh.i686 4.3.10-6.fc13更新
****正在运行:sudo-yum-search-zsh*****
加载的插件:presto,刷新packagekit
====================================================================================================================================================匹配:zsh===========================================================================
zsh-html.i686:html格式的zsh shell手册
自动跳转-zsh.noarch:zsh的自动跳转
fatrat-czshare.i686:fatrat插件支持czshare.com下载和上传
gromacs-zsh.noarch:gromacs-zsh支持
python-twisted-core-zsh.i686:zsh和twisted-core的制表符完成
zsh.i686:一个强大的交互式shell
environment-modules.i686:提供对用户环境的动态修改
plowshare.noarch:CLI下载/上传程序,适用于一些最流行的文件共享网站
****正在运行:echo“测试”*****
试验

你能展示一下
locu com
是如何设置/使用的吗?正如chown所说,你能在调用它之前放置一个echo${loc\u com[$i]}吗。你能发布一个不需要编辑就能正常工作的测试用例吗?@chown抱歉,我犯了一个错误,它是'arr'而不是'loc\u com','我在循环中也漏掉了一个'#',这很奇怪,因为它适用于
arr=(“sudo-apt-get-update”“sudo-apt-get-upgrade”)
但不适用于
arr=();arr=(${arr[@]}“sudo ln-s$HOME/blabla/usr/bin/blabla”)
。它总是一样的:字符串被拆分,shell对每个单词进行求值…@user996170您需要将其放在引号中,如下所示:
arr=();arr=(“${arr[@]}”“sudo ln-s$HOME/blabla/usr/bin/blabla”)
若要附加到数组,请使用
+=
运算符:
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