具有多个条件的if语句 [英] An if statement with multiple conditions
问题描述
由于我对Bash缺乏知识,所以我遇到了一个小问题.我有41个文件夹的应用程序"目录.在每个文件夹中,都有一个css
子目录,其中包含胶水1.css文件.我的目标是通过bash循环删除该css文件,但我需要免除两个文件夹:ad
和glue-resources
.
Due to my lack of knowledge in Bash, I come to you with a trivial problem. I have an 'Apps' directory of 41 folders. In each folder, there is a css
subdirectory containing a glue1.css file. My goal is to remove that css file via a bash loop, but I need to exempt two folders: ad
and glue-resources
.
我需要免除ad
文件夹,因为没有css文件.我想防止执行时发生以下错误:
I need to exempt the ad
folder because there is no css file. I want to prevent the following error from occurring upon execution:
rm: ad/css/glue1.css: No such file or directory
考虑以下假设目录(为简洁起见,缺少41个文件夹):
Consider this hypothetical directory (lacking 41 folders for brevity):
Apps
ad
css
(empty)
glue-resources
css
glue1.css (keep this file!)
profile
css
glue1.css (remove this file!)
...
这是我到目前为止的代码.如果父目录路径不等于glue-resources/css/
或ad/css/
,则应在胶1.css文件上调用rm
.
Here is the code I have so far. It should call rm
on the glue1.css file if the parent directory path is NOT equal to glue-resources/css/
or ad/css/
.
for dir in **/css/; do
if [[ "${dir}" != 'glue-resources/css/' || "${dir}" != 'ad/css/' ]]; then
rm "${dir}"glue1.css
fi
done
以上代码结果不起作用.我收到同样的没有这样的文件或目录"错误,并且已删除glue-resources/css
中的gum1.css文件.我不太确定为什么它不起作用-我在某处读到,如果您使用[[
和]]
,则可以组合多个条件.
The above code results doesn't work. I get the same 'No such file or directory' error and the glue1.css file in glue-resources/css
has been removed. I'm not quite sure why it doesn't work—I read somewhere that if you use [[
and ]]
you can combine multiple conditions.
我认为问题在于我不了解bash中的多个条件如何工作.以下版本的代码有效.
I think the problem is that I don't understand how multiple conditions in bash work. The following versions of the code work.
for dir in **/css/; do
if [[ "${dir}" != 'glue-resources/css/' ]]; then
rm "${dir}"glue1.css
fi
done
for dir in **/css/; do
if [[ "${dir}" != 'ad/css/' ]]; then
rm "${dir}"glue1.css
fi
done
我尝试向if语句添加多个条件失败.
My attempt to add multiple conditions to the if statement failed.
推荐答案
由于||
表示逻辑或,因此以下条件将始终返回true:
Because ||
represents logical-OR, the following condition will always return true:
[[ "${dir}" != 'glue-resources/css/' || "${dir}" != 'ad/css/' ]]
我怀疑您想要的是:
[[ "${dir}" != 'glue-resources/css/' && "${dir}" != 'ad/css/' ]]
&&
是逻辑与.它要求同时满足两个条件.
&&
is logical-AND. It requires that both conditions be met at the same time.
这篇关于具有多个条件的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!