-4

I'm trying to write a shell script which creates a user then ask the user which group name the user wants to assign the user which they have just created to and at the end it needs to ask if the user wants to create another user.

I keep getting a line 21: syntax error near unexpected token 'else' line 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
  • 5
    Too many else's! See shellcheck.net for a handy online tool for syntax checking – Jeff Schaller Mar 29 '16 at 17:41
  • 5
    Also have too many fi's. You need a 1-to-1 matchup of if's to fi's and no more than one else for each if. – doneal24 Mar 29 '16 at 17:48
  • @Doug O'neal Would you helping me correct my code please? I'm confused as i'm new to creating shell scripts. Thanks for the feedback I appreciate the help! – qedqejhdeqjd Mar 29 '16 at 18:15
  • 2
    I think that Doug has already pointed out explicit problems with your code, and Jeff has suggested some place to go for automated syntax checking. We're not going to write your code for you. – larsks Mar 29 '16 at 19:22
  • No one is being sarcastic. There are many many examples of correct code on this site. Try doing what has been suggested and taking out the extra "else" and extra "fi" and you will not get the same syntax error. (-1; this question does not show research effort.) – Wildcard Mar 29 '16 at 21:13
0

How about this:

#!/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
0

this should work:

#!/bin/bash

anotherUser() {
     read -p -r "Do you want to add another user? [y/n] yn" yn
     if [[ $yn = \*[yY]\* ]]; then
        checkUser
     fi
     return 1
}

checkUser() {
while :
     do
      read -p -r "Enter username you would like to generate: " userName
      if id "$userName" >/dev/null; then
         echo "Sorry user exists"
         anotherUser 
      else
         echo adduser "$userName"
         printf "User %s has been added\n" "$userName"
         read -p -r "Enter group you want to assign user to: " userGroups
         useradd -G "$userGroups" "$userName" &&
         printf "User %s has been added\n"  "$userName"
        return 0
     fi
done

}

checkUser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.