【问题标题】:Verification of http:// or https:// in an argument with grep in Bash在 Bash 中使用 grep 验证参数中的 http:// 或 https://
【发布时间】:2022-10-21 14:52:29
【问题描述】:
Input = ./q4.sh https://cdn.eso.org/images/thumb700x/eso1723a.jpg

echo $1 | -Eiq '^https?://' 2> /dev/null || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1

即使我的参数 1 有 http:// 或 https://,输出也总是跳转到最后一个 || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://'

【问题讨论】:

  • 我不能使用 if/fi 或任何其他命令然后 echo/grep 我必须将我的 stderr 重定向到 /dev/null
  • 不需要 2 个单独的 grep。只需一个简单的正则表达式就足够了:https?://
  • 像这样 ?回声 $1 | grep -i "https?://" $1 2> /dev/null || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1 因为它一直把我送到最后 ||即使我的 arg1 有 https:// 或 http:// 的选项

标签: bash grep


【解决方案1】:

这可能会让你走上正确的道路

echo "$1" | grep -Eiq '^https?://' 2>/dev/null || echo "fail"

【讨论】:

  • 你不需要2 >/dev/null。只需将grep -Eq 用于正则表达式和安静模式
  • 是的,这是个好建议。 GOY1M 可以使用它并给他们的教授留个便条,说明他们为什么选择它而不是管道到 /dev/null
  • 您好,感谢您的帮助!现在我将其更改为 echo $1 | grep -Eq "https?://" $1 || echo 'Ce script supporte seulement les URLs commencant par https:// ou http://' && exit 1 但它输出这个 grep:cdn.eso.org/images/thumb700x/eso1723a.jpg: No such file or directory
  • @G0Y1M 这是因为最后一个 $1。请仔细阅读命令。将此与您的进行比较
【解决方案2】:

假设不失一般性,您的脚本使用一个参数调用,即http://something

echo $1 | grep -i "http://" $1 不会在 http://something 输出到管道的字符串 http://something 中查找 http://,因为 grep option(s) regexp argument 会忽略其标准输入,而是读取文件命名为论点。因此它试图阅读一个名为 http://something 的文件这当然不存在。但是,由于您已重定向 2>/dev/null,因此告诉您此错误消息会消失,因此您只会收到抱怨 URL 的消息并退出。

执行echo $1 | grep -i "http://"(对于 https 也是如此)会起作用,但非常笨拙。它还将(匹配的)URL 输出到标准输出,您没有重定向,因此它可能会出现在您的终端上,这可能是也可能不是您想要的。通常,只要参数可能包含空格(或其他 IFS 分隔符)或任何通配符(glob)字符,您就应该使用echo "$1" ...,但在这种特定情况下,有效的 URL 不能做第一个,几乎永远不会做最后一个它不太重要。

还有那个 grep将要匹配并因此接受一个 URL包含 http:// or https:// but does not 首先它,因为回显消息状态是必需的。如果您只想在开头匹配,请在正则表达式中使用^

更有效的解决方案是使用来自 herestring 的输入的单个 grep(^https? 在扩展模式下表示“http 或 https,但仅在开始时”):

grep -Ei "^https?://" <<<$1 || echo "URL must begin ..." && exit 1
# if you don't want the matched URL output on stdout, 
# either redirect [1]>/dev/null or add q to the options (-Eiq) 

如果您可以仅使用小写字母(实际上这是人们总是用于 URL 方案的,即使标准说应该接受大写字母),效率更高(根本没有 grep)是:

case $1 in (http://* https://*) ;; (*) echo "URL must begin ..." ... ; esac

【讨论】:

  • 这是我的完整脚本。根据您的指示,它解决了我在第 3 行的问题,并且脚本的其余部分在没有添加第 3 行的情况下工作,但现在第 3 行工作似乎其余部分在有 http:// 时不会执行或 https:// 在 arg1 grep -Eiq "^https?://" <<<$1 || echo 'Ce script supporte seulement les commencant par https:// ou http://' && exit 1 NOMBRE=echo "$1" | grep -o '/' 2&gt; /dev/null | wc -l NOMBRE=$((NOMBRE+1)) ITEM=echo "$1" | cut -d '/' -f$NOMBRE 2&gt; /dev/null wget -q $1 && echo " Votre fichier a ete telecharge ici: "$PWD/$ITEM
  • @G0Y1M 不要将代码放入 cmets。没有人会读它,尤其是当它被可怕的格式完全扼杀时。如果它与当前问题相关,则编辑并将代码包含在问题本身中,否则打开另一个问题。并且不要在这里使用行号,没有人知道第 3 行在哪里。在该行上添加评论以显示它
【解决方案3】:

作为grep 甚至bash 的正则表达式匹配的替代方案,您可以使用模式匹配。

if [[ $1 != http?(s)://* ]]; then

【讨论】:

    【解决方案4】:

    如果您需要不区分大小写的匹配

    shopt -s nocasematch
    if [[ ! "$1" =~ ^https?:// ]]
    then
        echo 'ERROR' >&2
    fi
    

    【讨论】:

    • grep 也可以进行不区分大小写的匹配,但我仍然更喜欢 grep,无论大小写如何处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多