"wget: command not found" 正在下载到 $PATH; wget 已安装
"wget: command not found" downloading to $PATH; wget is installed
我希望这个 Bash 脚本能够:
- 读取文件内容并将其分配给
URLS
- 提示用户输入下载内容的路径,分配给
PATH
- 遍历 URL
- 使用 wget 将内容下载到 PATH。
这是我目前得到的:
#!/bin/bash
URLS=$(<urls.txt)
read -e -p "Please enter the path of the directory you'd like to save your files to: " PATH
for link in $URLS
do
wget --background --tries=45 $URLS --directory-prefix $PATH
done
exit 0
我的直觉是我没有正确使用 $PATH
和 wget
,因为如果我注释掉 --directory-prefix $PATH
,脚本将 运行 正确。
在此先感谢您的帮助。
PATH
是一个保留意义的环境变量:用于查找要执行的二进制文件的位置。
使用非保留名称以避免覆盖它,例如path
;此约定是 explicitly given in the POSIX specification 以避免覆盖对系统有意义的环境变量。加强调引用:
Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names. Uppercase and lowercase letters shall retain their unique identities and shall not be folded together. The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities.
...这甚至与未明确 export
环境的 shell 变量有关,因为分配给 shell 变量的名称与环境变量重叠(如PATH
)隐式覆盖后者
因此,您的代码可能如下所示:
#!/bin/bash
read -e -p "Please enter the path of the directory you'd like to save your files to: " path
while IFS= read -r link; do
wget --background --tries=45 --directory-prefix "$path" "$link"
done <urls.txt
另请参阅:
- BashFAQ #001,描述从 bash.
读取文件的最佳实践
- DontReadLinesWithFor,明确描述了使用
for
迭代要解释为面向行的数据的注意事项。
此外,请注意 zsh 不是 POSIX 兼容的 shell;它与标准的精神和意图不同的方面之一是它也给予 path
特殊待遇,如果 zsh 兼容性,使得 path
作为 PATH
的替代品不安全需要。
我希望这个 Bash 脚本能够:
- 读取文件内容并将其分配给
URLS
- 提示用户输入下载内容的路径,分配给
PATH
- 遍历 URL
- 使用 wget 将内容下载到 PATH。
这是我目前得到的:
#!/bin/bash
URLS=$(<urls.txt)
read -e -p "Please enter the path of the directory you'd like to save your files to: " PATH
for link in $URLS
do
wget --background --tries=45 $URLS --directory-prefix $PATH
done
exit 0
我的直觉是我没有正确使用 $PATH
和 wget
,因为如果我注释掉 --directory-prefix $PATH
,脚本将 运行 正确。
在此先感谢您的帮助。
PATH
是一个保留意义的环境变量:用于查找要执行的二进制文件的位置。
使用非保留名称以避免覆盖它,例如path
;此约定是 explicitly given in the POSIX specification 以避免覆盖对系统有意义的环境变量。加强调引用:
Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names. Uppercase and lowercase letters shall retain their unique identities and shall not be folded together. The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities.
...这甚至与未明确 export
环境的 shell 变量有关,因为分配给 shell 变量的名称与环境变量重叠(如PATH
)隐式覆盖后者
因此,您的代码可能如下所示:
#!/bin/bash
read -e -p "Please enter the path of the directory you'd like to save your files to: " path
while IFS= read -r link; do
wget --background --tries=45 --directory-prefix "$path" "$link"
done <urls.txt
另请参阅:
- BashFAQ #001,描述从 bash. 读取文件的最佳实践
- DontReadLinesWithFor,明确描述了使用
for
迭代要解释为面向行的数据的注意事项。
此外,请注意 zsh 不是 POSIX 兼容的 shell;它与标准的精神和意图不同的方面之一是它也给予 path
特殊待遇,如果 zsh 兼容性,使得 path
作为 PATH
的替代品不安全需要。