简体   繁体   中英

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

  • ./script.sh: line 25: [: missing `]'
  • ./script.sh: line 26: [: too many arguments
  • ./script.sh: line 27: [: too many arguments
  • ./script.sh: line 27: 12: command not found
  • ./script.sh: line 28: ]: command not found

There are syntax errors esp. space between brackets eg [ [

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM