0

I've read this and this, but didn't make it work.

Here's my script:

env -i MY_KEY=$my_key MY_ACCOUNT=$my_account command-that-outputs-a-list \
    | grep "name" | awk -v src=$src \
    '{a = substr($2,2, length($2)-3); print "my_copy_cmd -s xx-yy/"src"/"a" -d . "}'

and it output things like:

my_copy_cmd -s xx-yy/source-file-name -d .
my_copy_cmd -s xx-yy/source-file-name -d .
my_copy_cmd -s xx-yy/source-file-name -d .
my_copy_cmd -s xx-yy/source-file-name -d .

Now I'd juste like to execute it in the same line, but xargs makes all incoming into one line of arguments (and it doesn't run it). xargs -0 doesn't work either. And I'd like to run it using env -i to run it in an environment without variables, and to set temporarily my env variables (= like I did at the very first command), something like (which doesn't work):

env -i MY_KEY=$my_key MY_ACCOUNT=$my_account command-that-outputs-a-list \
    | grep "name" | awk -v src=$src \
    '{a = substr($2,2, length($2)-3); print "my_copy_cmd -s xx-yy/"src"/"a" -d . "}' \
    | xargs env -i MY_KEY=$my_key MY_ACCOUNT=$my_account 
0

2 Answers 2

1

Does this work:

env -i MY_KEY=$my_key MY_ACCOUNT=$my_account command-that-outputs-a-list \
    | grep "name" | awk -v src=$src \
    '{a = substr($2,2, length($2)-3); print "my_copy_cmd -s xx-yy/"src"/"a" -d . "}'|while read z;do $z 2>&1 ;done

The while loop will read each whole line from the output, and just execute it.

Sign up to request clarification or add additional context in comments.

7 Comments

It just does this once = the first line
If you try to debug with |while read z;do echo $z;done does it give you all of the lines again? If it does, then I would assume your first execution of my_copy_cmd exits with an error, which somehow prevents the rest of the copy commands to run.. or right after the run of the whole command use echo $? to see if the result is 0
Yes, it gives me all the "transformed lines" again (ready to be executed!) and the result of echo $? is 0
If the echo $z gives the proper output, which is the same as the one you see above, then the while loop parses each line properly.. now why your my_copy_cmd is not executing, I do not have enough details to say. If you manually execute in your shell the my_copy_cmd does it work ? with all the parameters? and the echo $? after it is again 0 ?
It's very strange: if I copy / paste line by line they work, but if I copy the whole lines then paste them in a shell, only the first one is executed. FYI it's launching az (azure command utility) under Linux
|
0

Try piping the output into sh or bash. You can set variables by adding the assignment before the command:

$ echo 'echo variable is $my_var' | my_var=foo bash
variable is foo

So something like this should work:

env -i MY_KEY=$my_key MY_ACCOUNT=$my_account command-that-outputs-a-list \
    | grep "name" | awk -v src=$src \
    '{a = substr($2,2, length($2)-3); print "my_copy_cmd -s xx-yy/"src"/"a" -d . "}' \
    | MY_KEY=$my_key MY_ACCOUNT=$my_account bash

I find that this is a pretty good trick to assemble and preview a batch of commands before actually executing them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.