【问题标题】:curl: (3) URL using bad/illegal format or missing URLcurl: (3) URL 使用错误/非法格式或缺少 URL
【发布时间】:2021-09-08 19:15:59
【问题描述】:

我尝试添加一个数组 -> 读取文件名,然后下载此文件。我还添加了 2 个名称作为忽略列表(有效),但如果我想在 while 循环中下载文件,我会收到此错误:

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

我的代码试试:

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

如果我使用 echo 而不是 curl,我会得到正确的 URL(不带 -o)...

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

我的意图: 无法在 umod.org 上获取任何版本数据。所以我在新版本的主插件后下载核心系统更新。之后,插件将被自动下载并覆盖。

【问题讨论】:

  • shellcheck.net检查你的脚本
  • 如果您的代码在语法上是正确的,它将重新下载插件目录中找到的任何插件文件,如果它不是CupboardLimitNotifier.csChatResponder.cs。除了来自 KamilCuk 的完全恰当的评论,重新下载你已经拥有的插件似乎是不恰当的。你能澄清一下你的代码的意图吗?
  • @lumpa:不要到处放一个echo,它无论如何都不能提供可靠的信息,而是用set -x运行你的程序并分析你得到的输出。
  • 请注意您的帖子中有智能引号; 如果那些在实际脚本中,它们会导致问题。

标签: bash


【解决方案1】:

这是您的脚本的语法正确的固定版本。

阅读 cmets 以获得每一步的解释。

在 URL 中按原样使用文件名是不安全的。它们需要进行编码,因此只有允许的字符构成 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

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多