bash 测试文件是否存在,bash脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12363594/
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
Test if file exist, bash script
提问by Yotam
Possible Duplicate:
Checking if a directory contains files
可能的重复:
检查目录是否包含文件
I want to see if a directory is empty, so I am using [
as follows:
我想看看一个目录是否为空,所以我使用[
如下:
[ -f "./ini/*"]
And the return value is always 1, even though the directory is not empty. Any ideas what's wrong?
并且返回值始终为 1,即使目录不为空。任何想法有什么问题?
回答by David W.
Try this:
尝试这个:
$ echo "*" #With quotes
vs.
对比
$ echo * #No quotes
See the difference? When you put quotes around a string, the shell can't globto replace the asterisk with the matching files.
看到不同?当你把周围一个字符串引号,shell不能glob的与匹配的文件替换星号。
There are multitude of ways to get what you want. Simplest is to use the ls
command without the test command. The if
statement takes the output results of the commandyou give it. The [...]
simply are an alias to the test
command. These two statements are equivalent:
有很多方法可以得到你想要的东西。最简单的是使用ls
命令而不使用 test 命令。该if
语句采用您给它的命令的输出结果。在[...]
简单的是别名的test
命令。这两个语句是等价的:
$ if test -f foo.txt
$ if [ -f foo.txt ]
Basically, all the test command does is return a 0
if the test is true or a non-zero if the test is false. All the if
command does is execute the if
statement if the command it's given returns a 0
exit code. The reason I'm going all through this is to say let's forget the test and simply use the ls
command.
基本上,所有测试命令所做的就是0
在测试为真时返回 a,如果测试为假则返回非零值。如果给定的命令返回退出代码,则该if
命令所做的就是执行该if
语句0
。我经历这一切的原因是说让我们忘记测试并简单地使用ls
命令。
Assume that you have a file foo.txt
, but no file bar.txt
. Try the following:
假设您有一个文件foo.txt
,但没有文件bar.txt
。请尝试以下操作:
$ ls foo.txt > /dev/null 2>&1
$ echo $?
0
$ ls bar.txt > /dev/null 2>&1
$ echo $?
2
The > /dev/null 2>&1
is used to inhibit all output. Notice that the exit code of the ls
command when a file exists is 0
while the exit code of the ls
command when the file doesn't exist isn't 0
. (It's 2
in this case). Now, let's use that instead of the test command:
将> /dev/null 2>&1
被用来抑制所有输出。请注意,ls
文件存在时命令0
的退出代码是ls
,文件不存在时命令的退出代码不是0
。(2
在这种情况下)。现在,让我们用它代替 test 命令:
if ls ./ini/* > /dev/null 2>&1
then
echo "There are files in that directory!"
else
echo "There are no files in that directory"
fi
Notice I don't even bother with the test command (which can be the word test
or the [..]
).
请注意,我什至不关心 test 命令(可以是单词test
或[..]
)。
Hope this makes sense to you. It's sometimes difficult for people to realize that in BASH shell, the if
command and the [...]
are two separate commands, and all if
is doing is running the command it's given and then acting depending upon the exit code of the command it runs.
希望这对你有意义。人们有时很难意识到,在 BASH shell 中,if
command 和 the[...]
是两个独立的命令,if
所做的只是运行它给出的命令,然后根据它运行的命令的退出代码执行操作。
回答by cdarke
Beware of filenames starting with '.', normal globbing or using ls
(shudder) will not detect those There are several ways to do this, the simplest I can come up with is:
注意以“.”开头的文件名,正常的 globbing 或 using ls
( shudder) 不会检测到那些有几种方法可以做到这一点,我能想出的最简单的是:
/home/user1> mkdir ini
/home/user1> cd ini
/home/user1/ini> touch .fred jim tom dick harry
/home/user1/ini> cd ..
/home/user1> declare -a names
/home/user1> names=(./ini/* ./ini/.*)
/home/user1> number_of_files=$(( ${#names[@]} -2 ))
/home/user1> echo $number_of_files
5
The -2
is required to remove .
and ..
from the list.
将-2
需要删除.
并..
从列表中。
回答by aurelihein
if find ./ini/ -maxdepth 0 -empty | read;
then
echo "Not Empty"
else
echo "Empty"
fi
回答by AnBisw
If the output of the command below is 0
then no files in directory (Directory empty) else it has files!
如果下面命令的输出是0
目录中没有文件(目录为空),否则它有文件!
$find "/path/to/check" -type f -exec echo {} \;|wc -l
so for you -
所以对你 -
exist=`find "./ini" -type f -exec echo {} \;|wc -l`
if [ $exist -gt 0 ]
then
echo "Directory has files"
else
echo "Directory is empty"
fi
Or
或者
if [ `find "./ini" -type f -exec echo {} \;|wc -l` -gt 0 ]
then
echo "Directory has files"
else
echo "Directory is empty"
fi
回答by Andrea Gherardi
This works for me
这对我有用
[ "$(ls -A /path/to/directory)" ]
in your case it would be
在你的情况下,它会是
[ "$(ls -A ./ini/)" ]
you can test it using
你可以使用
[ "$(ls -A /tmp)" ] && echo "Not Empty" || echo "Empty"