linux - Check if directory exists not working -
i have textfile (qrs.txt) contains dir names (one per line) , on server in same directory script have folders corresponding names text file.
this script:
#!/bin/bash while read p; if [ ! -d "$p" ]; echo "error $p" >> log.txt else echo "good" >> log.txt fi done < qrs.txt
qrs.txt:
1992300000183805 1992300001176204 1992300002145500 1992300003104507 1992300004104902 1992300005133703 1992300006117802 1992300007144501 1992300008172803 1992300009189005 1992300010146307 1992300011151700 1992300012190007 1992300013126802 1992300014111508 1992300015193908
when if statement inside loop returns error incorrect because can see folders exist. when take out of loop , check 1, works fine... when echo $p on same line error, can see file name checking indeed correct.
what missing here..?
edit:
screenshot of qrs.txt in hex mode:
resolved!
my qrs.txt in [dos] format once converted unix format using ":set ff=unix" script worked charm!
your script works fine.
i copied script local machine. when put blh blah
in qrs.txt
file, got error
each time ran script. ran 4 times. changed blh blah
valid path , received good
.
the directory 1992300000183805
instance, may not valid path. need qualified path name! example, /home/user/1992300000183805
.
error blh blah error blh blah good
edit
looking @ @chepner comments, recreated problem:
open qrs.txt file in vi
or vim
. should see ^m @ end of lines. remove ^m characters @ end of lines in vi, use:
:%s/^m//g
this should fix problem. if not, in vim
type this:
:set ff=unix
save file.
re-open qrs.txt
in vim
, run regex above again, or manually delete ^m
.
or can use perl
:
perl -pi -e "s/\r/\n/g;" <file>
Comments
Post a Comment