1

I'm using Manjaro Linux. I've changed my yt-dlp's zsh configuration and now I get

❯ ytvp
deal_with_long_filename:1: command not found: xsel -ob
deal_with_long_filename:2: command not found: xsel -ob

The log shows

Usage: yt-dlp [OPTIONS] URL [URL...]

yt-dlp: error: no such option: --continue --no-overwrites --no-post-overwrites --verbose --restrict-filenames --retry-sleep fragment:exp

Usage: yt-dlp [OPTIONS] URL [URL...]

yt-dlp: error: no such option: --continue --no-overwrites --no-post-overwrites --verbose --restrict-filenames --retry-sleep fragment:exp

Why is it treating all options as a single one?

I've tried running the xsel -ob command on its own and it works fine.

How do I fix this?

I would like to keep the send to background & option that I was using. Would it give problems with the definition of the function deal_with_long_filename?

This is my configuration now

opts="--continue
  --no-overwrites
  --no-post-overwrites
  --verbose
  --restrict-filenames
  --retry-sleep fragment:exp=2:64
  --print-to-file"

if [ -f /usr/local/bin/youtube-dl ]; then yt_dlp="/usr/local/bin/yt-dlp" else yt_dlp="$(which yt-dlp)" fi

If using Mac

if [[ "$(uname -a | awk '{print $1}')" == "Darwin" ]]; then paste="pbpaste" opts="${opts} --ffmpeg-location /usr/local/bin/ffmpeg" else # If using Linux paste="xsel -ob" fi

sanitize_linux_filename() { echo "$1" | sed -e 's/[^a-zA-Z0-9._-]/_/g' }

get_log_name() { TIMESTAMP=$( date +%y%m%d%H%M%S ) NAME=$( sanitize_linux_filename "$1" ) echo "yt-dlp_${TIMESTAMP}_${NAME}.log" }

deal_with_long_filename() { if ! $yt_dlp $opts --output "%(upload_date>%Y-%m-%d|)s%(upload_date& |)s%(title)s-%(id)s.%(ext)s" "$($paste)" >> "/tmp/$LOG_NAME" 2>&1; then $yt_dlp $opts --output "%(upload_date>%Y-%m-%d|)s%(upload_date& |)%(webpage_url_domain)s-%(id)s.%(ext)s" "$($paste)" >> "/tmp/$LOG_NAME" 2>&1 fi }

Video Playlist

ytvp() { LOG_NAME=$( get_log_name "$1" ) opts="${opts} --format '(bv+(wa[abr>=64]/ba))/b' --format-sort res:720,tbr~2000 --no-playlist --download-archive 'downloaded.txt'"

deal_with_long_filename "$1" "$LOG_NAME" }

1 Answers1

1

The error message is correct, there is no command called xsel -ob on your system. The command you're trying to use is xsel, with its -ob options. Since you put the command in a string, it's treated as a single entity.

The issue is similar to the issue described in How can we run a command stored in a variable?

Do store a collection of ordered strings in such a way that they can be used as separate strings, use an array.

opts=(
  --continue
  --no-overwrites
  --no-post-overwrites
  --verbose
  --restrict-filenames
  --retry-sleep fragment:exp=2:64
  --print-to-file
)

To add to an array:

opts+=( --ffmpeg-location /usr/local/bin/ffmpeg )

...

opts+=( --format '(bv+(wa[abr>=64]/ba))/b' --format-sort res:720,tbr~200 --no-playlist --download-archive 'downloaded.txt' )

Then, in the zsh shell, use it as $opts.

You also have the same issue with your paste variable, which should also be an array since you might want to treat it as the two strings xsel and -ob:

paste=( xsel -ob )

There are a number of other things in your script that are needlessly complicated, like using uname to get the OS type:

if [[ $OSTYPE == darwin* ]]; then ...; fi

... or using sed to remove certain characters from a string:

NAME=${1//[^[:alnum:].-]/_}
Kusalananda
  • 333,661
  • I get the exact same error for the options after applying this changes. The command not found is solved though. https://git.disroot.org/hirrolot19/dotfiles/commit/f5661648890cc933f56ecf61dddf0fcadd596c25#diff-6a0a53cfa400a5b85f7791056115eb86a4e7c783 –  Oct 21 '22 at 10:43
  • @DevLloyd That's because you still use $opts as a string further down in the code. – Kusalananda Oct 21 '22 at 11:08
  • So to use it I should do $opts instead of ${opts[@]}? How can I add the last options if I don't use it as a string? >> "/tmp/$LOG_NAME" 2>&1 & –  Oct 21 '22 at 11:40
  • @DevLloyd No, what I meant was that in the functions further down in the code, in yts and the other two functions, you do opts="${opts} ..." when you should be doing opts+=(...). – Kusalananda Oct 21 '22 at 11:48