Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to put an error checking solution to my script, it looks like this:

! [ `echo $?` != 0 ] || echo "omg, st went wrong, CTRL+C? `read`"

My goal is when this line runs it check the "$?" - if it's not 0 then it says "omg, st went wrong, CTRL+C?" then it waits for enter, or CTRL+C from the user. But my one-liner isn't working, because it waits for an enter before printing the "omg, st went wrong, CTRL+C?" text. Can someone help me what am I missing?

Thanks.

UPDATE: it would be better to do it after the command st like in: Check that there was an error one-liner in script

the "bad command" is "uname -k" the good command is "echo hi" - the uname gives non zero exit code, the echo gives 0

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

That's correct; the `read` will be performed and the result interpolated into the string sent to echo.

You're very much doing this the hard way, with all those interpolations and such.

if [[ $? -ne 0 ]]; then
    echo -n 'command failed.  ^C or "q" to abort.'
    read c
    [[ "$c" == q ]] && exit
fi
share|improve this answer
add comment

got the solution:

$ uname -k || echo 'omg, fail' || read
uname: invalid option -- 'k'
Try `uname --help' for more information.
omg, fail
$ echo hi || echo 'omg, fail' || read
hi
$ 

UPDATE: whoops, it doesn't waits for enter after printing the text :\

share|improve this answer
    
That doesn't work because the echo statement returns true. Try uname -k || { echo 'omg, fail' && read; } instead. –  CodeGnome May 15 '12 at 19:41
add comment

When I write possibly long running scripts; I prefer to get a warning with a pop up. E.g. with zenity

if [[ $? -ne 0 ]]; then
    if  zenity --question --text="Omg, something went wrong, do you wish to continue?"; then
    echo continue with program
    else
    echo "exiting" && exit; 
    fi
fi

This way I can do some other work and get notification if something breaks. Also, zenity info message is usually put at the end of script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.