You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash循环处理逐小时netcdf文件生成日平均文件报错如何解决

问题原因
  • 条件判断语法错误:Bash 中[是内置命令,前后必须保留空格,你写的[$month -eq 1]会被系统识别为[11这类不存在的命令,对应报错里的[11: command not found。
  • 变量赋值语法错误:Bash 变量赋值时等号两侧不能有空格,你写的iy = 31会被系统当成执行iy命令,对应报错里的iy: command not found。
  • 逻辑顺序错误:你把按日遍历的for day in $(seq 1 $iy)写在了判断每月天数iy的逻辑前面,首次执行时iy还未赋值,遍历根本无法正常执行。
  • 循环结构不完整:三层for循环都没有对应的done闭合,脚本运行会报语法错误。
  • ncea参数顺序错误:ncea的-o参数后面接输出文件路径,你原来的参数顺序写反了,会导致文件读取和输出逻辑异常。
  • 调试参数错误:set echo不是Bash开启命令回显的正确写法,正确写法是set -x。
修正后的完整脚本
set -x
for (( year = 2019 ; year <= 2019 ; year++))
do
  for ((month = 1 ; month <=12 ; month++)); do
    # 先计算当前月的天数
    if [ $month -eq 1 ] || [ $month -eq 3 ] ||  [ $month -eq 5 ] || [ $month -eq 7 ]  || [ $month -eq 8 ] || [ $month -eq 10 ] || [ $month -eq 12 ]; then 
      iy=31 
    elif [ $month -eq 4 ] || [ $month -eq 6 ] || [ $month -eq 9 ] || [ $month -eq 11 ]; then 
      iy=30 
    else 
      # 如需兼容闰年可取消下方注释,2019年不是闰年直接用28即可
      # leap=$((year%4==0 && (year%100!=0 || year%400==0) ? 1 : 0))
      # iy=$((28+leap))
      iy=28
    fi
    # 计算完天数再遍历日期
    for day in $(seq 1 $iy); do
      dd=$day
      if [ $day -lt 10 ]; then
        dd=0$day
      fi 
      mm=$month
      if [ $month -lt 10 ]; then
        mm=0$month
      fi
      # 修正ncea参数顺序,输入文件在前,-o指定输出文件
      ncea ./$year$mm$dd??.nc -o ./$year$mm$dd.nc
    done
  done
done

内容的提问来源于stack exchange,提问作者J A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 07:45:00