LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2010, 08:58 PM   #1
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Rep: Reputation: 0
How to get invalid user input into a variable


hey guys,

I have a script in which it searches the password file for a username based on the user's input. Is there a way I can take the bad input and keep it in a variable. (one that keeps adding all the non-existent users)

this is what I have so far

echo -n "Enter username: "
read username

grep -i "$username" /etc/passwd || echo "User not found."


so how would I get non-existent users into another variable?

thanks a lot!
Vegas
 
Old 04-08-2010, 09:32 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If I understand what you want
Code:
#straight copy (overwrite)
no_exist=$username

# concatenate
no_exist=$no_exist" "$username
 
Old 04-08-2010, 09:45 PM   #3
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Hey thanks for the response


Im trying to have the user enter a username they want to see the line in the password file that belongs to that user, but also keep the users that dont exist( that the user types in, into another variable) so that at the end of the script i can display all the names that are not accounts

I hope that makes sense
 
Old 04-08-2010, 09:45 PM   #4
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Hey thanks for the response


Im trying to have the user enter a username they want to see the line in the password file that belongs to that user, but also keep the users that dont exist( that the user types in, into another variable) so that at the end of the script i can display all the names that are not accounts

I hope that makes sense
 
Old 04-08-2010, 09:49 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
My 2nd suggestion is what you want if you want to loop asking the user for names until they give up.
 
Old 04-08-2010, 09:58 PM   #6
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
would you be able to add that to what i have already? Im sorry, Im not sure what you mean.


at the end of the script i want to be able to show the variable with all the non-existent usernames.
something like "These are all the names you tried without accounts: bob rob ted fred alex jim"
 
Old 04-08-2010, 10:40 PM   #7
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
This could be improved:


username="1"; while [ ! -z $username ] ;do read -p "Enter username: " username; [ ! -z $username ] && grep -i "$username" /etc/passwd || echo "User not found."; notfound=`echo "$notfound" "$username"`;done;echo "These names not found: $notfound";
 
Old 04-08-2010, 11:21 PM   #8
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
PERFECT


thanks alot!
 
Old 04-09-2010, 12:21 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Don't forget to mark as SOLVED once you have your solution.
 
Old 04-09-2010, 02:43 AM   #10
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Quote:
Originally Posted by TheNewVegas View Post
PERFECT


thanks alot!
He he... You're welcome. Definitely not perfect, but by all means use it to build on.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make my BASH script yes/no user input query terminate with 3 invalid inputs? lupusarcanus Linux - Newbie 9 03-16-2010 03:43 PM
[SOLVED] Very slow system response to user input, variable (and HIGH) CPU load on P4 HT system f4c3l355 Linux - General 3 09-23-2009 08:28 AM
combine bash & expect with variable from user input ndnd Linux - Newbie 2 09-17-2009 09:18 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
squirremail ...it says invalid user or invalid password. rnj Fedora 9 10-25-2004 09:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration