Help!!!!!!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help!!!!!!!!
# 1  
Old 03-30-2004
Help!!!!!!!!

As many of you on this forum already know, I have asked quite a few questions already that were probably elementary compared to your knowledge of Unix, but I have been searching through countless web pages, and consulting various books on the subject, and I am making progress to an extent.
I'm trying to write a shell script, that has been giving me an extreme headache, and I have it pretty much figured out...except for one certain part...a loop. Let me show you what I have so far so you can get a better idea of what I am trying to accomplish.

echo -n ' Login ID?: '
read loginID
echo -n ' UID?: '

Here's where I need a loop that will check to see if the UID is unique or not by checking in /etc/passwd.
I have a some idea of how to implement grep in a while loop, but I'm not sure of where to put what.
I came up with an if statement that I think will work, but then that wouldn't loop.

read UID
if [ " `grep $UID /etc/passwd ` " = 0 ] ; then
echo -n ' UID already exists - please re-enter a new UID '
UID = $<

That's probably not syntactically correct, but I think I'm in the ballpark somewhere.

The next lines are simple enough for me...

echo -n ' GID?: '
read GID
echo -n 'Comment?: '
read comment
echo -n ' Home Directory?: '
read homeDir
echo -n ' Startup Program?: '
read strtProg

Next I would just add the useradd command, but I couldn't find the proper option for the Login ID, so I am writing it as a ? in the below command:

useradd -? $loginID -u $UID -g $GID -c $comment -d $homeDir -s $strtProg

Now, when the script is finished, I should have an entry in the file passwd.user like:

johnd:x:204:300:John Doe:/home/johnd:/bin/bash


Could someone please help me with this? I am extremely confused with all the examples I've been reading online with the seds and awks, gawks, etc.

Thank you kindly

Meanwhile, I guess I will go throw on another pot of coffee...looks like I won't be sleeping tonight.
# 2  
Old 03-30-2004
You don't need to prompt for UID because useradd will default to the next available unique number. The login ID is mandatory and does not have an option letter - but it does have to be the last argument.

Here's an example of a loop...
Code:
UID=""
until [ -n "$UID" ] &&  grep -q ":$UID:" /etc/passwd
do
  printf "uid? "
  read UID
done

# 3  
Old 03-30-2004
Relykk please put a useful discription as the subject of your threads.

we all assume you are here for help and assistance so there is no need to use that as your subject.
# 4  
Old 03-30-2004
Sorry Optimus! I'll remember next time.

Ygor, I tried implementing the loop you showed to me, and two things are happening:

1) I keep getting prompted whether or not the UID is already existent, and


2) I'm getting an error on the UID = ""

Now, I do have some knowledge of programming. I did take a programming concepts course, but the syntax of this language is driving me crazy. I know that I need something like this:

Enter UID
read UID // Prime read?
while UID already exists OR until UID doesn't already exist
do
Please enter a different UID
read UID
end while

I'm just having one heck of a time with the proper syntax.

Now, I know that grep will search and return a value of 0 if it finds a match. So, I had tried to utilize this in a way of basing the loop on that information, but I'm obviously not piecing it together correctly.

Something like:

until grep $UID -ne 1
do
echo -n "UID in use - please re-enter"
read UID
end

I'm really getting discouraged. I've been playing around with this for the past few days, and my brain is pretty much fried at this point.
# 5  
Old 03-30-2004
also remember you will get an error if loginID exceeds the max length for loginID.
# 6  
Old 03-30-2004
Yes, but that isn't a problem at this point. Thanks though.
# 7  
Old 03-31-2004
Whoops! I had made an error in my previous post. Try this instead....
Code:
UID=""
while [ -z "$UID" ] || grep -q ":$UID:" /etc/passwd
do
  printf "uid? "
  read UID
done

...note that in UID="" there are no spaces either side of the equals sign.

Another way would be to use a flag...
Code:
Flag="Loop"
while [ "$Flag" = "Loop" ]
do
  printf "uid? "
  read UID
  if [ -z "$UID" ]
  then
      echo "UID cannot be null"
  else
      if grep -q ":$UID:" /etc/passwd
      then
         echo "UID already exists"
      else
         echo "OK! UID is $UID"
         Flag="OK"
      fi
  fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question