Fedora Linux Support Community & Resources Center
Old 25th October 2011, 10:34 PM
marinestu Offline
Registered User
Join Date: Sep 2008
Location: UK Northampton
Age: 40
Posts: 56
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

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

if [ -n $test ]
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
Reply With Quote
Old 26th October 2011, 12:06 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
Re: script to add user to system and test if user exists

[ -n "$test" ]
[[ -n $test ]]
[[ -n "$test" ]]
The quotes cause an empty $test to expand to a null string (""), which behaves as it should:
[ -n "" ]
Without that, the empty $test expands to nothing at all, and the test becomes:
[ -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.


---------- 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
Old 26th October 2011, 04:18 AM
RupertPupkin Offline
Registered User
Join Date: Nov 2006
Location: Detroit
Posts: 5,768
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:
test="$(grep ^${tuser}: /etc/passwd)"
if [ -n "${test}" ]; then
   echo "User ${tuser} is on the system, contact root"
   echo "Adding ${tuser} to system"
   # Do some more stuff
Reply With Quote
Old 26th October 2011, 09:48 AM
marinestu Offline
Registered User
Join Date: Sep 2008
Location: UK Northampton
Age: 40
Posts: 56
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

Reply With Quote

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 08: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 07:37 PM
Run script as user Artemis Using Fedora 8 8th October 2004 01:36 PM

Current GMT-time: 17:19 (Saturday, 20-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

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
...Kansai Int'l airport T1(Terminal 1), Osaka, Japan - Bar Robadors 23 Instagram Photos - Pandawa Cliff Estate