我正在围绕mail
编写一个包装脚本。程序中有一个函数需要循环回主菜单,但在声明函数之前,程序只退出到主提示符。这是代码:
function restart ()
{
m
}
clear
echo Working...
echo If you are prompted for your sudo password or asked if you want to continue, then you are being
echo prompted to install mailutils. This is normal upon first-time use, or
echo use on a computer without mailutils installed.
echo
echo Starting in 5 seconds...
sleep 5
echo Examining dependencies...
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils
echo Starting client...
function m ()
{
clear
echo Welcome to the Terminal GMail Client, or TGC!
echo Please enter your gmail address:
read acc
name=${acc%@*}
echo Welcome, $name! Would you like to read[R] or write[W] emails?
read opt
if [ $opt=="R" ] || [ $opt=="r" ]
then
echo Working...
sleep 1
clear
mail -u $acc -p
restart
elif [ $opt=="W" ] || [ $opt=="w" ]
then
clear
echo Working...
sleep 1
clear
echo Enter the subject here:
read sub
echo Enter the recipients address here:
read rec
echo Enter carbon copy [CC] here or leave blank for none:
read cc
echo Enter blind carbon copy [Bcc] here or leave blank for none:
read bcc
echo Enter the body of the email here:
read body
echo Sending to $rec...
mail -s $sub -c $cc -b $bcc --user=$acc $rec "$body"
echo Done! Going to main menu in 2 seconds...
sleep 2
restart
fi
}
你知道,没有错误,我在第15行之后,在“正在启动客户......”之后回到了提示。
答案 0 :(得分:2)
正如其他人在评论中指出的那样:不需要多个shell函数和递归 - 一个简单的while
循环将。
以下是您的代码的修订版本,其中包含正确的引用和基本的错误处理。 您的脚本需要更多的输入验证和错误检查才能经受住实际使用的考验。
但也许这会让你开始。
#!/usr/bin/env bash
clear
echo 'Working...'
echo 'If you are prompted for your sudo password or asked if you want to continue, then you are being
prompted to install mailutils. This is normal upon first-time use, or
use on a computer without mailutils installed.'
echo 'Starting in 5 seconds...'
sleep 5
echo 'Examining dependencies...'
dpkg -l | grep -qw mailutils || sudo apt-get install mailutils || exit
clear
echo 'Starting client...'
while true; do
echo 'Welcome to the Terminal GMail Client, or TGC!'
echo 'Please enter your gmail address:'
read -r acc
name=${acc%@*}
echo "Welcome, $name! Would you like to read[R] or write[W] emails or quit[Q]?"
read -r opt
case $opt in
r|R)
echo 'Working...'
sleep 1
clear
mail -u "$acc" -p || { echo "ERROR: Please try again." >&2; continue; }
;;
w|W)
clear
echo 'Working...'
sleep 1
clear
echo 'Enter the subject here:'
read -r sub
echo "Enter the recipient's address here:"
read -r rec
echo 'Enter carbon copy [CC] here or leave blank for none:'
read -r cc
echo 'Enter blind carbon copy [Bcc] here or leave blank for none:'
read bcc
echo 'Enter the body of the email here:'
read -r body
echo "Sending to $rec..."
mail -s "$sub" -c "$cc" -b "$bcc" --user="$acc" "$rec" "$body" || { echo "ERROR: Please try again." >&2; continue; }
echo 'Done! Going to main menu in 2 seconds...'
sleep 2
;;
q|Q)
echo 'Goodbye.'
exit 0
;;
*)
echo 'ERROR: Unknown command. Please try again.' >&2
;;
esac
done