首页 > 解决方案 > 放入 bash 脚本时未找到 wget 命令

问题描述

我编写了以下 bash 脚本:

 #!/bin/sh

echo "Number of command line arguments : $#"
if [ $# == 0 ]; then
   echo "Your command line contains no arguments"
   declare -a arr=("xx.xx.xx.xx" "yy.yy.yy.yy")
else
   declare -a arr=($1)
fi


for i in "${arr[@]}"
do
   URL="https://"$i":8443"
   echo "URL is $URL"
   wget --no-check-certificate $URL/heapdump
done

它在第 16 行一直失败: wget: command not found

我找到了一些相关的帖子,但不知道如何解决这个问题。谢谢。

标签: bashpathwget

解决方案


如果您键入命令(不是内部命令或 shell 函数),bash 会在变量中提到的目录中搜索PATH此名称的文件(wget在您的情况下),该文件为运行的用户设置了可执行位脚本。在你的情况下,没有找到合适wget的。

由于wget您的 PATH 中不太可能没有 x 位设置,因此最可能的原因是 PATH 缺少 wget 所在的目录。

您有两个选择:扩展 PATH,或wget在脚本中明确地为该行添加正确路径的前缀,即

/here/is/my/wget --no-check-certificate $URL/heapdump

推荐阅读