The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Configuration Mania aids access to some Firefox settings iBot UNIX and Linux RSS News 0 04-28-2008 01:10 AM
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 09:49 AM
for loop or other ? wisher115 UNIX for Dummies Questions & Answers 1 11-11-2006 11:04 AM
While loop whatisthis Shell Programming and Scripting 6 10-29-2004 06:30 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 08:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-28-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Thumbs up Loop Mania

hello all, here is my first post since i am in need of figuring out what my exact problem is. Here is what i'm trying to do:

run a script to check and see if a user exists on the system and if not sends an error message and 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 it 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.

Code:
#!/bin/bash
			error_fn()
{
echo user entered $username
echo try again or type exit to quit
read answer
case $answer in 
exit|Exit|EXIT) exit 20
;;
*)
echo please enter a user to find
read $usertry
while grep $usertry /etc/passwd
                do
		 finger $usertry 
		 exit 1
done
esac
}

echo "HELLO AND WELCOME TO YOUR USER SEARCHING UTILITY"
echo please enter the name of the user you wish to find
read username
echo "you entered $username"
	if grep $username /etc/passwd
		then finger $username 	
			else error_fn
fi

Script:

1st part) it will successfully grep users that are in /etc/passwd and finger them.

2nd part) However, if i type in a user that is not there and get taken to the error function that starts, it will ask me to enter exit or try another username entry. But when a user that exists is entered it will repeat the line and not finger and exit.

3rd part) last but not least when i continually enter in a username that doesnt exist it doesnt loop.
Reply With Quote
Forum Sponsor
  #2  
Old 03-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
If I were you I would take away the interactive features, but then you would end up with basically just grep anyway.

You really really must quote user-supplied input variables. Somebody could type `rm -rf $HOME` with the backticks at your prompt and then blame you for the consequences.

You are planning to not ever have a user named "exit" then? Perhaps empty input should be used to quit, instead.

You have the grep function twice. I would refactor so the grepping is in a function and the flow control is the main body of the script.

The actual problem is that you have a dollar sign in front of the variable in the read inside the function.

Hope this helps.
Reply With Quote
  #3  
Old 03-29-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
This will give you the desired output:

Code:
#!/bin/bash

error_fn () {
echo "   enter a valid username, or type exit to quit"
read name
        if grep "$name" /etc/passwd
           then finger "$name"
                   exit 0
            else
            if [ "$name" = exit -o "$name" = Exit -o "$name" = EXIT ]
               then
                 exit 20
             fi
         fi
}

echo " enter user or type exit to quit"
 read answer

    case "$answer" in

          exit|Exit|EXIT) exit 20
               ;;

           *)  while  true
                do
                      if grep "$answer" /etc/passwd
                       then
                          finger "$answer"
                          break
                       else
                            error_fn
                             continue
                      fi
               done
               ;;
    esac

Last edited by rubin; 03-30-2008 at 07:23 PM. Reason: added quoting & replaced exit 0 with break inside the loop ( thanks to era for suggesting )
Reply With Quote
  #4  
Old 03-29-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
I have successfully run the first portion of this script and it has taken me to the error function but here is where i get stuck. The case statement works correctly and will exit if the user has entered any of the exit words. If they enter a user name to search for that exists the script should display the user searched for, finger them, and exit to command prompt. If they enter a user that doesn't exist, they should be caught at the TRY AGAIN loop until they enter a proper username or type exit.

Anyhow, here is the updated code that i worked on this morning:

Code:
#!/bin/bash
			error_fn()
{
echo User Not Found please try again type exit or Q-q to quit
read answer
case $answer in 
q|Q|exit|Exit|EXIT) exit 20
;;
*)
while [ $? -gt 0 ] ; 
do
echo "User not found please try again:"
done
esac
}


echo please enter the name of the user you wish to find:
read username
echo "you entered $username"
	if cat /etc/passwd| grep $username
		then finger $username 	
			else error_fn
fi

So im not sure which loop to use in the function while? or until?
Reply With Quote
  #5  
Old 03-29-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
Hi,

Did you try the code that I just posted ?


There are a few logical issues with your code, especially in the loop part :

Code:
*)                                        
while [ $? -gt 0 ]          # here $? is 0 and not greater than 0
Reply With Quote
  #6  
Old 03-29-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Solved Thank You!!!

WOOOT Thank you soo much problem solved and the loop is in place.
I need to get this concept of thinking simple because i tend to easily get lost in syntax and trying to figure out which loop to use. Again, thanks for the help and i'll be posting some more puzzlers soon.

Reply With Quote
  #7  
Old 03-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
I would use a while with a non-local exit. Like this:

Code:
while true 
do
  stuff
  case $stuff matches)  do your things; break ;; esac
done
Incidentally, as others have already pointed out between the lines, cat file | grep expr is a Useless Use of Cat; the Useful way to do that is simply grep expr file

I'll say it again: where you have $variable, you should have "$variable", throughout. There are good FAQ sheets about shell quoting on the net -- find them and read them if you ever write anything which involves user input.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0