BASH + Best method of converting fields to variables

John Kodis kodis at mail630.gsfc.nasa.gov
Mon Jul 19 15:09:52 UTC 2004


Ow Mun Heng wrote:

>Guys,
>
>    Writing a batch_create_new_user script.
>input is a file with this format
>
>Username:Full Name:Default Group:Default Shell:Home Directory:Password
> 
>and the using those fields ($1 $2 $3 etc) and passing then to the 
>useradd program
>
>I'm having some trouble getting bash to get the positional fields 
>into variables which I
>can then plug into useradd
>
>eg:
>pseudo code
>for each line in input file
>  do
>    username=$1
>    name=$2
>    useradd -c "$2" $1
>  done
>
>Can it be done using bash itself? (without calling awk)

Try setting the input field separator, $IFS, something like this:

    $ IFS=:; while read a b c; do echo Got $b; done
    a:b:c
    Got b
    foo:bar:moo
    Got bar

-- 
John Kodis                                    Goddard Space Flight Center
kodis at mail630.gsfc.nasa.gov                      Greenbelt, Maryland, USA
Phone: 301-286-7376                                     Fax: 301-286-1771





More information about the fedora-list mailing list