Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 19/20 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 25th October 2011, 10:34 PM
marinestu Offline
Registered User
 
Join Date: Sep 2008
Location: UK Northampton
Age: 39
Posts: 56
linuxfirefox
script to add user to system and test if user exists

Hello Team

I have a scripting question for you all...would one of you good sorts please tell me why this script does not work. I am testing the variable $test to make sure that it contains a value. If it does the user is on the system and the script ends here. If however there is no value to the variable $test then the user is added to the system.

The thing is it seems the system thinks there is always a variable to $test and thus does not add the user.

I am sure this is something simple....who wants some easy credit here ;-)

Take care guys and thanks



#!/bin/bash
# Adding and testing to see if the someuser users exists on the system
tuser=someuser
test=`grep $tuser /etc/passwd`
#echo $test

if [ -n $test ]
then
echo "User $tuser is on the system contact root"

else echo adding $tuser to system; useradd $tuser; /bin/echo password | /usr/bin/passwd bmc --stdin
fi
Reply With Quote
  #2  
Old 26th October 2011, 12:06 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 31
Posts: 2,750
linuxfirefox
Re: script to add user to system and test if user exists

Use:
Code:
[ -n "$test" ]
or
Code:
[[ -n $test ]]
or
Code:
[[ -n "$test" ]]
The quotes cause an empty $test to expand to a null string (""), which behaves as it should:
Code:
[ -n "" ]
Without that, the empty $test expands to nothing at all, and the test becomes:
Code:
[ -n ]
i.e. it tests if "]" is a non-null string! "[" has an odd syntax. Although it is built into bash, traditionally it's an external command (see "/usr/bin/["), so the "]" is an optional part of its command line...

"[[ ... ]]" is guaranteed to be a shell built-in, and because of that is aware of $test being a shell variable (rather than only getting the expanded version on its command line), so the quotes are optional.

The "[ ... ]" syntax is for compatibility with traditional Bourne shell, POSIX prefers "[[ ... ]]" for new code.

Gareth

---------- Post added at 12:06 AM ---------- Previous post was at 12:01 AM ----------

Also note that your script will catch any match in /etc/passwd, not just user names. Try adding a user "bash" with your script for example.
Reply With Quote
  #3  
Old 26th October 2011, 04:18 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,599
linuxfedorafirefox
Re: script to add user to system and test if user exists

As Gareth mentioned, you should be careful to match only the username in the first colon-delimited field of /etc/passwd, which you could do like this:
Code:
#!/bin/bash
tuser=someuser
test="$(grep ^${tuser}: /etc/passwd)"
if [ -n "${test}" ]; then
   echo "User ${tuser} is on the system, contact root"
else
   echo "Adding ${tuser} to system"
   # Do some more stuff
fi
Reply With Quote
  #4  
Old 26th October 2011, 09:48 AM
marinestu Offline
Registered User
 
Join Date: Sep 2008
Location: UK Northampton
Age: 39
Posts: 56
windows_7firefox
Re: script to add user to system and test if user exists

Hello Guys

Thanks for taking the time to not only show me the error of my ways but also come up with a better solution to my question.

Looking forward to getting my hands on F16

Take it easy now

Cheers
Reply With Quote
Reply

Tags
add, exists, script, test, user

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for this signature-logos site (linux user, nvidia user, firefox user, ...) smurffit Wibble 3 12th March 2010 07:12 AM
Script for a AD domain user ic3000 Using Fedora 0 8th July 2008 10:07 AM
PHP script with sudo user PaLoBo Servers & Networking 0 18th January 2007 06:37 PM
Run script as user Artemis Using Fedora 8 8th October 2004 01:36 PM


Current GMT-time: 14:42 (Thursday, 24-07-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat