Shell script

Charles Steinkuehler charles at steinkuehler.net
Wed Nov 12 19:49:15 CST 2003


Fixes & comments inline...

brad wrote:
> Hmmm....same thing.  I was trying to send useradd the flags -g, -d, and
> -s, so I trimmed it down to just plain old /usr/sbin/useradd $1 and I
> get the same thing.  Since I can run my whole useradd line manually (of
> course replacing the variable with a real username) and it runs fine, I
> don't think my usage is off.  I still think it is having trouble picking
> up the variable.  Again, just echoing $1 provides nothing.  I will keep
> digging.

The problem is you're missing a set command

 > So I changed the make_ftp script to this:
 >
 > #!/bin/bash
 >
 > while read ALINE
 > do
 > IFS=|
 > echo $1
 > echo $2
 > done
 >
 > and I get no output at all...just 3-4 blank lines.  It seems that it
 > is not picking up the variables correctly.

Due to the missing set command

 > So that I can understand this, what is the IFS| ??

 From "man bash":
IFS    The Internal Field Separator that is  used  for  word  splitting
        after  expansion  and  to  split  lines into words with the read
        builtin  command.   The  default  value  is  "<space><tab><new-
        line>".

When using "set -- " to split arguments, any character contained in $IFS 
is considered "whitespace" while anything *NOT* in IFS is considered 
actual data.  By setting IFS to something other than the normal 
<space><tab><newline>, you can do fun parsing tricks with set (a 
built-in shell command that typically runs really fast compared to 
parsing things with awk, sed, or what have you).

Note that since your initial file contained tab seperated entries, and 
tab is part of the default IFS, as long as you don't have any spaces or 
newlines in your usernames or passwords, you could parse the file using 
read or set without having to change IFS (as mentioned in my first e-mail).

> Scott wrote:
>> Let me give the script to you again....  I think it's just a typo...
>> 
>> #!/bin/sh
>> while read ALINE
>> do
>> IFS=|
>> set ALINE

This version includes the set, but it's wrong.  The above line should read:

set -- $ALINE

Note the two dashes, which tells set to take everything else on the 
command line and parse it into positional arguments (even if one of the 
arguments starts with a - ), and the $ in front of ALINE, which causes 
the shell to do varaible expansion.

>> /usr/sbin/useradd -c "$1 Account Whatever Comment Here" $1
>> echo $2 | /usr/bin/passwd --stdin $1
>> done
>> 
>> That aught to get it for you....

Good luck!

-- 
Charles Steinkuehler
charles at steinkuehler.net




More information about the Kclug mailing list