予期しないトークン 'else'の近くに構文エラーがあり、間違ったインデントがある可能性があります。

予期しないトークン 'else'の近くに構文エラーがあり、間違ったインデントがある可能性があります。

ユーザーを作成してから、ユーザーに作成したユーザーを割り当てるグループ名を入力し、最後に別のユーザーを作成するかどうかを尋ねるシェルスクリプトを作成しようとしています。

Line 21: 予期しないトークン "else" の近くで構文エラーが発生し続けます。ライン21「else」

#!/usr/bin/env bash

anotherUser() {
     read -p "Do you want to add another user? [y/n] yn"
     if [[ $yn = *[yY]* ]]; then
        checkUser
     fi
     exit
}
checkUser() {
while :
     do
      read -p "Enter username you would like to generate: " userName
      read -s -p "Enter password : " userPass
      if id "$userName" >/dev/null; then
         echo "Sorry user exists"
         anotherUser 
      else
         echo adduser "$userName"
         printf "User %s has been added\n" "$userName"
      else
         read -p "Enter group you want to assign user to: " userGroups
         useradd -G "userGroups" "$userName" &&
         printf "User %s has been added\n"  "$userName"
            fi
            break
    done
            exit
     fi
done
}
checkUser

ベストアンサー1

これはどうですか:

#!/usr/bin/env bash

checkUser() {
    while true; do
       read -p "Enter username you would like to generate: " userName
       if id "$userName" >/dev/null 2&>1; then
          echo "Sorry user exists"
       else
          read -s -p "Enter password : " userPass
          echo adduser "$userName"
          printf "User %s has been added\n" "$userName"
          read -p "Enter group you want to assign user to: " userGroups
          useradd -G "userGroups" "$userName" &&
          printf "User has been added to group %s\n"  "$userGroups"
       fi
       read -p "Do you want to add another user? [y/n] " yn
       if [[ $yn = *[nN]* ]]; then
          break
       fi
    done
}
checkUser

おすすめ記事