4

I have written a shell script which allows me to create a user and assign them to a group but I keep getting the error line 19: syntax error: unexpected end to file

What am I doing wrong? Would it be possible someone could help me re structure the code to eliminate this problem.

Here is my shell script.

#!/bin/bash
username="U"
group="G"
while [ $username"U" ] >/dev/null 2>&1;
read -p "Please input the username you would like to generate"
if
  id -U $username >/dev/null 2&1;
echo 'User already exists"
while [ $group >dev/null 2>&1; 

echo "group exists"

else
    groupadd $group
fi

closed as off-topic by Jakuje, MelBurslan, Anthon, chaos, vonbrand Mar 22 '16 at 23:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jakuje, MelBurslan, Anthon, chaos, vonbrand
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Dear Poster, Please put your code into edit page and select it, Then press CTRL+K. – PersianGulf Mar 22 '16 at 20:46
  • Please dont post screenshots of text, paste the actual text... – jasonwryan Mar 22 '16 at 20:46
  • 1
    Your while [ condition ]; do... statements need closure with done. if [ condition ] statement needs to be flowed by then keyword. It is not implied. – MelBurslan Mar 22 '16 at 20:46
  • Put the script in here, surrounded by lines containing three backticks like ```. – Henrik Mar 22 '16 at 20:47
  • The indentation of your code is awful. Proper indentation would make it a lot easier to see what is wrong, but @MelBurslan has a lot of it. – Henrik Mar 22 '16 at 20:50
1

This is something you could use. It will remain in the loop until a new non existing user has been created.

#!/usr/bin/env bash

anotherUser() {
   read -p "Add another user? [y/n]" yn
   if [[ $yn = *[yY]* ]]; then
      checkUser
   fi
   exit
}
checkUser() {
while :
   do
      read -p "Enter user: " userName
      if id $userName >/dev/null
         then echo "User exists"
         anotherUser
      else
         adduser "$userName"
         printf "User %s has been added\n" "$userName"
         exit
      fi
   done
}
checkUser
  • This is perfect, thanks for taking the time to help me out. Instead of it keep looping would it be possible if you could add something like do you want to create another user y/n and if the user inputs n it will terminate. – Programmer Mar 22 '16 at 21:51
  • I have updated my answer. I haven't built any checking though, but that's left as exercise for the student! ;) – Valentin Bajrami Mar 22 '16 at 22:02
  • Thanks, what do you mean by checking? Do you mean running the script and seeing if it executes? – Programmer Mar 22 '16 at 22:07
  • I mean the user input. You never know if a user will put a _ or a ` ` <-- space etc. Anyways, you now should have a bit of an idea of what this script does. – Valentin Bajrami Mar 22 '16 at 22:10
  • Oh right. For example I should include something like if user inputs integer ask the question again. – Programmer Mar 22 '16 at 22:15
0

You have to change via $? variable. It stores the return value of last command:

id -u $username > /dev/null 2>&1 ;
if [[ "$?" -eq 0 ]]; then
        echo "username already exists."

If it equals 1, it doesn't exist.
BTW, your blocks are very dirty.... Use the following link:
Indentation (typesetting)
Also close your blocks.

  • Yes I'm aware of the indentations being poor but I'm trying my best. I have visited the hyperlink webpage but since I'm new at this I'm still very confused. Would you mind making some adjustments and pointing me in the right direction with my code please? – Programmer Mar 22 '16 at 21:25
  • @PersianGulf: you don't have to do this I think. if id -u $username >/dev/null 2>&1; then should work. – L. Levrel Mar 22 '16 at 21:34
  • @L. Levrel , I didn't use id command in if statement, please reread my code. – PersianGulf Mar 22 '16 at 21:53
  • @L.Docherty , You can use Advanced bash script by TLDP : tldp.org/LDP/abs/html ,Even you can download it's pdf from tldp.org – PersianGulf Mar 22 '16 at 21:55
  • 1
    @L.Docherty reality sorry, I think I answered you via bashism. – PersianGulf Mar 24 '16 at 0:04

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