Multiple unary operators in an if statement [duplicate]

橙三吉。 提交于 2019-12-05 03:46:53
if [ -f "file1" -a -f "file2" -a "file3" ]; then
   #some code
fi

If you use Bash's double-bracket, you can do this:

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

which I find cleaner to read than other options shown here (but that's subjective). However, it has other advantages.

And, as ghostdog74 shows, you should always quote variables containing filenames.

You can see the [ ... ] operator only as a shortcut for test .... The options are used th same way.

So in your case you could either write the ghostdog74 way or :

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!