我想通过 shell 脚本要求用户输入数据库名称、数据库用户等数据。
当用户无意中按下回车键时,输入为空白。我如何使输入成为必需的?如果输入为空,脚本会再次询问该问题?
我当前的脚本,但可能无法按我的预期运行。
#!/bin/bash
# Clear terminal first
clear
echo -n "Your Database Name : "
read dbname
if [ "$dbname" = "" ]; then
echo "ANSWER CANNOT BE BLANK!"
echo -n "Your Database Name : "
read dbname
else
... another code here
但这并不排除用户没有意外(甚至可能是故意)按下回车键的可能性——我必须做出多少 if else 条件?
最佳答案
如果输入不为空,您可以使用无限循环和break
:
while true; do
echo -n "Your Database Name: "
read dbname
if [[ "$dbname" != "" ]]
then
echo "dbname = $dbname"
break
fi
done
关于linux - 如何要求用户输入,这应该是必需的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39576390/