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

Tags
array, bash, command, ha, jenkins ad cli, newbies, script, thank you

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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; 1 Week Ago at 08:54 AM..
# 2  
Old 1 Week Ago
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:
rbatte1 (1 Week Ago)
# 3  
Old 1 Week Ago
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  
Old 1 Week Ago
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:
Corona688 (1 Week Ago), rbatte1 (1 Week Ago), RudiC (1 Week Ago)
# 5  
Old 1 Week Ago
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:
rbatte1 (1 Week Ago)
# 6  
Old 1 Week Ago
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  
Old 5 Days Ago
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 to Reply

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

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem running plsql using printf command on bash shell kamauv234 Shell Programming and Scripting 1 09-27-2018 01:33 AM
Running options in bash script Cody Learner Shell Programming and Scripting 6 01-15-2018 01:12 PM
Array compare bash script batchenr Shell Programming and Scripting 6 07-05-2017 01:18 PM
Array problem in Bash Script cogiz UNIX for Beginners Questions & Answers 4 10-30-2016 02:10 PM
Question on iterating array elements newbie_01 Shell Programming and Scripting 3 05-15-2015 10:35 AM
Bash Script Iterating Question totoro125 Shell Programming and Scripting 3 10-17-2014 01:07 AM
Bash Command To Delete Number from Array pmurray21 Shell Programming and Scripting 7 09-12-2013 06:16 PM
Bash shell script to check if script itself is running hedkandi Shell Programming and Scripting 8 11-05-2012 01:48 AM
script running by sh but not by bash 120945 Shell Programming and Scripting 5 05-23-2012 11:18 AM
Running set options from the command line and bash command Straitsfan UNIX for Dummies Questions & Answers 5 07-28-2011 12:27 PM
Validate BASH script before running it... tjones1105 Shell Programming and Scripting 7 12-27-2009 01:36 AM
bash Script: Issue with iterating Directory and store into array canishk Shell Programming and Scripting 19 08-26-2009 11:01 AM
running bash command inside awk ydk Shell Programming and Scripting 5 06-24-2009 10:17 AM
Running function or command concurrently in a bash script dj_bridges Shell Programming and Scripting 8 02-05-2009 07:58 AM
Script running in bash 3.0 not in 3.2 trutoman Shell Programming and Scripting 9 01-28-2009 07:53 AM
All times are GMT -4. The time now is 11:11 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.