HaBo
HaBo

Reputation: 14297

Bash script multiple if express groups

I am new to bash scripts, I am trying to group multiple expressions in my if statement

minuteRun = $1
if 
[
    [ [ $minuteRun -eq 25 ] &&  [ $HR != 01] && [ $HR != 13 ] ] || 
    [ [ $minuteRun -eq 50 ] && [ $HR -eq 01 || $HR -eq 13 ] ]
]   
 then

I call it as ./script.sh 45

Here are the errors

Upvotes: 2

Views: 88

Answers (2)

Ivan
Ivan

Reputation: 7297

Personally i'd use case for this

minuteRun=50 HR=01
case $minuteRun:$HR in
     50:01|50:13) echo ok;;
      *:01|*:13 ) echo fail;;
esac

Upvotes: 1

anubhava
anubhava

Reputation: 785631

There are syntax errors esp. space between brackets e.g. [ [

It is better to use arithmetic context in bash for this using (( ...)):

#!/usr/bin/env bash

minuteRun=$1

if 
((
    ( minuteRun == 25 && HR != 1 && HR != 13 )
    || 
    ( minuteRun == 50 && ( HR == 1 || HR == 13 ) )
))   
 then

Upvotes: 2

Related Questions