0

I have a bash script and I want to check with an (el) if statement if a string is not in a file and then run an action

This is the part of the script:

elif [ "$(cat balance.txt)" -eq 250000000 ] && [ "$(find files/ -type f | wc -l)" -ge 5 ] && [ "$(grep -c "$(cat string.txt)" whitelist.txt)" -ge 1 ] && [ "$(grep -c "$(cat string.txt)" whitelist2.txt) " -eq 0 ] ;   
then
sh result.sh 

The first 2 parts of the elif statements are working but only the third part gives me problems:

[ "$(grep -c "$(cat string.txt)" whitelist2.txt) " -eq 0 ] 

It skips this part and goes straight to the else part of the script which runs fine.

Does anybody maybe knows what I am doing wrong? Or what is should change?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • That's the 4th part. The 3rd part is `[ "$(grep -c "$(cat string.txt)" whitelist.txt)" -ge 1 ]`. – Barmar Jul 11 '22 at 21:02
  • Are you sure the string isn't in the file? It might be matching part of a line. Use `grep -x` to match the whole line. – Barmar Jul 11 '22 at 21:03
  • Thanks for your input. i found the problem :D – Koelie1984 Jul 11 '22 at 21:15
  • 3
    To see if a file contains a pattern, don't use `grep -c` and compare the output to see if it's 0; use the status of `grep -q` directly. See ["Checking if output of a command contains a certain string in a shell script"](https://stackoverflow.com/questions/16931244/checking-if-output-of-a-command-contains-a-certain-string-in-a-shell-script) (except use it on a file, rather than a pipe from another command). – Gordon Davisson Jul 11 '22 at 21:21
  • 1
    Glad you solved your problem, but I've voted to close as a typo as this is unlikely to help anyone in the future AND there probably numerous other Q/As that deal with this issue already. For your future reference, read the section "How to turn a bad code into a good question" in https://stackoverflow.com/tags/bash/info . Good luck. – shellter Jul 11 '22 at 22:14
  • You are aware that your `grep` does not search for literal strings in the lines, but for lines which match a certain regular expression, aren't you? Since you did not show here the content of `string.txt`, I can't say whether this causes the problem. – user1934428 Jul 12 '22 at 05:25

0 Answers0