LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-21-2018, 08:46 AM   #1
LittleMaster
Member
 
Registered: Jun 2012
Posts: 121
Blog Entries: 1

Rep: Reputation: Disabled
shell script check if a user exists Error


Hi All,

I have written script to find the user check in text file and print out the if exact user content matches out in text file. But unfortunately its printing all the user name contents

Is there a way to check if a user already exists in a script and print the exact user matches


#!/bin/bash
Users="/tmp/users"


#user_check
################################################################################
function user_check
{

user=$(cat $test| egrep -v "Password2|Password1 " | egrep "User2|User1" | cut -d "=" -f2|sed 's/ //g')

if grep -Fxq "$user" ${Users}

then

echo "ERROR: Please use a different username. Given username "$user" matches text content t. Exiting" && exit 1

fi

}



cat test

User2 = test1
Password2 = welcome2@
Password1 = welcome2@
User1 = root



vi /tmp/user
test1
test2
test
testing
tester


Output

+ grep -Fxq test1 /tmp/users
++ egrep -v 'Password2|Password1 '
++ cut -d = -f2
++ egrep 'User2|User1'
++ sed 's/ //g'
+ suser='test1
test2'
+ grep -Fxq 'test1
test2' /tmp/users
+ echo 'ERROR: Please use a different username. Given username test1 'test2 matches test content. Exiting'
ERROR: Please use a different username. Given username test1 test2 matches test content. Exiting
+ exit 1
 
Old 05-21-2018, 09:27 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
a snippet of your data file too would help. Along with more than just your function code would help more, but,

I think i have a ruff idea what you're trying to do. you are asking for input from the cli, then using that name to try and find a match to it in a different file?

two loops. outside loop is you control loop, the inner loop takes the name and holds it checking against every thing in the file? sub strings, or splitting the line up into separate words depending on the situation, as you're employing egrep and sed to try and do your dirty work for you.

Code:
#!/bin/bash

read -p "Give me user name, Yo " UName

max=$(cat testusers | wc -l)
echo "max $max"
while  [[ $count -lt $max ]]   ;
do
{
	
	while read L;
	do
	{ 		
	 	if [[ "$L" == "$UName" ]] ; then
		{
			echo "$UName : $L MATCH"
			 
		}
		fi 
		((count++))
		
		
	} done
		
} done <testusers
test file was just something simple.
Code:
Bob
bob
boB
BoB
BOB
bOb
using two separate files
Code:
#!/bin/bash

while read F ;
do
{
	while read L;
	do
	{  
		if [[ "$L" == "$F" ]] ; then		 
			echo "$L : $F MATCH"		 
		fi 
		((count++))
	} done <testfile
		 
} done <testusers
two files
Code:
# file 1
bob  
sally 90
Jill::70
carera 22


#file 2
Bob 34
bob 99
sally 99
Jill::70
boB
sally 88
BoB
sally 90
carera
BOB
sally 12
bOb
results
Code:
$ ./findUser
Jill::70 : Jill::70 MATCH
sally 90 : sally 90 MATCH

Last edited by BW-userx; 05-21-2018 at 10:16 AM.
 
Old 05-22-2018, 12:33 AM   #3
LittleMaster
Member
 
Registered: Jun 2012
Posts: 121

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
#user_check
################################################################################
function user_check
{

user=$(cat $test| egrep -v "Password2|Password1 " | egrep "User2|User1" | cut -d "=" -f2|sed 's/ //g')

if grep -Fxq "$user" ${Users} ---- Tried out to find the match word to print below but it print both users

then

echo "ERROR: Please use a different username. Given username "$user" matches text content t. Exiting" && exit 1

fi

}
 
Old 05-22-2018, 01:45 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
I'd try the way that BW-userx is suggesting. However, please show a line or two (or a record or two if they consist of multipe lines) of sample data so that we can see what you are really having to face.

Just working blindly without the data, I can say that the following line can be replaced by a sinlge call to awk so that you don't need to slow things down with so many processes:

Code:
user=$(cat $test| egrep -v "Password2|Password1 " | egrep "User2|User1" | cut -d "=" -f2|sed 's/ //g')
But again please show one or two records so we can see what the problem is.
 
Old 05-22-2018, 01:58 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be nice to use [code][/code] tags around your code and files, otherwise it is a bit hard to read.
the line user=$(cat ... ) will return not only a single user name, but all of them in one. Otherwise yes, that line can be easily simplified (for example egrep -v is not required at all, cat is useless (http://www.novosial.org/shell/useless-cat/)
 
  


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
[SOLVED] Shell script which will check the target file and folder exists and copy it agent_mach Linux - Newbie 5 11-18-2014 01:33 AM
Shell script which will check the target file and folder exists and copy it agent_mach Programming 2 11-15-2014 05:36 AM
Shell script to check whether directory exists on remote server sudhirav Programming 10 01-18-2011 05:39 PM
check if directory exists using shell script v333k Programming 9 04-23-2009 09:29 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM

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

All times are GMT -5. The time now is 06:12 AM.

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