curl: (3) URL using bad/illegal format or missing URL

Viewed 5332

I tryed to add an array -> read filenames and after them download this file. Also i added 2 names as ignor list (works), but if i want to download the files in an while loop i get this error:

curl: (3) URL using bad/illegal format or missing URL

My code try:

plugins=()
for filename in $(ls $HOME/serverfiles/oxide/plugins/)
do
plugins+=($filename)
done;
count=0
while [ "x${plugins[count]}" != "x" ]
do


if [ “${plugins[count]}” = “CupboardLimitNotifier.cs” ]
      then 
         count=$(( $count + 1 ))
      elif [ “${plugins[count]}” = “ChatResponder.cs” ]
        then
          count=$(( $count + 1 ))
      else
         curl https://umod.org/plugins/${plugins[count]} -o ${plugins[count]}
         count=$(( $count + 1 ))
  fi
done

If i put echo instead of curl i get the right URL for it (without -o)...

https://umod.org/plugins/AdminRadar.cs
https://umod.org/plugins/EntityOwner.cs

My intend: Its not possible to fetch any versions data on umod.org. So i download after new version of main plugin the core system update. After them automaticly the plugins will be downloaded and overwritten.

1 Answers

Here is a syntactically correct, fixed version of your script.

Read the comments to get explanation at each step.

File names are unsafe to use as-is in URLs. They need to be encoded so, only allowed characters composes the URL.

#!/usr/bin/env bash

plugins_dir="$HOME/serverfiles/oxide/plugins"
dl_url='https://umod.org/plugins'

# Need extended globbing for negative patterns
shopt -s extglob

# Be sure not to iterate the pattern itself if no match found
shopt -s nullglob

# Iterate all matches not named CupboardLimitNotifier.cs or ChatResponder.cs
# in "$plugin_dir"
for plugin_path in "$plugins_dir/"!(CupboardLimitNotifier.cs|ChatResponder.cs)
do
  # Skip if plugin_path is not a file
  [ -f "$plugin_path" ] || continue

  # Extract the file name from the full path
  filename="${plugin_path##*/}"

  # Use curl to url encode the file name so it becomes suitable
  # for use within an URL
  urlencoded_filename="$(
    curl -so /dev/null -w '%{url_effective}' \
      --get --data-urlencode "$filename" ''
  )"

  # Strip the leading /? to keep only the url encoded filename
  urlencoded_filename="${urlencoded_filename#/?}"
  curl \
    --output "$plugin_path" \
    --url "$dl_url/$urlencoded_filename"
done
Related