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.
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.
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.
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:
So im not sure which loop to use in the function while? or until?
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.
I would use a while with a non-local exit. Like this:
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.
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)
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)
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)
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)
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)
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)
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)
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)