Run an array of strings describing the shell command

I'm struggling to execute a set of commands stored as strings in an array. My code is as follows:

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 iterates over an array, the array is larger than the number of commands stored in it. As if the spaces in my lines break the array into more elements A typical output such as

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

This means that only sudo is called from the string, and I don’t understand why!

thanks

+6
source share
1 answer

Use ${#arr[@]} to get the number of elements in the array ( ${arr[@]} gives the number of words). Using eval or back-ticks ( ` ) to execute a command:

 [ 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 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 respond to your comment):

To expand the array, put the original array ( ${arr[@]} ) in quotation marks, for example:

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

Here it is in action:

 [ 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 environment plowshare.noarch : CLI downloader/uploader for some of the most popular file-sharing websites **** Running: echo 'TEST' ***** TEST 
+7
source

Source: https://habr.com/ru/post/899332/


All Articles