0

I've seen several posts on very similar queries, but here is my use case:

A shell script (ksh in my case) which has a step to check a dir for files ending in .txt or .TXT and assign a variable, INPUT_FILE, if present, for processing further in the script. If these file types are not present, exit out of whole job (exit 0).

This was working for when a) files were present, b) ignoring directories, c) ignoring zero byte files, but when the dir was empty, it was still setting the INPUT_FILE to a null value and failing. What am I missing?

code

change to some /dir

if [ -s `ls -pt | grep -v / | grep -i .txt` ]; 
    then
     INPUT_FILE=`ls -pt | grep -v / | head -1` 
     echo "${INPUT_FILE} exists. Moving on to import" 
else
    echo "No file to import. Exiting job."
    exit 0
fi

error snippet showing echo with "null" value

exists. Moving on to import

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
sirabhorn
  • 33
  • 3
  • What you're asking about is [BashFAQ #4](http://mywiki.wooledge.org/BashFAQ/004). – Charles Duffy Mar 16 '20 at 19:13
  • On the linked SO duplicative question, one of my favorite answers is https://stackoverflow.com/a/15515152/14122 -- yes, it has a corner case (when a file exists with a name matching *the glob expression itself*), but that's relatively minor. – Charles Duffy Mar 16 '20 at 19:15

0 Answers0