2

I would like to create a script on SUSE Enterprise Linux, it first asks for a string (representing a date) and then search for all files in a certain directory which contains the string in its filename. If there is an empty input, todays date should be used.

I now have this:

read -p "Send files from date (MM-DD-YYYY) : " fromdate
if [[ -z ${fromdate// } ]]
then
    echo "Empty input"
    fromdate=$(date +%m-%d-%Y)
fi
echo "Input: $fromdate"
cd /path/to/directory
while IFS= read -r -d '' file ; do
echo "$file"
done < <(find . -maxdepth 1 -name "*$fromdate*" -print0)

When I enter a date, it works fine, for empty input, I get an error:

fromdate: command not found

The input is still empty, and all files are displayed. What could be my mistake? Should I use the 'let'-command?

  • Why not just use find directly? – Wildcard Nov 18 '15 at 15:09
  • I assume that error is coming from line 5? Can you comment it out and re run and confirm the error goes away? My guess is you have a space between fromdate and =. – Mikel Nov 18 '15 at 15:16
  • I would also suggest trimming the spaces separately. You seem to check for spaces, but use the string including spaces if it was non-empty? – Mikel Nov 18 '15 at 15:18
  • Did you perhaps edit this script on a Windows box, then copy it over to a Linux or Unix box without converting EOLs? – ghoti Nov 18 '15 at 16:17
1

This works for me (but on CentOS 7.1). Did I miss something?

read -p "Send files from date (MM-DD-YYYY) : " fromdate
if [[ -z ${fromdate// } ]]
then
    echo "Empty input"
    fromdate=$(date +%m-%d-%Y)
fi
echo "Input: $fromdate"

find /path/to/directory -maxdepth 1 -name "*$fromdate*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.