BASH 如果目录包含文件或不包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17766197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:58:55  来源:igfitidea点击:

BASH if directory contains files or doesn't

bashshell

提问by Geofferey

I created a script that executes some commands based on a condition. If directory contains files then run "screen -r" anything else run "screen". Problem is screen sometimes gets executed even when the directory contains files.

我创建了一个脚本,根据条件执行一些命令。如果目录包含文件,则运行“screen -r”,否则运行“screen”。问题是即使目录包含文件有时也会执行屏幕。

if [ "$(ls -A $DIR)" ]; then
screen -r
else
screen
fi

What I want to do is refine it and break it down into two statements. If directory contains files then run screen-r" & if a directory doesn't contain files run "screen"

我想做的是对其进行改进并将其分解为两个语句。如果目录包含文件,则运行 screen-r" & 如果目录不包含文件,则运行 "screen"

if [ "$(ls -A $DIR)" ]; then
screen -r
fi

&

&

if ["$(directory without files)"] ; then
screen
fi

Maybe even a statement that executes based on # of file. If directory contains X amount of files.

甚至可能是基于文件# 执行的语句。如果目录包含 X 个文件。

Can somebody help me out with this? I hope I explained what I want thoroughly.

有人可以帮我解决这个问题吗?我希望我彻底解释了我想要的东西。

Thanks,

谢谢,

Geofferey

杰弗里

Again thank you for all your help, I got it all working now. Here is the final script. It's for the iPhone and an app I'm making called MobileTerm Backgrounder.

再次感谢您的所有帮助,我现在一切正常。这是最终的脚本。它适用于 iPhone 和我正在制作的名为 MobileTerm 后台程序的应用程序。

#Sets up terminal environment? 

if [[ $TERM = network || -z $TERM ]]; then
export TERM=linux
fi

# This script automatically runs screen & screen -r (resume) based on a set of conditions. 

# Variable DIR (variable could be anything)

DIR="/tmp/screens/S-mobile"

# if /tmp/screens/S-mobile list files then run screen -x

if [ "$(ls -A $DIR)" ]; then
screen -x
fi

#if /tmp/screens/S-mobile contains X amount of files = to 0 then run screen -q

if [ $(ls -A "$DIR" | wc -l) -eq 0 ]; then
screen -q
fi

采纳答案by ДМИТРИЙ МАЛИКОВ

findcould be helpful here:

find在这里可能会有所帮助:

if [[ $(find ${dir} -type f | wc -l) -gt 0 ]]; then echo "ok"; fi

if [[ $(find ${dir} -type f | wc -l) -gt 0 ]]; then echo "ok"; fi

UPD: what is -gt?

UPD:是什么-gt

man bash-> / -gt/:

man bash-> / -gt/:

   arg1 OP arg2
          OP is one of -eq, -ne, -lt, -le, -gt, or -ge.  These arithmetic binary operators return true if  arg1  is  equal  to,  not
          equal  to,  less than, less than or equal to, greater than, or greater than or equal to arg2, respectively.  Arg1 and arg2
          may be positive or negative integers.

So, -gtis "greater than" boolean function.

所以,-gt是“大于”布尔函数。

回答by Fà Bio

I would use lsand wcthis way:

我会用lswc是这样的:

if [ $(ls -A "$DIR" | wc -l) -gt 0 ]; then
   screen -r
else
   screen
fi

You have to double quote the $DIRvariable otherwise you'll have problems with directory names that contains spaces.

你必须双引号$DIR变量否则你会遇到包含空格的目录名称的问题。