Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

airflow - Bash syntax: if [ ! -f "{}" ]; then exit 1; fi'

I'm reading this bash command in a Bash Operator for Apache Airflow and I couldn't really understand it despite some Googling attempt.

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

in this code chunk:

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

Could you help me explaining this bash command?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The format() method will replace {} with the value of input_file. If the value of input_file is somefile.txt, the shell command will become

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

This will exit with a non-zero status code, indicating an error, if the file doesn't exist.

The if statement isn't really needed, since the [ or test command itself works the same way. It can be simplified to

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...