Search code examples
bashshellwhile-loopsolaris-10

How to execute an input asking utility in a loop within a shell script?


I am trying to run an utility(nco_confpack) which works perfectly fine when used directly on command line. But when I am using the same utility in a while loop in a shell script the utility enters into infinite loop without accepting any input. How to avoid that infinite loop for utility ? Below is the code snippet which is working fine

bash-3.2$ $OMNIHOME/bin/nco_confpack -import -server SERVERA -user root -password XXXXX-select Class.list -package Class.jar
*********************************************************************
*                          W A R N I N G                            *
*                                                                   *
* This action may overwrite configuration currently in your system. *
*                                                                   *
* It is recommended that a backup is made of the current data       *
* before importing new data.                                        *
*                                                                   *
*********************************************************************
Do you want to continue (y/n) [N]? y
bash-3.2$

However, when the same utility is placed in a while loop the Do you want to continue(y/n) message enters into a infinite loop without accepting any input

bash-3.2$ ./classInstall.sh
Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? Do you want to continue (y/n) [N]? ^C

What I am doing wrong here ? Is something wrong with the utility or do I have to mention something in script explicitly ?

The script contains the same command just that its running under a finite while loop.

bash-3.2$vi classInstall.sh
cat $1 | while read theLine
do
$OMNIHOME/bin/nco_confpack -import -server SERVERA -user root -password XXXXX-select Class.list -package Class.jar
done

$1 contains two entries only so the loop , and the command is expected to run twice only


Solution

  • I'm not entirely sure why the while loop feeds your utility with line feeds, but you can solve the problem by feeding the expected 'y' to the utility yourself:

    cat $1 | while read theLine
    do
        echo y | $OMNIHOME/bin/nco_confpack -import -server SERVERA -user root -password XXXXX-select Class.list -package Class.jar
    done