Re: BASH + Best method of converting fields to variables

From: Aaron Gaudio (prothonotar_at_tarnation.dyndns.org)
Date: 07/17/04

  • Next message: William Hooper: "Re: Core 3 Release 1 - SELinux is activated by default"
    Date: Fri, 16 Jul 2004 22:29:38 -0400
    To: For users of Fedora Core releases <fedora-list@redhat.com>
    
    
    
    

    Behold, Ow Mun Heng <Ow.Mun.Heng@wdc.com> hath decreed:
    > 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
    >
    > I ended up using awk, which works but seems complicated
    >
    > eval `awk -F ':' \
    > '{printf ("useradd -c \"%s\" ",$2)}; \
    > length($3)>0 {printf ("-g %s ",$3)}; \
    > length($4)>0 {printf ("-s %s ",$4)}; \
    > length($5)>0 {printf ("-d %s ",$5)}; \
    > {printf ("%s\n;",$1)};' $1`
    >
    > Can it be done using bash itself? (without calling awk)
    > Actually I was hoping I can get awk to get the fields for the variables

    You could try this:

    read -d: USERNAME
    read -d: NAME
    read -d: GROUP

    etc..

    Keep in mind that when using -d, read will not treat a newline
    as a delimiter, so you have to use more trickery if you want
    optional fields.
    > > psuedo code
    > username = awk -F: {'print $1}'
    > name = awk -F: {'print $2}'
    >
    > Any takers??
    >
    > BTW, what's the best way to check the number of fields in the input file?
    > I was thinkning of counting how many ":" there are in each line. But can't
    > figure out how to actually _do_ it.
    >
    >
    >
    > --
    > Ow Mun Heng
    > Fedora GNU/Linux Core 2 on D600 1.4Ghz CPU kernel
    > 2.6.7-2.jul1-interactive
    > Neuromancer 17:13:44 up 5:56, 10 users, load average: 1.65, 1.32, 0.97
    >
    >
    > --
    > fedora-list mailing list
    > fedora-list@redhat.com
    > To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list

    -- 
    prothonotar at tarnation.dyndns.org
    "Every man is a mob, a chain gang of idiots." 
                               - Jonathan Nolan, /Memento Mori/
      
    
    

    
    

    -- 
    fedora-list mailing list
    fedora-list@redhat.com
    To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list
    

    • application/pgp-signature attachment: stored

  • Next message: William Hooper: "Re: Core 3 Release 1 - SELinux is activated by default"

    Relevant Pages

    • Re: Request for Comments -- shell features and documentation
      ... > As you know, I've been adding to Bash many useful features from Awk, ... > Python, Zsh, and Ksh, and other things that you probably never heard of. ... > If command substitution string begins with '..', ...
      (comp.unix.shell)
    • Re: BASH + Best method of converting fields to variables
      ... =>I'm having some trouble getting bash to get the positional fields into variables which I ... (without calling awk) ... Time flies like the wind. ... Stranger things have .0. ...
      (Fedora)
    • Re: unix command
      ... Just where did I say I was new at awk? ... bash and the performance of the bash script is good enough that I just did ... familiarity with it is a result of less use, ... that the bash script was "EASIER" for me. ...
      (comp.unix.shell)
    • Re: log parsing
      ... > Bourne shell is quite generally applicable. ... > or do you use any non-POSIX features of bash? ... Awk is still useful for one or two-liners, ...
      (comp.lang.awk)