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;








5















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









share|improve this question
























  • @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











  • @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


















5















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









share|improve this question
























  • @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











  • @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














5












5








5








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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, 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











  • 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, 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











1 Answer
1






active

oldest

votes


















8














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.






share|improve this answer


















  • 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











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
);



);













draft saved

draft discarded


















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









8














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.






share|improve this answer


















  • 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















8














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.






share|improve this answer


















  • 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













8












8








8







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company