bash - 我用于检查闰年的 shell 脚本显示错误

标签 bash shell

#!/bin/bash

echo "Enter the year (YYYY)"
read year

if[ $((year % 4)) -eq 0] 
then
  if[ $((year % 100)) -eq 0] 
    then
    if[ $((year % 400)) -eq 0] 
          then 
        echo "its a leap year"
    else
           echo "its not a leap year"
    fi
  else
  echo "Its not a leap year"
  fi
else
 echo "its not a leap year"

fi

它在第 7 行和

上显示错误
[ $((year % 4)) -eq 0] 

最佳答案

你把它搞得太复杂了。使用此代码计算闰年:

isleap() { 
   year=$1
   (( !(year % 4) && ( year % 100 || !(year % 400) ) )) &&
      echo "leap year" || echo "not a leap"
}

测试一下:

$ isleap 1900
not a leap
$ isleap 2000
leap year
$ isleap 2016
leap year
$ isleap 1800
not a leap
$ isleap 1600
leap year

关于bash - 我用于检查闰年的 shell 脚本显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196628/

相关文章:

regex - 当我匹配所有字符时在 SED 中转义 ~

linux - 终止 lxsession (xorg) 后运行脚本

bash shell 脚本 : writing to file does not work

bash - Basename 无法接收带破折号的文件名

bash - 如何使用 Go 获取 shell 脚本?

bash - 使用终端在 Ubuntu 中移动特定大小的文件

mysql - 如何避免 MySQL/BaSH 中的特殊/转义字符

linux - 找不到 bash 脚本命令 - 命令已正确执行

linux - 从文本文件数据库中替换当前字符串

shell - awk: hping: 打印 icmp 发起/接收之间的差异