8

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
4

4 回答 4

10
if [ -f "file1" -a -f "file2" -a "file3" ]; then
   #some code
fi
于 2010-02-22T09:16:49.160 回答
8

如果你使用 Bash 的双括号,你可以这样做:

if [[ -f "$input_file" && -f "$output_file" && -f "$log_file" ]]

我发现它比这里显示的其他选项更容易阅读(但这是主观的)。但是,它还有其他优点

而且,正如ghostdog74所示,您应该始终引用包含文件名的变量。

于 2010-02-22T13:13:58.957 回答
5

您只能将 [ ... ]运算符视为 的快捷方式test ...。选项的使用方式相同。

因此,在您的情况下,您可以编写ghostdog74方式或:

if [ -f $input_file ] && [ -f $output_file ] && [ -f $log_file ]
then
### Some Code here
fi
于 2010-02-22T09:30:18.540 回答
1

[ 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
于 2010-02-22T09:16:56.687 回答