Need a Bash script for iterating thru an array and running a command

Hi ,
I am a total beginner so bear with me.

I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below.
I need to run this in a Jenkins script , so I would need below
bash script to run interactively in the command line
an escaped version to run in Jenkins build

This does not work . basically does not execute

pids=(2567538 2356789); export pids;echo "pid = ${pids[*]}";arr=($(echo "${pi
ds[*]}" | tr " " "\n")); echo "arr = ${arr[*]}" ; for pid in $arr do echo $pid
 done;
>

Bash version details

$ bash -version
GNU bash, version 4.4.12(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later

This works

$ pids=(2567538 2356789); export pids;echo "pid = ${pids[*]}";arr=($(echo "${pids[*]}" | tr " " "\n")); echo "arr = ${arr[*]}" ;

I'm not sure what you're trying to do with your code, you're way overthinking it. All you need is text and a basic loop.

Version which is just text:

VAR="2567538 2356789"
for x in $VAR # Note VAR is not quoted here
do
        echo "x=$x"
done

People jump straight into bash arrays convinced that everything needs to be an array when much of the time it's not really need at all.

Note how much more complicated the array use is:

ARR=( 1234 5678 )

for x in "${ARR[@]}" # Note, requires special syntax and quoting
do
        echo "x=$x"
done
1 Like

I solved this myself .

cids=(`sudo docker ps -aq`); export cids;echo "cids = ${cid[@]}"; for containerId in "${cids[@]}" ; do  sudo docker rm -f "${containerId}"; done'

--- Post updated at 04:21 PM ---

Thanks for the response!. The approach without using the array is very helpful. What would be the change if the delimiter is not spaces

Stupid question, but why not:

sudo docker rm -f $(docker ps -aq)

Andrew

3 Likes

The shell has a special variable you can set, IFS, to change its internal delimiter. It is not any sort of regex, only a list of characters which are valid "splitters". By default it is space, newline, and tab. It's only used for variable quoting purposes and some builtins like read. So:

OLDIFS="$IFS" ; IFS=","
VAR="1,2,3,4,5,6,7,8,9,10"
for X in $VAR
do
        echo $X
done
1 Like

Off topic, but may be pertinent to @SVRao19056.

As you are using docker, you probably already know that the output of some commands can be formatted with GO Templates:

docker images --format '{{json .}}'
docker images --format '{{json .Tag}}'

The examples above use the GO template to format in JSON format. I strongly recommend that you install the command line utility jq to use with this output. Not only does it allow for readable formatting of the JSON, but it allows for drilling down into the data more easily than just reading the output. For example, the keywords of the output of docker images :

docker images --format '{{json .}}' | head -1 | jq '.|keys'

Find the keys in the IPAM component of a network:

docker network inspect --format '{{json .IPAM}}' cf06ecfeb5f0 | jq '.|keys'

You can only access one component in docker so you have to use jq:

docker network inspect --format '{{json .IPAM}}' cf06ecfeb5f0 | jq '.Config,.Driver'
docker network inspect --format '{{json .}}' cf06ecfeb5f0 | jq '.IPAM|.Config,.Driver'

and so on.

Andrew

Andrew ,
You suggested a simpler solution . I am a newbie and simpler alternatives are appreciated