help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] catching read error conditions


From: Mart Frauenlob
Subject: [Help-bash] catching read error conditions
Date: Sat, 26 Jan 2013 23:34:12 +0100
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2

Hello,

I'm trying to catch two error conditions in this example script.

1. Detect if read times out. This is the only way I found that seems to work ( || ... after the read command). Is this correct?

2. Detect if the command inside the redirected process substitution fails and print a message to stderr. What happens for me here is, that after the script exits, the prompt returns, but then suddenly after a while the 'failed' message turns up (The time varied in my tests, also using TMOUT variable). Everything after the || is executed and the shell hangs without a prompt. I have to hit return to get it back.
What am I doing wrong?

# cat read_fun
#!/bin/bash
set -x
while read -r -t 1 || \
    { (($? > 128)) && \
        printf "timeout reached\n" >&2 && break; }; \
    do :
done < <( { sleep 2 ; false; } || { printf "failed\n" >&2; exit 2; })
exit $?

# ./read_fun
+ read -r -t 1
++ sleep 2
+ (( 142 > 128 ))
+ printf 'timeout reached\n'
timeout reached
+ break
+ exit 0
# ++ false
++ printf 'failed\n'
failed
++ exit 2

<-- no prompt, have to hit return.


Best regards

Mart



reply via email to

[Prev in Thread] Current Thread [Next in Thread]