Linux Bash 函数条件执行用户输入
Linux Bash function conditional execution user input
我正在尝试读取用户输入(例如是和否)并执行一个工作正常的函数,但它在执行一次后退出脚本。
当用户输入是或Y键时,我想编写脚本来执行指定的功能,然后重复上一步。
这是脚本。
redosmartcheck () {
read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
case $doit in
y|Y) smartcheck;;
n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
*) echo dont know ;;
esac
}
redosmartcheck
exit 0
如果用户按Y键,脚本执行smartcheck函数并退出脚本。我怎样才能让它重复 redosmartcheck 功能而不是退出?
提前致谢
我建议插入一个while
:
redosmartcheck () {
while [[ $doit == "" || $doit =~ y|Y ]]; do
read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
case $doit in
y|Y) smartcheck;;
n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
*) echo "dont know";;
esac
done
}
redosmartcheck
我正在尝试读取用户输入(例如是和否)并执行一个工作正常的函数,但它在执行一次后退出脚本。
当用户输入是或Y键时,我想编写脚本来执行指定的功能,然后重复上一步。
这是脚本。
redosmartcheck () {
read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
case $doit in
y|Y) smartcheck;;
n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
*) echo dont know ;;
esac
}
redosmartcheck
exit 0
如果用户按Y键,脚本执行smartcheck函数并退出脚本。我怎样才能让它重复 redosmartcheck 功能而不是退出?
提前致谢
我建议插入一个while
:
redosmartcheck () {
while [[ $doit == "" || $doit =~ y|Y ]]; do
read -n1 -p "Do you want to Hot Swap the Hard Disks and reperform the smart test? [y,n]" doit
case $doit in
y|Y) smartcheck;;
n|N) echo "continuing to the next stage to wipe all hard disk drives" ;;
*) echo "dont know";;
esac
done
}
redosmartcheck