Loop Mania


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop Mania
# 1  
Old 03-29-2008
MySQL 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.
# 2  
Old 03-29-2008
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.
# 3  
Old 03-29-2008
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 11:23 PM.. Reason: added quoting & replaced exit 0 with break inside the loop ( thanks to era for suggesting )
# 4  
Old 03-29-2008
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?
# 5  
Old 03-29-2008
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

# 6  
Old 03-29-2008
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.

Smilie
# 7  
Old 03-29-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

How to loop use while loop in csh script?

Hi all, i got 2 text file. file.txt value.txt i want use C shell script to write out while both of the file got different limit....how i going to write it in 1 while loop? (4 Replies)
Discussion started by: proghack
4 Replies

6. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

7. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

how to get the similar function in while loop or for loop

Dear all How to write the shell script for the following statement: (C programming) for (i=0;i<30;i++) { if i=1 continue *skip this number (To do function here....) ... } similar statement in while loop.... I wrote the script in sh... (3 Replies)
Discussion started by: trynew
3 Replies
Login or Register to Ask a Question