Linux脚本无法正常工作
问题描述:
我有一个脚本,由于某种原因,它似乎跳过了一步。Linux脚本无法正常工作
[2]) echo "Delete a User"
read -p "What is the user that you would wish to delete?" username
egrep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
read -p "Do you want to delete their home directory also? 1(yes)/2(no)" home
else
echo "That user does not exist"
sleep 3
if [ $home -eq 1 ]; then
userdel -r $username
else
userdel $username
if [ $? -eq 0 ]; then
echo "$username deleted."
sleep 3
else
echo "$username was not deleted."
sleep 3
fi fi fi
;;
它的工作起来,我问,如果用户希望他们的主目录删除或不。如果我打yes或no,它只是跳过并转到脚本的菜单..
答
这是您的脚本正确缩进时的外观。你可以看到,你从用户输入有关删除主目录后,一切都否则将不会得到执行下
echo "Delete a User"
read -p "What is the user that you would wish to delete?" username
egrep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
read -p "Do you want to delete their home directory also? 1(yes)/2(no)" home
else
echo "That user does not exist"
sleep 3
if [ $home -eq 1 ]; then
userdel -r $username
else
userdel $username
if [ $? -eq 0 ]; then
echo "$username deleted."
sleep 3
else
echo "$username was not deleted."
sleep 3
fi
fi
fi
它可能应该是这样的
echo "Delete a User"
read -p "What is the user that you would wish to delete?" username
egrep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
read -p "Do you want to delete their home directory also? 1(yes)/2(no)" home
else
echo "That user does not exist"
sleep 3
fi
if [ $home -eq 1 ]; then
userdel -r $username
else
userdel $username
if [ $? -eq 0 ]; then
echo "$username deleted."
sleep 3
else
echo "$username was not deleted."
sleep 3
fi
fi
答
缩进你的代码,使问题很明显:
read -p "What is the user that you would wish to delete?" username
egrep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
read -p "Do you want to delete their home directory also? 1(yes)/2(no)" home
# <-- 2
else
echo "That user does not exist"
sleep 3
if [ $home -eq 1 ]; then # <-- 1
userdel -r $username
else
userdel $username
if [ $? -eq 0 ]; then
echo "$username deleted."
sleep 3
else
echo "$username was not deleted."
sleep 3
fi
fi
fi
,我已经标记为<-- 1
线,以及其后面的所有行似乎都属于标记为<-- 2
的位置 - 测试$home
的值只有在读取值后才有意义。