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

Airflow 2.0中使用BashOperator向Shell脚本传参失败求助

问题原因

你当前的写法里,bash_command中直接传入的Date和Hr是字面字符串,Airflow不会将其替换为你定义的变量值,也不会解析模板语法,所以脚本接收到的就是字符串"Date"和"Hr"。

正确传参方式

方法1:直接在bash_command中嵌入模板语法

不需要单独定义变量,直接把模板表达式写在bash_command的参数位置:

BashOperator(
    task_id="your_task_id",
    dag=dag,
    bash_command=''' 
    echo "hi" 
    /path/file.sh {{ (next_execution_date + macros.timedelta(hours=9, days=-3)).strftime("%Y%m%d") }} {{ (next_execution_date + macros.timedelta(hours=9, days=-3)).strftime("%H") }}
    '''
)

方法2:引用定义的变量(确保模板被渲染)

如果要保留单独定义的变量,需将变量正确插入到bash_command中,让Airflow能解析模板:

# 定义日期和小时变量
target_date = '{{ (next_execution_date + macros.timedelta(hours=9, days=-3)).strftime("%Y%m%d") }}'
target_hour = '{{ (next_execution_date + macros.timedelta(hours=9, days=-3)).strftime("%H") }}'

BashOperator(
    task_id="your_task_id",
    dag=dag,
    bash_command=f''' 
    echo "hi" 
    /path/file.sh {target_date} {target_hour}
    '''
)

注意事项

  • Airflow默认会对BashOperator的bash_command字段进行模板渲染,无需额外配置template_fields。
  • 你的file.sh脚本写法没问题,$1和$2会正确接收传入的第一个和第二个参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 12:45:37