119

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
LotoLo
  • 606
  • For debugging something like this checking your variable values (by echo-ing them to the terminal) often gets you to the solution quickly. – kasterma Dec 13 '10 at 13:52

7 Answers7

106

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'`
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
43

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

  • 1
    I ended up here, because I couldn't remember this command name. My favorite use case was indeed: xargs -I % some_command --input=% -e -t -c (I put it here as a note for others that follow the same trace) – mpasko256 Dec 12 '17 at 21:22
  • 1
    Hi @Bruce Ediger! I randomly stumbled across you when googling this :D Hope you're doing well! – frederix May 18 '20 at 20:49
  • 2
    Hey @frederix who let you in here? I see they will let just anyone in here these days! LOLOLOLOL – abgordon May 18 '20 at 20:53
18

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
OneOfOne
  • 1,394
6

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.

AdminBee
  • 22,803
SethG
  • 314
  • 2
  • 4
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 {} 
  • Can u elaborate? Give a example... Because i tried it here and did'nt work. I did'nt understand. I tried: ps -fA | grep {} | head -n 1 | xargs -I{} lsof -i:80 | awk -F ' ' '{if(NR>1)print $2}' {}

    It did'nt work... Command lsof + awk gets PID of proccess, and ps + grep outputs what proccess has this PID.

    – Raul Chiarella May 03 '22 at 17:35
  • Errors:

    → awk: cannot open {} (No such file or directory) → xargs: lsof: terminated by signal 13

    – Raul Chiarella May 03 '22 at 17:35
3

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
wag
  • 35,944
  • 12
  • 67
  • 51
0

Note: I prefer the xargs option over the next one.


Following is another solution using a function that provides the output of the first command argument as elements into the $i variable to the second command argument

Definition

my_lambda() { for i in "$1"; do eval ${2}; done; }

Usage

my_lambda "$(find ~/Documents | grep *.pdf)" "ls -l $i"
weshouman
  • 121
  • 1
    'eval' is a common misspelling of 'evil'. If eval is the answer, surely you are asking the wrong question. See http://mywiki.wooledge.org/BashFAQ/048 – Gilles Quénot Jan 01 '23 at 02:22