[英]Check if directory exists not working
I have a textfile (qrs.txt) which contains dir names (one per line) and on my server in the same directory as the script I have those folders with corresponding names from the text file. 我有一个文本文件(qrs.txt),其中包含目录名称(每行一个),并且在我的服务器上与脚本位于同一目录中,我有那些文件夹以及来自文本文件的相应名称。
This is my script: 这是我的脚本:
#!/bin/bash
while read p; do
if [ ! -d "$p" ];
then
echo "ERROR $p" >> log.txt
else
echo "GOOD" >> log.txt
fi
done < qrs.txt
qrs.txt: qrs.txt:
1992300000183805
1992300001176204
1992300002145500
1992300003104507
1992300004104902
1992300005133703
1992300006117802
1992300007144501
1992300008172803
1992300009189005
1992300010146307
1992300011151700
1992300012190007
1992300013126802
1992300014111508
1992300015193908
When that if statement is inside the loop it always returns error which is incorrect because I can see the folders exist. 当该if语句在循环内时,它总是返回错误,这是不正确的,因为我可以看到文件夹存在。 When I take it out of the loop and check for just 1, it works fine... When I echo $p on the same line as error, I can see the file name its checking is indeed correct.
当我将其从循环中取出并仅检查1时,它可以正常工作...当我在错误的同一行上回显$ p时,我可以看到其检查的文件名确实正确。
What am I missing here..? 我在这里想念什么..?
EDIT: 编辑:
Screenshot of qrs.txt in hex mode: 十六进制模式下qrs.txt的屏幕截图:
http://i.snag.gy/25mqJ.jpg http://i.snag.gy/25mqJ.jpg
RESOLVED! 解决!
My qrs.txt was in [dos] format originally but once converted to unix format using ":set ff=unix" the script worked like a charm! 我的qrs.txt最初是[dos]格式,但是一旦使用“:set ff = unix”转换为unix格式,该脚本就可以发挥作用了!
Your script works fine. 您的脚本工作正常。
I copied your script to my local machine. 我将您的脚本复制到了本地计算机上。 When I put
blh blah
in the qrs.txt
file, I got ERROR
for each time I ran your script. 当我在
qrs.txt
文件中添加blh blah
时,每次运行您的脚本时都会ERROR
。 I ran it four times. 我跑了四遍。 I changed the
blh blah
to a valid path and I received GOOD
. 我将
blh blah
更改为有效路径,并收到GOOD
。
The directory 1992300000183805
for instance, may be not be a valid path. 例如,目录
1992300000183805
可能不是有效路径。 You need the fully qualified path name! 您需要完全限定的路径名! For example,
/home/user/1992300000183805
. 例如,
/home/user/1992300000183805
。
ERROR blh blah
ERROR blh blah
GOOD
GOOD
EDIT 编辑
Looking at @chepner comments, I recreated your problem: 查看@chepner的评论,我重新创建了您的问题:
Open your qrs.txt file in vi
or vim
. 在
vi
或vim
打开您的qrs.txt文件。 You should see ^M at the end of your lines. 您应该在行尾看到^ M。 To remove the ^M characters at the end of all lines in vi, use:
要删除vi中所有行末的^ M字符,请使用:
:%s/^M//g
This should fix your problem. 这应该可以解决您的问题。 If not, in
vim
type this: 如果没有,在
vim
输入:
:set ff=unix
save the file. 保存文件。
Re-open qrs.txt
in vim
, then run the regex above again, or manually delete the ^M
. 在
vim
重新打开qrs.txt
,然后再次运行上述正则表达式,或手动删除^M
Or you can use perl
: 或者您可以使用
perl
:
perl -pi -e "s/\\r/\\n/g;" <file>
OK so looking at your provided file it seems those are relative directory names -- as such current directory is very important when you execute the script. 好的,因此查看您提供的文件似乎是相对目录名称-因为执行脚本时当前目录非常重要。 Do you execute the script from its own directory or from the parent directory to all the (sub)directories shown in your example?
您是从脚本自己的目录还是从父目录到示例中显示的所有(子)目录执行脚本? In other words have you tried:
换句话说,您是否尝试过:
cd <parent directory>
/path/to/yourscript.sh
? ?
Not to mention the location of qrs.txt seems to be specified relative rather than absolute path. 更不用说qrs.txt的位置似乎是相对的,而不是绝对路径。 So if there's no qrs.txt in the current directory I don't think your script would work.
因此,如果当前目录中没有qrs.txt,则我认为您的脚本无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.