help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] add and expand


From: Greg Wooledge
Subject: Re: [Help-bash] add and expand
Date: Tue, 6 Jun 2017 17:06:36 -0400
User-agent: Mutt/1.4.2.3i

On Tue, Jun 06, 2017 at 08:48:25PM +0000, Val Krem wrote:
> I have two jobs running one after the other (job2 run after job1). Job1 has 
> three jobs running within for loop.

Background processes?

> val="Four five six"
> 
> Job1.
>    for a in $val

STOP THIS!  Stop putting "lists" in a string variable with spaces between
things.  Use an array.

>             do
>           Within this loop three jobs are submitted 
>         done

Are you running *background jobs*?  Or what?  What do you do inside the
loop?

> Job2. This job should run after the three jobs completed.

Are they background jobs?  Did you capture their PIDs with $! one by
one?  Where did you store the PIDs?  Or do you simply want to call
"wait" to wait for all the background jobs to complete?

> My issue to extract the three PIDs and export them as one variable

EXPORT?!  Why?

> Here is my attempt
>   for tr in ${val}; 

I thought your loop iterator was "a", not "tr".  Why did you change it?
What does "a" mean?  What does "tr" mean?  Why did you choose these
variables?

>          do 
> 
>            job[${#tr}]= some process;

You are creating an array named job and indexing it by the LENGTH of
the string variable tr?  Huh?  What?

What is "some process"?

Your assignment has a god damned SPACE after the = sign so it doesn't
even work.

>                 tt1="${job[${#tr}]}"     #### gets the PIDs for each job
>             echo $tt1
>         done

Can't even bring myself to comment any more.  So tired.

> The echo statement within the for loop  produced the three PID like
> 
> 1009
> 1010
> 1011

"... BY SOME MIRACLE I have an array with three elements."

Let's just assume you got these values somehow.  They are in an array
named "job".  I can't for the life of me figure out HOW you got them,
because your code isn't even code, and even your *fake* code is a
damned disaster, but let's say you have an array.

job=(1009 1010 1011)

> I want this three  PIDs  to be  exported as one variable like, 
> 
> test2=$1009,$1010,$1011

I told you how to do this already.  You use the [*] expansion in
single quotes to join an array into a string variable, using the
first character of IFS.

IFS=,
export test2="${job[*]}"
unset IFS

Are those dollar signs supposed to be part of the string?  Then you
could do a fancypants expansion.

IFS=,
export test2="${job[*]/#/\$}"
unset IFS


WHY are you exporting this crazy string into the environment with a
useless name like "test2" and inscrutable contents?  What are you doing
with it?

If you just want to launch 3 background processes and wait for them all
to finish, you don't need ANY of this crap.

for i in 1 2 3; do
    ./my background job "$i" &
done
wait

That's it!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]