LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-21-2004, 08:58 PM   #1
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Rep: Reputation: 15
simple if condition


Hey this is a simple task i want to do with my script. its going to check the /etc/passwd file and see if you can find a particular user. The script will handle 1 paramater.

ok so its like this: ./checkuser userid

userid can be what ever userid i want to check for

heres my script
----------------------------------------------------------
cat /etc/passwd | grep $1

if ($? == 0); then

echo User found!

else
echo User not found!
fi
------------------------------------------------------------

so i run:

[ninja@P1 scripts]$ ./checkuser ninja
ninja:x:500:500:dark:/home/ninja:/bin/bash
./backemup: line 3: 0: command not found
User not found!

$? returns 0 if its completed right? and 1 if its not found a user name?
can anyone help me clear this up.

and one more thing. you can use () instead of [] right?
 
Old 05-21-2004, 09:12 PM   #2
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
Quote:
$? returns 0 if its completed right? and 1 if its not found a user name?
Yes

Quote:
and one more thing. you can use () instead of [] right?
Not in my version of bash.

You use '-eq' for integer equality comparison.

e.g
if [ $1 -eq 0 ]
then
echo $1 equals zero.
fi
 
Old 05-21-2004, 09:28 PM   #3
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Original Poster
Rep: Reputation: 15
cat /etc/passwd | grep $1

if [$1 -eq 0];
then
echo $1 equals zero.
fi
-----------------------------------------------------------------
[ninja@P1 scripts]$ ./checkuser ninja
ninja:x:500:500:dark:/home/ninja:/bin/bash
./checkuser: line 3: [ninja: command not found

i dont know whats wrong with line 3.. its not displaying the message either. could you posible get your script to work and then show me? thanks.
 
Old 05-21-2004, 09:39 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
You need spaces around the [ and ].

[ is actually parsed as a regular command. There exists a /bin/[ , (or /usr/bin/[) although your shell may use a builtin.
 
Old 05-21-2004, 09:44 PM   #5
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Original Poster
Rep: Reputation: 15
cat /etc/passwd | grep $1

if [ $1 -eq 0 ] ;
then
echo $1 equals zero.
fi
----------------------------------------
i added the spaces like that but still same error. it dipalys the name but wont echo the message. Can anyone make a working version and show me please. its still giving the same error.
 
Old 05-21-2004, 09:55 PM   #6
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 388

Rep: Reputation: 52
Code:
#!/bin/bash

grep "$1" /etc/passwd &> /dev/null

if [ $? -eq 0 ]
then
	echo User found!
else
	echo User not found!
fi
Or, a little more succinctly
Code:
#!/bin/bash

if grep "$1" /etc/passwd &> /dev/null
then
	echo User found!
else
	echo User not found!
fi
Lyle
 
Old 05-21-2004, 10:03 PM   #7
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Original Poster
Rep: Reputation: 15
thanks it works fine. your script dosent diplay the users home dir. like the other script i did. is it going to be an issue later on because i have to check the users home dir for backing up of files later. thanks for your code.
 
Old 05-21-2004, 10:16 PM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
I must be a bit confused because your second program is completely different from the first. You also call the argument userid and then use a user name for it. Here is a version which takes a name for an argument and returns whether the associated uid is 0.
Code:
#!/bin/sh
foo=`grep "^$1:" /etc/passwd | cut -f 3 -d :`
if [ x"$foo" = x ]; then
        echo User $1 not found
        exit
fi
if [ `grep "^$1:" /etc/passwd | cut -f 3 -d :` -eq 0 ]; then
        echo $1 has uid equal to zero.
fi
I'm not really sure if that's what you want.

I'm also not sure what you mean by the "same error" because my last version is not going to give
Quote:
[ninja: command not found
, since [ is not next to $1

edit: I got distracted while writing that and there were 2 replies posted between when I started and finished

Last edited by aluser; 05-21-2004 at 10:17 PM.
 
Old 05-21-2004, 10:26 PM   #9
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 388

Rep: Reputation: 52
Oops, I didn't realize you wanted the user's home directory, so I redirected all output from grep to the null device.

I also realized there's a pretty bad bug in our script--it'll say the user is found if it finds that username anywhere in /etc/passwd. For example, run it passing the username as ':' (including the single quotes). I changed the regular expression so it only matches the username in the first field.

Code:
#!/bin/bash

if grep "^$1:" /etc/passwd
then
        echo User found!
else
        echo User not found!
fi
Or if you want just the user's home directory without all the other /etc/passwd fields:

Code:
#!/bin/bash

if OUTPUT=$(grep "^$1:" /etc/passwd)
then
	echo "$OUTPUT" | cut -d':' -f6
	echo User found!
else
	echo User not found!
fi
You're welcome!

Lyle
 
Old 05-21-2004, 10:31 PM   #10
sonesay
Member
 
Registered: May 2004
Location: AUCKLAND
Distribution: Fedora core 2 , RH9
Posts: 64

Original Poster
Rep: Reputation: 15
hey thanks thats alot of help there. I will try your last one later. I have been at this cmputer for hours. I need to take a break now. do you guys use this forum alot? I find it a good place for info so far. I am starting to understand linux more now thanks to this forum. I've solved or understood so many things about linux from the searches you can do here.
 
Old 05-21-2004, 10:34 PM   #11
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 388

Rep: Reputation: 52
Quote:
Originally posted by aluser
edit: I got distracted while writing that and there were 2 replies posted between when I started and finished
Sorry aluser! I think I kind of hijacked this thread without realizing it!

Lyle
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
wine - Condition Zero - installation problem anavrin Linux - Games 1 09-19-2005 01:06 PM
cedega and CS: condition zero bojann Linux - Games 8 06-27-2005 01:49 PM
What condition I don't need initrd? Chowroc Linux - General 3 05-22-2005 02:59 AM
If condition in script imsajjadali Red Hat 5 06-07-2004 06:52 AM
CounterSrike: Condition Zero Comarez Linux - Games 2 04-20-2004 02:08 PM


All times are GMT -5. The time now is 01:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration