这是一个由 2 部分组成的问题。
场景:该脚本位于 cronjob 上。如果文件夹不存在,系统会向我们发送一封电子邮件,打开一个票证,通知我们该文件夹不可用。我们必须手动登录并删除前面的文件夹 atm。
我希望我们能够手动执行脚本并通过按“Y”删除前面的文件夹,或者在我们登录并手动执行脚本时按“Enter”键继续。
这就是我到目前为止所拥有的......
#-- check to see if cache folder exists
{ echo "Checking to see if ...";
echo "${wDir}/${client%/}/.ftp-vendor-scripts/cache exists ... "; echo ""; } >> "$log"
if [ ! -d "${wDir}"/"${client%/}"/.ftp-vendor-scripts/cache ]; then
echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache DOES NOT exists - Failed ..." >> "$log";
if [ ******** this script is being executed manually ******* ]; then
echo "Would you like to delete the ${wDir}/${client%/}/.ftp-vendor-scripts folder?"
echo "Press \"Y\" to delete the ${wDir}/${client%/}/.ftp-vendor-scripts."
echo "Press \"Enter\" to continue without deleting the .ftp-vendor-scripts folder."
else
echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache DOES NOT exists - Failed ..." | mail -s "${wDir}/${client%/}/.ftp-vendor-scripts/ca$
fi
else
echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache exists - Success ..." >> "$log";
fi
答案1
你需要这样的东西:
#!/usr/bin/env sh
if [ -t 1 ]
then
interactive=1
else
interactive=0
fi
if [ "$interactive" -eq 1 ]
then
printf "interactive\n"
while true
do
printf "Rm directory? "
read -r reply
if [ "$reply" = "y" ]
then
printf "directory will be removed\n"
break
elif [ "$reply" = "n" ]
then
printf "directory will not be removed\n"
break
else
printf "Uknown reply - it must be either y or n\n"
fi
done
else
printf "non interactive\n"
fi
上面的脚本是POSIX
兼容的,并使用shellcheck
.它将检查它是否在interactive
或non interactive
模式下运行,可能通过cron
并会采取相应的行动。我已经用 、 和 对其bash
进行dash
了Busybox ash
测试FreeBSD
。