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?

4
  • Why not just use find directly? Commented Nov 18, 2015 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 =. Commented Nov 18, 2015 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? Commented Nov 18, 2015 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? Commented Nov 18, 2015 at 16:17

1 Answer 1

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*"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.