How to skip command if it is hanging while waiting for response

Hello,

I have a script that contains the command "whois 1.2.3.4"

Sometimes this command takes far too long to produce any output and as a result the rest of the script is not executed.

Can anyone suggest a method so that if no output is produced after say 2 seconds the script skips that command and continues on. I imagine some sort of if statement would do this.

Any assistance would be greatly appreciated.

Regards,
Colin

Do you have expect installed?
If not, you could run the command in background, save its pid, sleep for 2 seconds and, if the program is still running, kill it.

How about tis (if your sleep dosn't support decimal values ie only whole integers you will need to sleep 1 instead and reduce your wait count to 2).
If you don't have the /proc filesystem you will need to run ps -p $WPID 2> /dev/null and test the exit status

#!/bin/bash
whois 1.2.3.4 > /tmp/wi$$.out &
WPID=$!
while [[ -d /proc/$WPID ]] && [[ $((++wait)) -lt 8 ]]
do
    sleep 0.25
done
if [[ -d /proc/$WPID ]]
then
    WHOIS="<unknown>"
    echo "whois taking too long - killing"
    kill $WPID
else
    WHOIS=$(cat /tmp/wi$$.out)
fi
rm /tmp/wi$$.out