Skip to content

Instantly share code, notes, and snippets.

@efrecon
Created February 22, 2018 07:17
Show Gist options
  • Save efrecon/a93124bc1ac9968420cd4b17b08ef521 to your computer and use it in GitHub Desktop.
Save efrecon/a93124bc1ac9968420cd4b17b08ef521 to your computer and use it in GitHub Desktop.
Wait for a number of files to be present (including 0, the use case!) and start a command
#!/bin/sh
# Number of files to wait for, 0 is usually what you want
TARGET=$1
# Pattern to match path
PATTERN=$2
# Command to execute once target has been reached
COMMAND=$3
while true; do
NFILES=$(ls -1 ${PATTERN}|wc -l)
WHEN=$(date +'%Y%m%d %H:%M:%S')
echo "[${WHEN}] $NFILES file(s) matching $PATTERN"
if [ "$NFILES" -eq "$TARGET" ]; then
exec $3
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment