script hangs when reading from stdin

script:

   while read inputline; do
   if [ "$inputline" != "" ] ; then
   if [ ! -z "`(echo $inputline | grep \#END)`" ]; then
   break
   fi
   fi
   done

Looks like the script hangs when stdin is empty or contains space. Any ideas on how to circumvent this? is it possible to use getline to process stdin content?

What exactly are you trying to do with this script?

I don't see any reason that should hang unless stdin does. What do you have on stdin?

For that matter -- what's your system? What's your shell? I'm sure you don't need to use grep in backticks to tell whether the string contains #END, using shell builtins will be hundreds of times faster.

#!/bin/bash

while read inputline
do
        [[ -z "${inputline}" ]] && continue
        [[ "${inputline}" == *#END* ]] && break

        echo $inputline
done < fdata

As for using getline to process input -- that also depends on your system and shell. It'd also be helpful to know what you wanted to use it for.

it's KSH on AIX. trying to use the script to capture errors generated by another program into the stdin.

Are you sure the program is printing them to stdout? Usually they go to stderr.

1 Like