gpt4 book ai didi

Linux 脚本无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:37 25 4
gpt4 key购买 nike

我有一个脚本,由于某种原因,它似乎跳过了一个步骤。

[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
;;

它可以工作到我询问用户是否希望删除他们的主目录。如果我点击"is"或“否”,它就会跳过并转到脚本菜单。

最佳答案

这就是正确缩进后脚本的外观。您可以看到,在收到用户关于删除主目录的输入后,所有内容都在 else 下,不会被执行

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

关于Linux 脚本无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com