我正在编写一个 bash 脚本来下载 imgur 相册以用于目的或学习 bash。到目前为止我已经写了这么多但是当我运行它时我得到这个输出:
Enter the URL of the imgur album you would like to download: imgur.com
./imgur_dl.sh: line 25: Validating: command not found
./imgur_dl.sh: line 26: Getting: command not found
./imgur_dl.sh: line 30: imgur.com: command not found
到目前为止,这是我的代码。
#!/bin/bash
url=""
id=""
get_id(){
echo "Getting Album ID..."
local url=$1
echo $url
echo "Successflly got Album ID..."
return
}
validate_input(){
echo "Validating Input..."
local id=$1
echo $id
echo "Input Validated Successfully..."
return
}
get_input(){
read -p "Enter the URL of the imgur album you would like to download: " url
echo $url
$(validate_input url)
$(get_id url)
return
}
$(get_input)
我做错了什么或者我没有得到什么? 我在 macOS 上工作,它完全有帮助。
最佳答案
只需直接调用函数,例如:
valide_input $url
等等。
#!/bin/bash
url=""
id=""
get_id ()
{
echo "Getting Album ID..."
local url=$1
echo $url
echo "Successflly got Album ID..."
return
}
validate_input ()
{
echo "Validating Input..."
local id=$1
echo $id
echo "Input Validated Successfully..."
return
}
get_input ()
{
read -p "Enter the URL of the imgur album you would like to d ownload: " url
echo $url
validate_input $url
get_id $url
return
}
get_input
此外,正如其他人所建议的,您可以通过将 $Url 放在双引号内来改善这一点,例如
validate_input "$url"
因此它处理其他无效的 url。
关于Bash 脚本 "Command Not Found Error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46695443/