Register
It is currently Tue Sep 30, 2014 3:48 am

Need help with user exist/does not exist script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Mar 31, 2010 10:13 am   

Joined: Sat Mar 27, 2010 7:06 pm
Posts: 5
This is the last part, there's more but I have finished that. Just need help with this.

Code:
I think this will work  -     grep "^${USERNAME}:" /etc/passwd    -  but I am having trouble making it work.


I need a loop, but am having trouble getting it to turn over completely.

Here are the instructions:
The text: Please enter a user name to find. The program then takes user input from the command line and checks to see if the user that the person entered exists on the system. If the user does not exist, the script sends an error message that says "try again or type exit to quit". If the user types exit the program will terminate. If the user does exist on the system (all users are located in /etc/passwd), the program will finger the user and returns the information to the screen. Once the successful user was found the program will exit normally. If the user is not found the program will loop until the user either types exit or successfully enters a user name.

thanks for checking it out.


Top
 Profile  
 PostPosted: Wed Mar 31, 2010 11:21 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
Code:
#!/bin/bash

getName() {
   printf '%s\n' cuu1 el | tput -S
   read -p'enter a name: ' name
   seek /etc/passwd $name
}
seek() {
   [ -z "$flag" ] && { exec 3<$1; flag=1; }
   userName="$2"
   read -u 3 line
   [ -n "$line" ] && {
      [ "${line%%:*}" = "$userName" ] && found=1 || seek $1 $userName
   }
   [ ${found:-0} -eq 1 ] && { finger ${line%%:*}; exit; } || { unset flag; getName; }
}

getName
=))


Top
 Profile  
 PostPosted: Wed Mar 31, 2010 5:39 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
Here are some improvements :
Code:
#!/bin/bash

seek() {
   [ -z "$found" ] && {
      [ -z "$flag" ] && { exec 3<$1; flag=1; }
      userName="$2"
      IFS=: read -u 3 name line
      [ -n "$name" ] && {
         [ "$name" = "$userName" ] && {
            found=1
            finger "$userName"
          } || {
            seek $1 "$userName"
          }
      } || {
         printf '%s\n' cuu1 el | tput -S
         read -p"${userName:+$userName does not exist. }enter a name: " name
         unset flag
         seek /etc/passwd $name
      }
   }
}

seek /etc/passwd $1


Last edited by Watael on Wed Apr 07, 2010 12:57 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Wed Apr 07, 2010 8:52 am   

Joined: Sat Mar 27, 2010 7:06 pm
Posts: 5
thanks for the help. I get unexpected end of file on the second script.

have a good day.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP