LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-31-2009, 05:42 AM   #1
nomad77
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Rep: Reputation: 0
Useradd script problem


Hello,

I have a problem with my useradd-script.

The script:
Code:
#!/bin/bash
# Set the field seperator to a newline
# Loop through the file
for line in `cat /www/fotoalbum/users.lst`;do
  useradd "$line"
done
The users.lst-file
Code:
user01
user02
If I run the script these lines are created in the /etc/passwd:
Code:
:/bin/sh1036::/home/user01
:/bin/sh1037::/home/user02
When I use useradd @ the prompt, these (correct) lines are created:
Code:
user01:x:1047:1047::/home/user01:/bin/sh
user02:x:1048:1048::/home/user02:/bin/sh

Why is the result of the useradd in the script different?
 
Old 04-01-2009, 03:08 AM   #2
nomad77
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
UPDATE:

lines in etc/passwd added by the script viewed with the command 'more etc/passwd'
Code:
:/bin/sh1036::/home/user01
:/bin/sh1037::/home/user02
lines in etc/passwd added by the script viewed with vi
Code:
user01^M:x:1031:1031::/home/user01^M:/bin/sh
user02^M:x:1032:1032::/home/user02^M:/bin/sh
I think the problem is caused by blank spaces in the line.
Does anyone have a solution for this?

Last edited by nomad77; 04-01-2009 at 03:10 AM.
 
Old 04-01-2009, 05:33 AM   #3
nomad77
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
UPDATE:

I changed the script-code:
Code:
#!/bin/bash
# Set the field seperator to a newline
# Loop through the file
for line in `cat /www/fotoalbum/users.lst`;do
   echo $line | head -c 6
   useradd "$user"
done
The echo gives the first 6 chars of the line:
user01
user02

I gues without blank spaces.

Now I need to get this value into the variable user, how do I do this?
 
Old 04-01-2009, 05:39 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Did you write that script on a windows machine?
 
Old 04-01-2009, 05:51 AM   #5
maxy7710
Member
 
Registered: Jan 2008
Location: Mumbai, india
Distribution: REDHAT, FEDORA,SUSE, UBUNTU, ORACLE ENTERPRISE LINUX & SOLARIS 10
Posts: 130

Rep: Reputation: 17
Try using "/usr/sbin/useradd" instead of useradd in the script.

cos crontab needs full path.
 
Old 04-01-2009, 06:32 AM   #6
nomad77
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I wrote the script in vi on a debian server (putty-session).

Thanks maxy7710, I changed the line.

Code:
#!/bin/bash
# Loop through the file
for line in `cat /www/fotoalbum/users.lst`;do
   user=$line | head -c 6
   echo $user
   /usr/sbin/useradd "$user"
done
The $line | head -c 6 gives the correct output.
But when I want to fill the variable user it's empty.
The echo $user shows two empty lines.

How do I need to assign the variable?
 
Old 04-01-2009, 06:58 AM   #7
maxy7710
Member
 
Registered: Jan 2008
Location: Mumbai, india
Distribution: REDHAT, FEDORA,SUSE, UBUNTU, ORACLE ENTERPRISE LINUX & SOLARIS 10
Posts: 130

Rep: Reputation: 17
do user=`$line | head -c 6`

use " ` " key on left of numeric 1

u r issue will be solved
 
Old 04-01-2009, 06:59 AM   #8
maxy7710
Member
 
Registered: Jan 2008
Location: Mumbai, india
Distribution: REDHAT, FEDORA,SUSE, UBUNTU, ORACLE ENTERPRISE LINUX & SOLARIS 10
Posts: 130

Rep: Reputation: 17
also use /bin/echo in the script & try
 
Old 04-01-2009, 07:00 AM   #9
nomad77
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Problem solved!

Code:
Code:
#!/bin/bash
# Loop through the file
for line in `cat /www/fotoalbum/users.lst`;do
   first_part=${line%?}
   echo $first_part
   /usr/sbin/useradd $first_part
done

With this code the user-line in the etc/passwd is correct.
 
  


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
useradd in a script john83reuben Linux - Newbie 8 05-28-2008 07:48 PM
Script for useradd mossy464 Programming 3 04-16-2008 03:32 PM
useradd script headache croc Programming 7 11-07-2006 05:15 AM
useradd script newuser455 Programming 4 01-28-2005 12:48 AM
Useradd Script jcornel7 Linux - Newbie 2 02-17-2003 10:48 AM


All times are GMT -5. The time now is 04:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration