简体   繁体   English

Bash 脚本多个 if express 组

[英]Bash script multiple if express groups

I am new to bash scripts, I am trying to group multiple expressions in my if statement我是 bash 脚本的新手,我正在尝试在 if 语句中对多个表达式进行分组

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我称之为./script.sh 45

Here are the errors这是错误

  • ./script.sh: line 25: [: missing `]' ./script.sh:第 25 行:[:缺少 `]'
  • ./script.sh: line 26: [: too many arguments ./script.sh:第 26 行:[:太多 arguments
  • ./script.sh: line 27: [: too many arguments ./script.sh:第 27 行:[:太多 arguments
  • ./script.sh: line 27: 12: command not found ./script.sh:第 27 行:12:找不到命令
  • ./script.sh: line 28: ]: command not found ./script.sh:第 28 行:]:找不到命令

There are syntax errors esp.有语法错误,尤其是。 space between brackets eg [ [括号之间的空格,例如[ [

It is better to use arithmetic context in bash for this using ((...)) :为此,最好在 bash 中使用算术上下文((...))

#!/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我个人会为此使用case

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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