0

I would like to search a directory (should search only in that directory and shouldn't search in the recursive manner by searching inside the sub directories) with multiple file formats and even if anyone file format is available, I would like to execute the commands inside IF. If not, it should go to ELSE and throw the error. But, the below code is not working as expected. Please help me to fix the code. Also, I know there should be a simple way to write it in IF ELSE, but I am not sure how to do that.

cd /new_folder/dta

if [ ! -f /new_folder/dta/NY_* && ! -f /new_folder/dta/NC_* ];
then
echo -e "New Files are not available to process (or) Any one of the file/files are  missing"| mutt -s "File Process " $MAIL_TO_SUPPORT
  exit 1
fi

if [ -f /new_folder/dta/NY_* || -f /new_folder/dta/NC_* ];
then
mv /new_folder/dta/NY_* /new_folder/dta/NYFiles
mv /new_folder/dta/NC_* /new_folder/dta/NCFiles

fi
exit 0
prabu
  • 19
  • 1
  • 6
  • Globs get expanded before `[` starts, so `[ -f *.txt ]` can become `[ -f a.txt b.txt c.txt ]`, which isn't valid syntax. (`[` is just a command, it's not anything syntactically special -- there's a builtin version, but it's just a performance optimization; it behaves almost identically to when the external `/usr/bin/[` copy is used instead). – Charles Duffy Apr 01 '21 at 16:00
  • Also, `||` doesn't have special meaning in `[` for the same reason (it's just a regular command!), so `[ foo || bar ]` runs `[ foo` as one command and `bar ]` as a second, separate command. If you need special handling, you need an extended shell (like ksh but not POSIX sh) that supports `[[`; but even then, that won't provide the globbing behavior you're asking for. – Charles Duffy Apr 01 '21 at 16:01
  • Thank you! But still I am not clear. I would like to check multiple file name formats inside a single IF condition and wants to execute the commands inside IF, even if any one of the file name format matches. I am not going to check `*.txt` . It is something like NY_* , NC_* , DC_* , CM_*, etc. – prabu Apr 01 '21 at 16:05
  • Can you please help me with this? – prabu Apr 01 '21 at 16:25
  • I'd suggest defining a function. `exists() { [ -e "$1" ] || [ -L "$1" ]; }`, and then `if exists NY_* || exists NC_*; then ...` is the easiest approach that comes direct to mind. – Charles Duffy Apr 01 '21 at 18:47
  • 1
    Alternately, `any_exists() { local status; status=1; for arg in "$@"; do if [ -e "$arg" ] || [ -L "$arg" ]; then status=0; break; fi; done; return "$status"; }`, and then `if any_exists {NY,NC,DC,CM}_*; then ...` – Charles Duffy Apr 01 '21 at 18:49
  • ...in the linked duplicate, the answer https://stackoverflow.com/a/51401971/14122 describes an equivalent to the practice in the above comment. – Charles Duffy Apr 01 '21 at 18:50
  • (the difference between `exists` and `any_exists` is that the former assumes only one glob is being expanded at a time unless the bash `nullglob` option or whatever ksh's equivalent to it is enabled). – Charles Duffy Apr 01 '21 at 18:57
  • Thank you @CharlesDuffy ! What does -e and -L perform? – prabu Apr 02 '21 at 18:28
  • `test -e` checks if something exists in general, but it counts a broken symlink as not existing. `test -L` returns true if something exists as a symlink, even if it's a broken one. (`[` is a synonym for `test`; typically `/usr/bin/[` and `/usr/bin/test` are provided by the exact same executable, and it only looks at the name it was called with to determine whether it expects `]` as a final argument) – Charles Duffy Apr 02 '21 at 18:32
  • Many thanks! It worked perfectly. – prabu Apr 02 '21 at 20:17

0 Answers0