0

i have the following bash code:

function () {
   curl="$(curl -s "$2" >> "$1")"
   version="$(grep '$3' $1 | $4 )"
   echo $version
}

function "test" "https://google.com" "String" "cut -d' ' -f3 | cut -d'<' -f1"

Basically the function downloads the page and then uses grep to look for a specific string. After that "cut", cuts down the results further. But ... unfortenatly the cut inside the function doesn't work. I only get the following output:

"usage: cut -b list [-n] [file ...]
       cut -c list [file ...]
       cut -f list [-s] [-d delim] [file ...]"

Maybe i overlooked something ... or maybe you have a better idea :-)

Stfn
  • 11
  • 2
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Nov 12 '18 at 19:25
  • 1
    `function` is a bash builtin command/keyword. See: `help` – Cyrus Nov 12 '18 at 19:27
  • 1
    I don't understand what you're trying to do with `curl="$(curl ...)"`, as you don't ever use the `curl` variable after that. – Benjamin W. Nov 12 '18 at 20:52
  • yeah, there won't be anything in it anyway. stdout is redirected, and stderr won't be collected into the var. – Paul Hodges Nov 12 '18 at 22:07
  • A yes the curl variable was nonse and the function isn't titled function of course. It was just for the posting here :) – Stfn Nov 13 '18 at 06:55

1 Answers1

0

Since $4 contains shell metacharacters that aren't processed when expanding variables, you need to use eval to execute it.

Also, you need to put $3 in double quotes, not single quotes, otherwise the variable won't be expanded.

   version="$(grep "$3" "$1" | eval "$4" )"
Barmar
  • 741,623
  • 53
  • 500
  • 612