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: Val Krem
Subject: Re: [Help-bash] add and expand
Date: Tue, 6 Jun 2017 20:48:25 +0000 (UTC)



Thank you  Greg and Peter,
Here is my issue,

I have two jobs running one after the other (job2 run after job1). Job1 has 
three jobs running within for loop.

val="Four five six"

Job1.
   for a in $val
            do
          Within this loop three jobs are submitted 
        done

Job2. This job should run after the three jobs completed.
My issue to extract the three PIDs and export them as one variable


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

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

The echo statement within the for loop  produced the three PID like

1009
1010
1011

I want this three  PIDs  to be  exported as one variable like, 

test2=$1009,$1010,$1011



Any help is highly appreciated.












On Tuesday, June 6, 2017 6:57 AM, Greg Wooledge <address@hidden> wrote:



On Mon, Jun 05, 2017 at 11:52:32PM +0000, Val Krem wrote:
> What I did not mention in my previous posting was after adding a prefix I 
> want export the expanded variables  as one variable.I amde a littel progress

If you want to join an array into a single string, you use the [*]
expansion inside double quotes.

array=(one two three)
string="${array[*]}"
# This uses the first character of IFS, or space if IFS is unset, as
# the separator between array elements when creating the string.


> set -- one two three

That's not an array, but it's *closer* than what you started with.

> test1="${@/#/Number}"

Are you really trying to do this with the positional parameters instead
of an array?  Well, if so, you still want to use the * instead of the @,
because you're creating a single string instead of expanding the array
(or pseudo-array of positional parameters) as a list.

string="${*/#/Number}"

Note the difference between @ and *:

imadev:~$ set -- one two "two and a half"
imadev:~$ args "${@/#/Number}"
3 args: <Numberone> <Numbertwo> <Numbertwo and a half>
imadev:~$ args "${*/#/Number}"
1 args: <Numberone Numbertwo Numbertwo and a half>


> echo $test1

QUOTES!

echo "$test1"


> How do I wrap this in quotation mark and treat it as one variable?

It already IS.  Though you still should use * and not @, because you
are joining.


> echo ${test2}

QUOTES!

USE MORE QUOTES!


echo "$test2"

Curly braces are NOT a substitute for quotes.


array=(one two three)
export JOINED="${array[*]/#/Number}"

That's all you need.


Note that when the child process receives this string, it *cannot*
un-join it back into its constituent elements.

array=(one two "two and a half")
export JOINED="${array[*]/#/Number}"
declare -p JOINED

read -ra array <<< "$JOINED"
declare -p array

You do not get back what you started with.  You CANNOT get back what
you started with.  The space separator can appear inside an array
element, and then you get ... that.

This is why you do not export arrays through the environment.  Not unless
you can serialize them in some reversible way.  Simple space-joining is
not such a way.

The easiest way to serialize an array reversibly is to dump it out with
a NUL byte after each element.  This data stream may be strred in a
file, or sent through an open file descriptor (e.g. stdout).  It CANNOT
be stored in a variable, because bash variables cannot store the NUL
byte.  (That's why NUL can be used as a delimiter, after all.)

What are you ACTUALLY trying to do?


reply via email to

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