LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-12-2005, 02:17 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
add user bash script


I wrote a bash to add user to the system which takes user name as a command line argument.

the only problem is i want it to check /etc/password to see if the user name already exists and if not, add user. my script is not working...

The problem is apparently with the check = "`cat /etc/passwd | grep $username`"

username="echo $@"
check = "`cat /etc/passwd | grep $username`"
if [ -n "$check" ]; then
echo "The name you have chosen already exist"
fi
 
Old 08-12-2005, 04:54 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
The problem with grep an user name in /etc/passwd is that the user name can be
similar to another string.

I would use id command to check the user existence, like :
Code:
if id $username > /dev/null 2>&1; then
    echo "The name you have chosen already exist"
    exit 1
fi
 
Old 08-12-2005, 07:27 AM   #3
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Thanks a bunch. that solved the problem.
 
Old 08-12-2005, 07:34 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Good
Also, why not just use :
Code:
username=$1
instead of :
Code:
username="echo $@"
 
Old 08-13-2005, 09:24 AM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by keefaz
The problem with grep an user name in /etc/passwd is that the user name can be
similar to another string.
So match the pattern properly:
Code:
grep "^$username:" /etc/passwd
And please don't cat a file into a command that accepts a filename argument! You can also redirect to its stdin:
Code:
grep "^$username:" </etc/passwd
 
  


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
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 06:48 PM
add user script satinet Linux - General 2 10-21-2005 03:48 AM
Need help finishing Bash Script to add new user and map virtual e-mail info. O_Chaos Linux - Newbie 1 02-09-2004 05:06 PM
add user script? ezra143 Linux - Software 2 10-22-2003 12:21 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 10:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 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