How to pipe multiple results into a command?How to do a control loopHow to make xargs ping and head output as expected?Pipe into if statement?Piping find results into another commandAccessing results of pipe as variable?how to pass multiple commands to sqlite3 in a one liner shell commandhow to pipe PID of java app into a command?How to concatenate results of multiple commands and pipe into another without intermediate file?How to access further members of an array when using bash variable indirection?Pipe echo of associative array into dmenu
How to write a vulnerable moment without it seeming cliche or mushy?
Humans meet a distant alien species. How do they standardize? - Units of Measure
Modern approach to radio buttons
Scala list with same adjacent values
Relativistic resistance transformation
What does it mean by "d-ism of Leibniz" and "dotage of Newton" in simple English?
Future enhancements for the finite element method
What if you don't bring your credit card or debit for incidentals?
Is the capacitor drawn or wired wrongly?
Creating Fictional Slavic Place Names
How do I truncate a csv file?
Explain Ant-Man's "not it" scene from Avengers: Endgame
Is there a way to save this session?
How can I offer a test ride while selling a bike?
How do I get a list of only the files (not the directories) from a package?
What should I do about a religious player who refuses to accept the existence of multiple gods in D&D?
Can an old DSLR be upgraded to match modern smartphone image quality
Is the world in Game of Thrones spherical or flat?
Why don't I have ground wiring on any of my outlets?
The most awesome army: 80 men left and 81 returned. Is it true?
How can I grammatically understand "Wir über uns"?
Constructing a CCY Gate
How did the Zip Chip and RocketChip accelerators work for the Apple II?
Bringing Food from Hometown for Out-of-Town Interview?
How to pipe multiple results into a command?
How to do a control loopHow to make xargs ping and head output as expected?Pipe into if statement?Piping find results into another commandAccessing results of pipe as variable?how to pass multiple commands to sqlite3 in a one liner shell commandhow to pipe PID of java app into a command?How to concatenate results of multiple commands and pipe into another without intermediate file?How to access further members of an array when using bash variable indirection?Pipe echo of associative array into dmenu
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id $SharedFileSystem | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "$EFS_SERVER_IPS_ARRAY[@]"
do
echo "$element"
echo "$element $MOUNT_SOURCE" >> /etc/hosts
done
This works but looks ugly. I want to avoid the array variable and the for
loop (basically I don't care about the first echo command).
Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:
long AWS command >> echo $element $MOUNT_SOURCE >> /etc/hosts
with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?
The output of the AWS command is like this:
10.10.10.10
10.22.22.22
Then, the added lines in /etc/hosts
look like:
10.10.10.10 unique-id.efs.us-east-1.amazonaws.com
10.22.22.22 unique-id.efs.us-east-1.amazonaws.com
bash shell-script aws bash-expansion bash-array
|
show 7 more comments
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id $SharedFileSystem | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "$EFS_SERVER_IPS_ARRAY[@]"
do
echo "$element"
echo "$element $MOUNT_SOURCE" >> /etc/hosts
done
This works but looks ugly. I want to avoid the array variable and the for
loop (basically I don't care about the first echo command).
Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:
long AWS command >> echo $element $MOUNT_SOURCE >> /etc/hosts
with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?
The output of the AWS command is like this:
10.10.10.10
10.22.22.22
Then, the added lines in /etc/hosts
look like:
10.10.10.10 unique-id.efs.us-east-1.amazonaws.com
10.22.22.22 unique-id.efs.us-east-1.amazonaws.com
bash shell-script aws bash-expansion bash-array
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
Yes but is this really adding the IP address to your/etc/hosts
? It seems more likely it is just adding the literal numbers0
and1
to it.
– Jesse_b
May 16 at 20:19
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
1
No I'm saying that when an array is called with the$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).
– Jesse_b
May 16 at 20:26
|
show 7 more comments
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id $SharedFileSystem | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "$EFS_SERVER_IPS_ARRAY[@]"
do
echo "$element"
echo "$element $MOUNT_SOURCE" >> /etc/hosts
done
This works but looks ugly. I want to avoid the array variable and the for
loop (basically I don't care about the first echo command).
Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:
long AWS command >> echo $element $MOUNT_SOURCE >> /etc/hosts
with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?
The output of the AWS command is like this:
10.10.10.10
10.22.22.22
Then, the added lines in /etc/hosts
look like:
10.10.10.10 unique-id.efs.us-east-1.amazonaws.com
10.22.22.22 unique-id.efs.us-east-1.amazonaws.com
bash shell-script aws bash-expansion bash-array
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id $SharedFileSystem | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "$EFS_SERVER_IPS_ARRAY[@]"
do
echo "$element"
echo "$element $MOUNT_SOURCE" >> /etc/hosts
done
This works but looks ugly. I want to avoid the array variable and the for
loop (basically I don't care about the first echo command).
Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:
long AWS command >> echo $element $MOUNT_SOURCE >> /etc/hosts
with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?
The output of the AWS command is like this:
10.10.10.10
10.22.22.22
Then, the added lines in /etc/hosts
look like:
10.10.10.10 unique-id.efs.us-east-1.amazonaws.com
10.22.22.22 unique-id.efs.us-east-1.amazonaws.com
bash shell-script aws bash-expansion bash-array
bash shell-script aws bash-expansion bash-array
edited May 17 at 5:02
jwodder
192111
192111
asked May 16 at 20:08
CarmageddonCarmageddon
1316
1316
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
Yes but is this really adding the IP address to your/etc/hosts
? It seems more likely it is just adding the literal numbers0
and1
to it.
– Jesse_b
May 16 at 20:19
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
1
No I'm saying that when an array is called with the$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).
– Jesse_b
May 16 at 20:26
|
show 7 more comments
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
Yes but is this really adding the IP address to your/etc/hosts
? It seems more likely it is just adding the literal numbers0
and1
to it.
– Jesse_b
May 16 at 20:19
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
1
No I'm saying that when an array is called with the$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).
– Jesse_b
May 16 at 20:26
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
Yes but is this really adding the IP address to your
/etc/hosts
? It seems more likely it is just adding the literal numbers 0
and 1
to it.– Jesse_b
May 16 at 20:19
Yes but is this really adding the IP address to your
/etc/hosts
? It seems more likely it is just adding the literal numbers 0
and 1
to it.– Jesse_b
May 16 at 20:19
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
1
1
No I'm saying that when an array is called with the
$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).– Jesse_b
May 16 at 20:26
No I'm saying that when an array is called with the
$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).– Jesse_b
May 16 at 20:26
|
show 7 more comments
1 Answer
1
active
oldest
votes
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts
or, if you prefer,
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq '.MountTargets[].IpAddress' -r | sed -e "s~$~$MOUNT_SOURCE~" >> /etc/hosts
All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq
(top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519366%2fhow-to-pipe-multiple-results-into-a-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts
or, if you prefer,
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq '.MountTargets[].IpAddress' -r | sed -e "s~$~$MOUNT_SOURCE~" >> /etc/hosts
All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq
(top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
add a comment |
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts
or, if you prefer,
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq '.MountTargets[].IpAddress' -r | sed -e "s~$~$MOUNT_SOURCE~" >> /etc/hosts
All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq
(top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
add a comment |
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts
or, if you prefer,
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq '.MountTargets[].IpAddress' -r | sed -e "s~$~$MOUNT_SOURCE~" >> /etc/hosts
All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq
(top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts
or, if you prefer,
aws efs describe-mount-targets --file-system-id $SharedFileSystem
| jq '.MountTargets[].IpAddress' -r | sed -e "s~$~$MOUNT_SOURCE~" >> /etc/hosts
All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq
(top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.
answered May 16 at 20:34
Michael HomerMichael Homer
52.6k9146182
52.6k9146182
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
add a comment |
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
1
1
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :)
– Carmageddon
May 16 at 20:40
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f519366%2fhow-to-pipe-multiple-results-into-a-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added)
– Carmageddon
May 16 at 20:18
Yes but is this really adding the IP address to your
/etc/hosts
? It seems more likely it is just adding the literal numbers0
and1
to it.– Jesse_b
May 16 at 20:19
@Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1?
– Carmageddon
May 16 at 20:21
@Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point?
– Carmageddon
May 16 at 20:25
1
No I'm saying that when an array is called with the
$!name[@]
syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc).– Jesse_b
May 16 at 20:26