2013-03-16 72 views
0

我收到以下錯誤語法錯誤:擊:靠近意外的標記'其他」

./adduser.sh: line 21: syntax error near unexpected token `else' 
./adduser.sh: line 21: `  else' 

我已經困在這裏了一個小時,我只是不明白。

#!/bin/bash 
#========================================================================================================================================== 
# Script Name: adduser.sh 
# By:   Tim mayo 
# Date:   3/2013 
# Purpose:  Add a user to the Linux system 
# Command line: adduser.sh 
# 
#========================================================================================================================================== 
     read -p "Enter username : " username 
     read -s -p "Enter password : " password 
     egrep "^$username" /etc/passwd >/dev/null 
     if [ $? -eq 0 ]; then 
       echo "$username exists!" 
       exit 1 
     else 
       pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) 
       useradd -m -p $pass $username 
       [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" 
     fi 
       else 
         echo "Root user can only add a user to the Linux system" 
         exit 2 
fi 
+0

你可以把整個腳本請在identation表明有缺失的部分。 – 2013-03-16 19:10:48

回答

1

else關鍵字不與任何if語句關聯;您在第20行用fi關鍵字結束了if聲明。也許您在兩個read聲明之前缺少if

+0

非常簡單的感謝指出。如果[$(id -u)-eq 0];我忘了添加;然後...在讀取語句之前 – user2177712 2013-03-16 19:13:16

+0

@ user2177712歡迎來到SO!如果有任何答案適合您,請考慮通過單擊答案左側的空心綠色勾號來接受它。 *(低於上下的選票)* – jkshah 2013-11-19 10:14:37

0

另一個「運行僅根」例如:

#!/bin/bash 


if [ $UID -eq "0" ] 
then 
     read -p "Enter username : " username 
     read -s -p "Enter password : " password 
     egrep "^$username" /etc/passwd >/dev/null 
     if [ $? -eq 0 ]; then 
       echo "$username exists!" 
       exit 1 
     else 
       pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) 
       useradd -m -p $pass $username 
       [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" 
     fi 
       else 
         echo "Root user can only add a user to the Linux system" 
         exit 2 
fi