Shell script

brad brad at bradandkim.net
Wed Nov 12 21:16:46 CST 2003


OK, I got it working all the way.  Thanks for everyone's input, but I
ended up going with Charles' script.  It looked the least Greek to me
and was the easiest to finish off.  Jason, I don't have ruby installed
so I didn't try any of yours.  I do take your suggestion of scripting
seriously though and will probably try learning perl.  I can see the
power of it, but it is ugly to someone with no programming experience. 
Shell scripts seem a little more intuitive and you can use man.  For
anyone interested, here is the final script:
-------------------------------------------------
#!/bin/sh

myprocedure () {
   local IFS='  '       
   while read USER PASS JUNK
   do
     adduser -g ftp -s /etc/ftponly -d
	/var/www/html/web.domain.com/./$USER $USER
     echo $USER:$PASS |chpasswd
     chown $USER:ftp /var/www/html/web.domain.com/$USER
     chmod 705 /var/www/html/web.domain.com/$USER
   done
}

myprocedure < userlist

------------------------------------------------

Thanks again!

Brad

Charles wrote:
> 
> <code>
> #!/bin/sh
> myprocedure () {
>    local IFS='	'	# There's a tab in there!
>    while read USER PASS JUNK
>    do
>      # Do your thing here
>      echo User: $USER
>      echo Pass: $PASS
>    done
> }
> 
> myprocedure < /path/to/file
> <code>
> 
> Of course, there are about a zillion other possible ways to do this, and 
> lots of features you can add to the above.  One of my personal favorites 
> is to support blank-space and comments by adding a simple case statement 
> inside the do loop:
> 
> <code>
>      # Skip comments and blank lines
>      case "$USER" in
>        #*|"") continue ;;
>      esac
> </code>
> 
> If you don't need to mess with IFS (to change whitespace to only tab, 
> default is tab, space, and newline), you can even do the above pretty 
> easily on the command line:
> 
> [admin at mongoose config]$ while read A B C
>  > do
>  > echo $A
>  > echo $B
>  > echo $C
>  > echo -----
>  > done </my/text/file
> 
> <output appears here :>
> 
> NOTE:  When using read to grab info from a file like this (or in 
> general) it's always wise to include one more parameter than you 
> currently support.  If you only read two variables, and there are more 
> than two fields in your file, you'll get all the extras jammed into the 
> last read variable:
> 
> $ echo 1 2 3 4 | ( read A B ; echo $A ; echo $B ; )
> 1
> 2 3 4
> $ echo 1 2 3 4 | ( read A B C ; echo $A ; echo $B ; )
> 1
> 2
> $
> 
> ...hence the third variable "JUNK" in the above example.




More information about the Kclug mailing list