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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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

Code:
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
Code:
$ 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
Code:
$ pids=(2567538 2356789); export pids;echo "pid = ${pids[*]}";arr=($(echo "${pids[*]}" | tr " " "\n")); echo "arr = ${arr[*]}" ;


Last edited by rbatte1; 12-06-2018 at 07:54 AM..
# 2  
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:
Code:
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:

Code:
ARR=( 1234 5678 )

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

This User Gave Thanks to Corona688 For This Post:
# 3  
I solved this myself .

Code:
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
# 4  
Quote:
Originally Posted by SVRao19056
I solved this myself .

Code:
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:
Code:
sudo docker rm -f $(docker ps -aq)

Andrew
These 3 Users Gave Thanks to apmcd47 For This Post:
# 5  
Quote:
Originally Posted by SVRao19056
Thanks for the response!. The approach without using the array is very helpful. What would be the change if the delimiter is not spaces
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:

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

This User Gave Thanks to Corona688 For This Post:
# 6  
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:
Code:
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:
Code:
docker images --format '{{json .}}' | head -1 | jq '.|keys'

Find the keys in the IPAM component of a network:
Code:
docker network inspect --format '{{json .IPAM}}' cf06ecfeb5f0 | jq '.|keys'

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

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

and so on.

Andrew
# 7  
Thank you for simper solution!

Andrew ,
You suggested a simpler solution . I am a newbie and simpler alternatives are appreciated
Quote:
Stupid question, but why not:

sudo docker rm -f $(docker ps -aq)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Question on iterating array elements
newbie_01
Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ...... Shell Programming and Scripting
3
Shell Programming and Scripting
Bash Script Iterating Question
totoro125
I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops?... Shell Programming and Scripting
3
Shell Programming and Scripting
Running set options from the command line and bash command
Straitsfan
I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line...... UNIX for Dummies Questions & Answers
5
UNIX for Dummies Questions & Answers
bash Script: Issue with iterating Directory and store into array
canishk
Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ...... Shell Programming and Scripting
19
Shell Programming and Scripting
Running function or command concurrently in a bash script
dj_bridges
I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,...... Shell Programming and Scripting
8
Shell Programming and Scripting