Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
goodmorning (here anyways), I've been busy with scripting with linux. A simple useradd script and a userdell. Script: #!/bin/sh for i in `more userlist.txt ` do echo $i useradd $i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9

    #!/bin/sh


    goodmorning (here anyways),

    I've been busy with scripting with linux. A simple useradd script and a userdell.

    Script:
    #!/bin/sh
    for i in `more userlist.txt `
    do
    echo $i
    useradd $i
    echo $i"123?" | passwd --stdin "$i"
    echo; echo "User $username password changed"
    done


    `more userlist.txt ` means there is a userlist.txt file where the username should be. Allso this scripts makes a standard password that is 123[username].

    When i typ ./user.txt in the shell, i get this output:
    Script:
    more
    Changing password for more.

    User password changed
    userlist.txt
    useradd: Account 'userlist.txt' already exists.
    Changing password for userlist.txt

    User password changed

    same happens when i do userdell with a simalair script

    what goed wrong? I made this script a long tima ago and then it works great. Did i mist something?

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    hm. more is a interactive program that you shouldn't use in a script. use cat instead.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9
    'cat userlist.txt' ?

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Kind of:

    Code:
    #!/bin/sh
    for i in `cat userlist.txt `
    do
    echo $i
    useradd $i
    echo $i"123?" | passwd --stdin "$i"
    echo; echo "User $username password changed"
    done
    Note that ' and ` quotes have a different meaning.

  5. #5
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9
    well, that worked.. but now i get the following message:

    changing login shell for basdijkstra
    enter the new value, or press return for de default.
    chsh: Shell must be a full path name.
    chsh: 'basdijkstra 123' does not exist.
    chsh: 'basdijkstra 123' is not executable
    Login Shell [/bin/bash] : Warning: "basdijkstra 123" is not listed in /etc/shells.
    Shell Changed

    User password changed

  6. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Code:
    #!/bin/sh
    for i in `cat userlist.txt `
    do
    # remove whitepaces
    i="${i/ /}123"
    echo $i
    
    # add user
    useradd -p password $i
    
    # change password not with passwd, but usermod (see man usermod)
    echo "User $username password changed"
    done
    Please refer to
    Code:
    man passwd
    man usermod
    man useradd
    man adduser
    man id
    for proper usage. For example the --stdin flag is unknown to me. Oh, and NEVER call useradd on a login that already exists. Do proper exists checks before.
    Last edited by Kloschüssel; 09-03-2010 at 09:51 AM.

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Kloschüssel View Post
    Code:
    #!/bin/sh
    for i in `cat userlist.txt `
    do
    # remove whitepaces
    i="${i/ /}123"
    echo $i
    
    # add user
    useradd -p password $i
    
    # change password not with passwd, but usermod (see man usermod)
    echo "User $username password changed"
    done
    Please refer to
    Code:
    man passwd
    man usermod
    man useradd
    man adduser
    man id
    for proper usage. For example the --stdin flag is unknown to me. Oh, and NEVER call useradd on a login that already exists. Do proper exists checks before.
    Better is to avoid the use of cat in a loop, don't go for the Useless Use of Cat Award:

    Code:
    #!/bin/sh
    
    while read i
    do
      echo "$i"
      ...
      ...
    done < userlist.txt

  8. #8
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    In the end everybody gets something new to learn from time to time.

  9. #9
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9
    well. I have the following script. Somewhere there is something wrong:

    Code:
    #!/bin/sh
    for i in `cat userlist.txt `
    do
    i="${i/ /}123"
    echo $i
    useradd -p password $i
    echo; echo "Username $username password changed"
    done
    Output from terminal:
    Code:
    Linuxuser123
    
    Username password changed
    So it's making the user, but not with the password 123 but makes a user with 123 afther.

    No errors, but i can't login.

    ??

  10. #10
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9
    Allso, it's not making a homedir. I thought useradd -m (username) was for making a homedir?

    if i do that, the error is that there is to many arguments

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •