Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep and integer expression expected

I have just lost my mind. I code in Windows, now i have to make one tiny plug-in in Linux, to get communicated with my main code. I make code in bash, and on Windows, everything will be ok, but here, on Debian... I have spend 2 days to figure out what is going on, and tried almost everything.

It is my code :

#!/bin/bash 
search1=`cat /home/qlik/skrypty/windows/kody.txt | grep -E '[0-2]'`
case $search1 in
[|1|])
echo "Error, need restart"
exit 2
;;
case $search1 in
[|2|])
echo "Warning with server process, waiting"
exit 1
;;
*)
echo "OK"
exit 0
;;
esac

I have also tried that way

#!/bin/bash
plik1='/home/qlik/skrypty/windows/kody.txt'
szukaj1=$( grep 1 $plik )
szukaj2=$( grep 2 $plik )
if [ $($szukaj1) -eq "1" ] ; then
echo "Error, need restart"
exit 2
elif [ $($szukaj2) -eq "2" ] ; then
echo "Server process warning, waiting"
exit 1
else
echo "OK"
exit 0
fi

and all kinds of different ways. All the time I've got an errors :

integer expression expected

or

binary operator expected

or

[: -eq: unary operator expected

I have read about that, i think the problem is with symbols $(...)/[...]/.../'...'/[[...]]/$($...)/"..." and I really, really don't know what to do with my code, i tried almost all configurations.

The code is simple -

  • it read file.txt
  • in file.txt there are 4 numbers [for example : 0 0 2 0 ]

  • if all are 0, then is ok,

  • if one of them is 1, then is error

  • if one of them is 2, then is information that process failed.

  • when 1 or 2 are met, the exit code should be the same as error in txt

I just need to recognize what number is in file.txt and show different information and exit code.

Could You please help me?

like image 286
Renver Avatar asked Feb 03 '26 14:02

Renver


1 Answers

Try:

#!/bin/bash

plik1='/home/qlik/skrypty/windows/kody.txt'

if grep -q -- 1 "$plik1" ; then
    echo "Error, need restart"
    exit 2
elif grep -q -- 2 "$plik1" ; then
    echo "Server process warning, waiting"
    exit 1
else
    echo "OK"
    exit 0
fi

It's based on the second code example in the question, but the szukaj variables have been removed because they aren't necessary (and they were being used incorrectly).

like image 195
pjh Avatar answered Feb 06 '26 07:02

pjh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!