2

I want to have an if statement in bash that says

if ((var1 greater than var2) and 
   whole word var3 cannot be found in file path var4); then

    do this

fi

but I cannot seem to get the correct syntax

if [[ "$VAR1" -gt "$VAR2" && ! grep -q -w "$VAR3" "$VAR4" ]] ; then

I have tried many different combinations with [] () and still never works, anyone know of this?

is there a link to the bash boolean operation I can read up?

I have tried simpler expressions like if [[ ! grep -q -w "$VAR3" "$VAR4" ]]; then that had worked, but can't get it into a chain

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    are you sure you got `if [[ ! grep -q -w "$VAR3" "$VAR4" ]]; then` to work? – markp-fuso Aug 30 '23 at 16:42
  • 1
    Don't use `[[ ]]` around commands; `[[ ]]` and `[ ]` are things that let you use test expressions (e.g. comparisons) *in places where you'd normally be testing a command's success*. That means you don't put them around a command you want to test the success of, like the `grep` command here. You should only put them around the test expression part. See my answer [here](https://stackoverflow.com/questions/49849957/bash-conditional-based-on-exit-code-of-command). – Gordon Davisson Aug 30 '23 at 16:59
  • Re: "is there a link to the bash boolean operation I can read up?": The [Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html) is very helpful, though in your case the problem was that you already had some sort of misconception that led you to think the `grep` should be inside `[[ ... ]]`, and the reference manual does *not* make a point of addressing misconceptions, so you might have found it difficult to correct your misconception just by reading it. :-/ – ruakh Aug 30 '23 at 17:02
  • [Shellcheck](https://www.shellcheck.net/) can identify many common problems with shell code. The report includes links to more information about the problems and how to fix them. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Aug 30 '23 at 21:39
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. See [the relevant help-center article](https://stackoverflow.com/help/someone-answers) – Gilles Quénot Sep 02 '23 at 22:56

2 Answers2

2

Like this:

if ((VAR1 > VAR2)) && ! grep -q -w "$VAR3" "$VAR4"; then
    [...]

The (( )) operators are bash arithmetic.

See http://mywiki.wooledge.org/ArithmeticExpression

You can't use grep inside bash test [[ ]]

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

One idea:

if [[ "$VAR1" -gt "$VAR2" ]] && ! grep -q -w "$VAR3" "$VAR4" ; then
                             ^^^^^^^^^

Where:

  • pull the && ! grep outside of the [[ ... ]] construct
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • `grep -q -v ...` returns success if there's *even one* line that doesn't include the search term; whereas the OP only wants success if *no* lines include the search term. So the OP should stick with `! grep -q ...`. – ruakh Aug 30 '23 at 16:47
  • 1
    @ruakh true; I was focused on a small sample in my env; thanks – markp-fuso Aug 30 '23 at 16:50
  • Thanks, putting grep outside [[ ]] was the correct way to do it – Jimmy Chi Kin Chau Sep 02 '23 at 20:59