#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0

    KSH: Reading Configuration File With While Loop Issue


    I have a ksh script that reads a configuration file, then calls a function with the variable as an argument:



    egrep '^[^# ]' $BATCHCONFIG | while read i; do

    export VAR1=`echo $i | cut -f1 -d:`
    export VAR2=`echo $i | cut -f2 -d:`
    export VAR3=`echo $i | cut -f3 -d:`

    do_something $VAR3

    done


    The problem is that the function that is called calls a binary that sometimes requires user input. The problem is that the user input is then read by the outer while loop and seen as input by the associated read command, thus prempting any chance for the user to provide said input.

    I've isolated the issue to the 'while read i; do' loop as the problem does not occur when calling the function manually.

    Any ideas how to get around this?

    -A.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    Figured it out.... for those who are interested, the solution lay in defining an alternate file descriptor for the configuration file. The following script shows how it works:


    #!/bin/ksh

    while read -u3 i; do
    if [ "x`echo $i | egrep '^[^# ]'`" = "x" ]; then
    continue
    fi
    export TARGET=`echo $i | cut -f1 -d:`
    export USER=`echo $i | cut -f2 -d:`

    echo "Target: $TARGET"
    echo "User: $USER"

    read dumm #read from stdin
    echo "Dummy: $dumm"
    done 3<test.conf

IMN logo majestic logo threadwatch logo seochat tools logo