How to pass the output of one command as the command-line argument to another?

So I have a script that, when I give it two addresses, will search two HTML links:

echo "http://maps.google.be/maps?saddr=$1&daddr=$2" | sed 's/ /%/g'

I want to send this to wget and then save the output in a file called temp.html. I tried this, but it doesn’t work. Can someone explain why and/or give me a solution please?

#!/bin/bash
url = echo "http://maps.google.be/maps?saddr=$1&daddr=$2" |  sed 's/ /%/g'
wget $url

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use backticks (`) to evaluate a command and substitute in the command’s output, like:

echo "Number of files in this directory: `ls | wc -l`"

In your case:

wget `echo http://maps.google.be/maps?saddr=$1&daddr=$2 | sed 's/ /%/g'`

Method 2

You could use “xargs”. A trivial example:

ls -1 *.c | sort -n | xargs cat

You would have to take care that xargs doesn’t split its stdin into two or more invocations of the comman (“cat” in the example above).

Method 3

you’re not actually executing your url line :

#!/bin/sh
url="$(echo http://maps.google.be/maps?saddr=$1&daddr=$2 | sed 's/ /%/g')"
wget $url

Method 4

It seems you could use a combination of the answers here. I’m guessing you are wanting to replace space chars with their escaped ASCII values in the URL.

To do this, you need to replace them with %20, not just %. Here’s a solution that should give you a complete answer:

$ wget $(echo http://maps.google.be/maps?saddr=$1&daddr=$2 | sed -e 's/ /%20/g') -q -O temp.html
  • The $( ... ) indicate that the enclosed command should be interpreted first, and the result sent to wget. Notice I escaped the space and % chars in the sed command to prevent them from being misinterpreted.
  • The -q option for wget prevents processing output from the command being printed to the screen (handy for scripting when you don’t care about the in-work status) and the -O option specifies the output file.

FYI, if you don’t want to save the output to a file, but just view it in the terminal, use - instead of a filename to indicate stdout.

Method 5

xargs is the best option to place output from a command into the argument of another command.

Suppose the output of command1 is 3 and you want your next command to take this 3 as an argument, you will want something like this

command2 3(which is output of 1st command) 4 5

So, for this you can do like so

command1 | xargs -I{} command2 {} 4 5

Where 4 and 5 are the other two arguments that may be needed for command2.

You can place the curly brackets in the position where you want the output from first command.

So, use

command1 | xargs -I{} command2 {}

Method 6

wget also accepts stdin with the - switch.

If you want to save the output in a file, use the -O switch.

echo http://maps.google.be/maps?saddr=$1&daddr=$2 | sed 's/ /%/g' | wget -i- -O temp.html


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x