Find the answer to your Linux question:
Results 1 to 5 of 5
hi all. im new to scripting. im trying to add a list of users which are all stored in a data file. so far, i can only get it to ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3

    bash scripting

    hi all. im new to scripting.

    im trying to add a list of users which are all stored in a data file. so far, i can only get it to do this for the first line in the data file.

    this is what i have so far.

    #!/bin/sh
    var=`cat /root/data`
    for i in $var; do
    exec adduser -s /sbin/nologin $i; password $i
    done

    any help would be greatly appreciated.

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    put double quotes and see how it goes
    Code:
    for i in `cat filename`
    do
      exec adduser -s /sbin/nologin "$i"; password "$i"
    done
    otherwise, use a while loop instead
    Code:
    while read -r i
    do
       exec adduser -s /sbin/nologin "$i"; password "$i"
    done < file
    otherwise, it both did not work, show us samples

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    Thanks. For some reason its still only executing it for the first line in the file.

  4. #4
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    exec: exec [-cl] [-a name] file [redirection ...]
    Exec FILE, replacing this shell with the specified program.

    -- excerpt from man bash
    So you are saying, "run adduser, and don't continue with the remainder of the code in this script, exit after the first run of adduser". To fix this part of the script, just remove the exec:
    Code:
    ...
    do
       adduser -s ...
    There are situations when you should use exec, but this is not one of them ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    thanks heaps. that works fine now

Posting Permissions

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