LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Useradd script problem (https://www.linuxquestions.org/questions/linux-newbie-8/useradd-script-problem-715721/)

nomad77 03-31-2009 04:42 AM

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?

nomad77 04-01-2009 02:08 AM

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?

nomad77 04-01-2009 04:33 AM

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?

Tinkster 04-01-2009 04:39 AM

Did you write that script on a windows machine?

maxy7710 04-01-2009 04:51 AM

Try using "/usr/sbin/useradd" instead of useradd in the script.

cos crontab needs full path.

nomad77 04-01-2009 05:32 AM

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?

maxy7710 04-01-2009 05:58 AM

do user=`$line | head -c 6`

use " ` " key on left of numeric 1

u r issue will be solved

maxy7710 04-01-2009 05:59 AM

also use /bin/echo in the script & try

nomad77 04-01-2009 06:00 AM

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.


All times are GMT -5. The time now is 02:32 PM.