[英]How to prompt user for sudo password?
我的问题是,在运行此bash脚本时做出选择后,如何实施要求sudo密码的操作。 例如,如果用户选择在/目录中进行搜索,则脚本将以拒绝权限返回。 我想弄清楚如何让脚本提示输入密码,然后继续运行。
#!/bin/bash
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}
selection=
where_selection=
what_selection=
until [ "$selection" = "3" ]; do
echo -e "Where would you like to search
1- Root
2- Home
3- Exit
Enter your choice ---> \c"
read selection
case $selection in
1) cd / ; press_enter ;;
2) cd /home ; press_enter ;;
3) echo "Have a great day!" ; exit ;;
esac
echo "What is the name of the file you would like to search for?"
read -r a
if find . -name "$a" -print -quit | grep -q .
then
echo "You found the file"
else
echo "You haven't found the file"
fi
done
您可以做这样的事情,尽管有点麻烦:
#!/bin/bash
function press_enter
{
echo ""
echo -n "Press Enter to continue"
read
clear
}
selection=
where_selection=
what_selection=
sudo=""
until [ "$selection" = "3" ]; do
echo -e "Where would you like to search
1- Root
2- Home
3- Exit
Enter your choice ---> \c"
read selection
case $selection in
1) cd / ; sudo="sudo"; press_enter ;;
2) cd /home ; sudo=""; press_enter ;;
3) echo "Have a great day!" ; exit ;;
esac
echo "What is the name of the file you would like to search for?"
read -r a
if $sudo find . -name "$a" -print -quit | grep -q .
then
echo "You found the file"
else
echo "You haven't found the file"
fi
done
基本上$sudo
是一个空字符串,除非用户选择1,否则它将运行“ sudo”。 更好的方法是让第二个if
块在需要时使用sudo运行命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.