Skip to content

Instantly share code, notes, and snippets.

@pasdam
Last active January 2, 2026 15:56
Show Gist options
  • Select an option

  • Save pasdam/57fe4cdb3a28da591963c0baad31bc7d to your computer and use it in GitHub Desktop.

Select an option

Save pasdam/57fe4cdb3a28da591963c0baad31bc7d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_usage() {
SCRIPT_NAME=$(basename "$0")
echo "Description"
echo ""
echo "Usage: $SCRIPT_NAME <path1> [path2 ...] -- <command>"
echo "Example: $SCRIPT_NAME src/ lib/ -- npm test"
echo "Parameters:"
echo " -h, --help Show this help message"
exit "$1"
}
# Check if we have at least 3 arguments (path -- command)
if [ $# -lt 3 ]; then
print_usage 1
fi
COMMAND=""
PARSING_PATHS=true
PATHS=()
for arg in "$@"; do
if [ "$arg" = "--" ]; then
PARSING_PATHS=false
continue
fi
if [ "$PARSING_PATHS" = true ]; then
PATHS+=("$arg")
else
if [ -z "$COMMAND" ]; then
COMMAND="$arg"
else
COMMAND="$COMMAND $arg"
fi
fi
done
# Validate that we have paths and a command
if [ ${#PATHS[@]} -eq 0 ]; then
echo -e "${RED}Error: No paths specified${NC}"
print_usage 2
fi
if [ -z "$COMMAND" ]; then
echo -e "${RED}Error: No command specified${NC}"
print_usage 3
fi
# Verify paths exist
for path in "${PATHS[@]}"; do
if [ ! -e "$path" ]; then
echo -e "${RED}Error: Path does not exist: $path${NC}"
exit 1
fi
done
trap printout SIGINT
printout() {
echo ""
1>&2 echo "Watch path terminated forcefully"
exit
}
eval "${COMMAND}"
while true
do
printf "\n\n\n-------------------\n\n\n"
watch -d -t -g ls -lRT ${PATHS[@]} > /dev/null && RES=$? && eval "${COMMAND}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment