LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   What is wrong in the statement: $line || echo "error" && break; (https://www.linuxquestions.org/questions/linux-general-1/what-is-wrong-in-the-statement-%24line-%7C%7C-echo-error-and-and-break%3B-470976/)

nadavvin 08-05-2006 04:42 AM

What is wrong in the statement: $line || echo "error" && break;
 
I did a little script to execute commands:

Code:

#!/bin/bash
exec < $1
while read line;
do     
        $line || echo "error" && break;
done

The problem is that the loop break after the first time.

what is the problem?

if $line is work fine, it is not supposed to execute the right side of the OR gate.

if $line is failed, it's supposed to continue the echo "error" and this is should to return true and only then execute the break.

if I use:
Code:

#!/bin/bash
exec < $1
while read line;
do     
        $line || break;
done

it's work good.

and also this code:
Code:

#!/bin/bash
exec < $1
while read line;
do

        $line || !(echo "error") || break;
done


gilead 08-05-2006 05:25 AM

Aren't both operators of equal precedence? If they are, it executes from left to right, evaluating the || first and if either one was true it does the &&, causing the break. In effect it acts like:
Code:

($line || echo "error") && break
But you want it to behave like:
Code:

$line || ( echo "error" && break)
Those brackets are only me trying to explain what is happening, I haven't checked whether you can group operations like that with them.


All times are GMT -5. The time now is 11:07 PM.