在if语句bash脚本使用运营商和(安培;&AMP) [英] using and (&&) operator in if statement bash script

查看:121
本文介绍了在if语句bash脚本使用运营商和(安培;&AMP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个变量:

VAR1="file1"
VAR2="file2"
VAR3="file3"

如何使用和(&放大器;&安培; )运算符在if语句是这样的:

How to use and (&&) operator in if statement like this:

if [ -f $VAR1 && -f $VAR2 && -f $VAR3 ]
   then ...
fi

在我写这篇文章code提示错误。什么是正确的方法是什么?

When I write this code it gives error. What is the right way?

推荐答案

因此​​,为了使你的前pression工作,改变&放大器;&安培; -a 会做的伎俩。

So to make your expression work, changing && for -a will do the trick.

这是正确的是这样的:

 if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ]
 then  ....

或类似

 if [[ -f $VAR1 && -f $VAR2 && -f $VAR3 ]]
 then  ....

甚至

 if [ -f $VAR1 -a -f $VAR2 -a -f $VAR3 ]
 then  ....

您可以找到这个问题的 bash的进一步的细节:在给定的,如果有喜欢<声明以及一些参考多一元运算符A HREF =htt​​p://mywiki.wooledge.org/BashFAQ/031>什么是测试之间的差异,[和[[?。

You can find further details in this question bash : Multiple Unary operators in if statement and some references given there like What is the difference between test, [ and [[ ?.

这篇关于在if语句bash脚本使用运营商和(安培;&AMP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆