1

I'm trying to create a script to do the following: extract the magnet link from a lynx bookmark and then use it as part of a curl command. like this:

curl http://my_ip:my_port_number/startdownload/magnet_link

I have the following to get the magnet link:

lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep "magnet" | sed 's/&.*//' | cut -c 7-

But I am unsure as to how to glue the two parts together to create a single script.

Can I have some suggestions please?

BTW, I am a total amateur at this and I'm sure that even what have can be improved.

1

So what you need is to capture the output of one command, and use it in another. The bash $(..) syntax will let you execute a command and capture the output, then you can use that output in another command.

One way is to capture the output in a variable, such as:

shortcut=$(lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep magnet | sed 's/&.*//' | cut -c 7-)
curl "$shortcut"

Or, you can use the output directly:

curl $(lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep "magnet" | sed 's/&.*//' | cut -c 7-)

Which you choose depends on your preference. If you need to use the output of the command more than once then it is a good idea to capture the output in a variable and use it from there.

1

put the following lines into a script file, such ad mydownload.sh

magnet=$(lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep "magnet" | sed 's/&.*//' | cut -c 7-)
curl http://my_ip:my_port_number/startdownload/${magnet}

save and exit

chmod 755 mydownload.sh
./mydownload.sh
  • Thanks Mel, this is what I was looking for. Thanks go out to everybody else as well, your answers have been very helpful and informative. – Phil Feb 9 '16 at 20:56
0
bash-4.1$ echo $( echo hi )
hi
bash-4.1$ 

So,

curl $( ... )

Would pass the output of whatever ... is to curl by running whatever those commands are as a subshell. $() can also be written with backticks, though those are header to read, and do not nest.

grep | sed can probably be replaced with a single call to awk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.