2

first of all, I'm quite new with bash scripting and I'm just starting to learn, evidently there's something wrong with this script, but I don't know what it is...

I created a bash script to automate downloading videos with youtube-dl:

#!/bin/bash

echo url:
read url
export url
youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' $url

The idea is that I type in the command line the name of the script, e.g.: "360" and it will ask for a url (e.g.: a Youtube video), I paste it and youtube-dl downloads it with the stated parameters. It works like a charm...

Now, I want to make the script more complex and I think I need to convert the youtube-dl command to a variable (of course, being a newbie, I might be wrong, but let's assume I'm right for a moment...)

#!/bin/bash

video="youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]'"

echo url:
read url
export url
$video $url

When I try this, it throws me an error: "ERROR: requested format not available " I don't know what's wrong... I'd like to solve the problem with the least changes to the code as possible and I repeat, I'd like to know what's wrong with the current code so I can learn from it.

Thank you very much in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tuqueque
  • 31
  • 7

2 Answers2

2

It's explained in detail here here: I'm trying to put a command in a variable, but the complex cases always fail!

First always double-quote your variables, unless you know exactly what will happen if you don't.

You don't need to export that variable: you're not invoking any other program that needs to use it.

When you want to re-use a command, think about putting it in a function:

#!/bin/bash
function video {
    youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' "$1"
}
read -p "url: " url
video "$url"

Actually, I would do this:

  1. add that function to your ~/.bashrc,
  2. source that file: source ~/.bashrc
  3. then you can use it from the command line:

    video 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
    
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Fantastic, it works perfect with several combinations of parameters!... As I said, I'm just beginning to learn bash script and I understand like 10% of what I see on Internet when I search for help... One question about your code modifications, though... Why exactly is needed the "$1" ? – tuqueque Feb 28 '19 at 22:33
  • That represents the first argument you provide for the function. In this case that is how the function receives `"$url"` – glenn jackman Feb 28 '19 at 22:35
  • You could also change `"$1"` to `"$@"` so that you can call it like `video url1 url2 url3 ...` – glenn jackman Feb 28 '19 at 22:37
  • Great, now I understand!... Thank you very much! – tuqueque Feb 28 '19 at 23:18
0

Remove the single quote from the -f parameter it will work.

For eg.

video="youtube-dl -f bestvideo[height<=360]+worstaudio/worst[height<=360]"
error404
  • 2,684
  • 2
  • 13
  • 21