I am writing a Bash script that searches for a file matching a set pattern in a directory and when found, carries out additional processing.
#!/bin/sh
set -eu
INTERVAL=5
DIRECTORY=/home/methuselah
PATTERN=(MT*xx.txt)
while :
do
if [[ -e "$DIRECTORY/$PATTERN" ]]; then
echo "File has arrived. Starting processing..."
# Not sure about this!
for file in "$DIRECTORY/$PATTERN"; do
echo "foo" > "$file"
done
fi
echo "Waiting for file..."
sleep $INTERVAL
done
Right now the above script does nothing, even when the file gets dropped in the directory. How can I fix it? And secondly when a file has arrived in the directory, is it possible for me to displa the name?
Update
#!/bin/bash
set -u
INTERVAL=5
DIRECTORY=/home/methuselah
PATTERN=(MT*xx.txt)
while :
do
if [[ -e $DIRECTORY/$PATTERN ]]; then
echo "File has arrived. Starting processing..."
# How do you print the file name out here?
fi
echo "Waiting for file..."
sleep $INTERVAL
done