Is it possible to have multiple unary operators in if statements.. Here is the code snippet which is giving me error.
Please correct the code here.
if [ -f $input_file ] -a [ -f $output_file ] -a [ -f $log_file ] ]
then
### Some Code here
fi
Is it possible to have multiple unary operators in if statements.. Here is the code snippet which is giving me error.
Please correct the code here.
if [ -f $input_file ] -a [ -f $output_file ] -a [ -f $log_file ] ]
then
### Some Code here
fi
if [ -f "file1" -a -f "file2" -a "file3" ]; then
#some code
fi
如果你使用 Bash 的双括号,你可以这样做:
if [[ -f "$input_file" && -f "$output_file" && -f "$log_file" ]]
我发现它比这里显示的其他选项更容易阅读(但这是主观的)。但是,它还有其他优点。
而且,正如ghostdog74所示,您应该始终引用包含文件名的变量。
您只能将 [ ... ]
运算符视为 的快捷方式test ...
。选项的使用方式相同。
因此,在您的情况下,您可以编写ghostdog74方式或:
if [ -f $input_file ] && [ -f $output_file ] && [ -f $log_file ]
then
### Some Code here
fi
[
is a command, not part of the if
statement. As such you should pass it each of the appropriate arguments instead of trying to incorrectly run it as you have.
if [ arg1 arg2 arg3 arg4 ... ]
then