• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

add user script

#!/bin/bash
cat  $1 | while read  un  gn  pw fn
      do
            cat  /etc/group  |  egrep $gn
                  if  [ $? == 0 ]; then
                        echo “Group $gn  already exists”
                  else
                        groupadd  $gn
                  fi
            pass=$(perl –e ‘print crypt($ARGV[0], “password”)’ $pw)
            cat /etc/passwd | egrep $un
                  if  [  $? == 0 ]; then
                        echo “user $un already exists”
                  else
                        useradd –p $pass –g $gn  $un
                        if  [ $? == 0 ]; then
                              echo “user added successfully added”
                        fi
                  fi
      done
In the code given above is the test.sh.
first I made a user.txt which contains username group name password and when I excute
./test.sh user.txt it gives me errors at line 10 unexpected EDF while looking for matching ')' and at line 21 syntex error.

What seems to be the problem
thank you for your help.
0
bt1942
Asked:
bt1942
  • 2
  • 2
2 Solutions
 
woolmilkporcCommented:
Could it be that a password in your input file contains an opening parenthesis?

If so, try "$pw" in the perl command.
0
 
ozoCommented:
perl –e ‘print crypt($ARGV[0], “password”)’ $pw
should be
perl -e 'print crypt($ARGV[0], "password")' $pw
0
 
woolmilkporcCommented:
Wow, hard to see! Compliment to your excellent eyesight, ozo!

The same is true then for this line:

useradd –p $pass –g $gn  $un
should be
useradd -p $pass -g $gn  $un
0
 
ozoCommented:
likewise, you probably meant
 echo "Group $gn  already exists"
when you said
 echo “Group $gn  already exists”
0
 
bt1942Author Commented:
I don't know why it shows –p but I wrote -p. I think there's something wrong with expert exchange website.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now