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

Viewed 118

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?

1 Answers

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))
Related