0

I have the following bash script

i=1
while SH= read -r LINE
    do
        if echo $LINE | grep 'Network connection down!'
        then 
            echo "shutdown"
            exit
    elif echo $LINE | grep 'Network connection available'
    then
        echo $LINE
        ((i++))
    fi

done < <(tac /var/log/messages)

This script works so far. I am not an expert at shell scripting. I am trying to add an and operator to the first conditional statement, something like this

if echo $LINE | grep 'Network connection down!' && $i<4

Can anyone help show me how to make this work in shell script, every variation I have tried so far ends with various errors like

conditional binary operator expected

and various other ones that I can describe. Many thanks to everyone in advance :)

7
  • I think what you want is [[ $i < 4 ]] Commented Oct 5, 2015 at 18:24
  • like if echo $LINE | grep 'Network connection down!' && [[$i<4]] ? Commented Oct 5, 2015 at 18:25
  • @lindsaymacvean: be careful of spaces inside [[ ... ]] Commented Oct 5, 2015 at 18:29
  • Yes, but put spaces around <, it's important Commented Oct 5, 2015 at 18:29
  • 1
    ...since [[ i < 4 ]] won't do what you intend if i=39. Commented Oct 5, 2015 at 18:35

2 Answers 2

4

Shell programming requires a special command to perform comparisons, as the if statement itself does not work with expressions, only commands. In bash, the command to use is the conditional command, [[ ... ]].

if echo "$line" | grep 'Network connection down!' && [[ $i -lt 4 ]]; then

You can use an arithmetic command (( ... )) as well, which allows a more familiar looking expression:

if echo "$line" | grep 'Network connection down!' && (( i < 4 )); then

You can also replace your call to grep with the =~ operator:

if [[ $line =~ Network\ connection\ down! && $i -lt 4 ]]; then

( or [[ $line =~ Network \connection \down! ]] && (( i < 4)), but you probably get the point).

Sign up to request clarification or add additional context in comments.

3 Comments

woops, sorry. Forgot we were talking about numbers
perfect thanks. my script worked with [[ $i <4 ]] does it require (( parentheses when using < instead of -lt ?
Inside [[ ... ]], -lt is used for integer comparisons and < is used for string comparisons. Inside ((...)), everything is an arithmetic operation, so < can be used for integer comparisons instead of -lt.
0

The "if" line can be written this way. I've changed "<" by "-lt" since "<" and ">" are supposed to be used with strings, while -lt, -gt, -eq and others are better for numbers.

if [[ `echo $LINE|grep -c 'Network connection down!'` -eq 1 && $i -gt 4 ]]

The execution of this part counts the lines with text 'Network connection down!', always one or zero. Then this result is compared to one (1)

echo $LINE|grep -c 'Network connection down!' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.