3

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?

4
  • What do you see when you launch echo $search1? Commented Oct 16, 2018 at 11:24
  • when i leave in my script : search1=cat /home/qlik/skrypty/windows/kody.txt | grep -E '[0-2]' echo $search1 the result is, like my txt file : 0 0 1 0 Commented Oct 16, 2018 at 12:07
  • when i leave all as it was, execute the script in main console of linux i get * ./qlik_procesresponse.sh: line 8: syntax error near unexpected token $search1' ./qlik_procesresponse.sh: line 8: case $search1 in' * and then on that console when I wrote *echo $search1, the result is blank space, like echo of anything = blank enter Commented Oct 16, 2018 at 12:40
  • Shellcheck finds several problems with the code presented in the question. Commented Oct 16, 2018 at 13:39

1 Answer 1

1

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).

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

2 Comments

TY, it works ok. So in summary grep could be used next to if, and it does't need any (). Why is it so chaotic with that brackets and $ things? Could you provide me some sources, where I can learn about that?
@Renver, see How do I use a file grep comparison inside a bash if/else statement? and BashPitfalls #9 for more information about the use of grep with if. The use of $ is confusing because it does three different things: parameter expansion, command substitution, and arithmetic expansion. See Bash Guide for Beginners - Shell expansion for more information.

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.