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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need a Bash script for iterating thru an array and running a command
# 1  
Old 12-05-2018
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 08:54 AM..
# 2  
Old 12-05-2018
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  
Old 12-05-2018
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 12-06-2018
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  
Old 12-06-2018
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  
Old 12-06-2018
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 12-10-2018
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

3. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies

4. Shell Programming and Scripting

Question on iterating array elements

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 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

Bash Script Iterating Question

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? (3 Replies)
Discussion started by: totoro125
3 Replies

6. Shell Programming and Scripting

Bash Command To Delete Number from Array

Hi, I am writing a script to split a log file - the log could contain multiple days worth of logs. The second line of the log contains the string "Version ". In my test log which comprises of two days worth of logs, this string appears twice - once each day. Essentially I would like to split... (7 Replies)
Discussion started by: pmurray21
7 Replies

7. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

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... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

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" ... (19 Replies)
Discussion started by: canishk
19 Replies

9. Shell Programming and Scripting

running bash command inside awk

Org file 192.168.1.10 d:\adir\xdir 192.168.1.11 d:\bdir\ydir want to covert it into robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\adir robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\bbdir (5 Replies)
Discussion started by: ydk
5 Replies

10. Shell Programming and Scripting

Running function or command concurrently in a bash script

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,... (8 Replies)
Discussion started by: dj_bridges
8 Replies
Login or Register to Ask a Question