2014-11-24 191 views
0

我有一個腳本,由於某種原因,它似乎跳過了一步。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,它只是跳過並轉到腳本的菜單..

回答

0

這是您的腳本正確縮進時的外觀。你可以看到,你從用戶輸入有關刪除主目錄後,一切都否則將不會得到執行下

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 
0

縮進你的代碼,使問題很明顯:

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的值只有在讀取值後纔有意義。