首页 > 解决方案 > Bash 语法:如果 [ ! -F \”{}\” ]; 然后退出1;菲'

问题描述

我正在 Apache Airflow 的 Bash Operator 中阅读这个 bash 命令,尽管尝试了一些谷歌搜索,但我还是无法真正理解它。

if [ ! -f \"{}\" ]; then exit 1; fi

在这个代码块中:

check_file_existence =  BashOperator(
    task_id='check_file_existence',
    bash_command='if [ ! -f \"{}\" ]; then exit 1; fi'.format(input_file))

你能帮我解释一下这个 bash 命令吗?

标签: bashairflow

解决方案


format()方法将替换{}为 的值input_file。如果值为input_fileis somefile.txt,则 shell 命令将变为

if [ ! -f "somefile.txt" ]; then exit 1; fi

如果文件不存在,这将以非零状态码退出,指示错误。

if语句并不是真正需要的,因为[ortest命令本身的工作方式相同。它可以简化为

check_file_existence =  BashOperator(
    task_id='check_file_existence',
    bash_command='test -f \"{}\"'.format(input_file))

推荐阅读