Skip to content

Instantly share code, notes, and snippets.

@rickwillcox
Last active August 25, 2022 12:33
Show Gist options
  • Save rickwillcox/605685dd563dca96938a4d6b980ce411 to your computer and use it in GitHub Desktop.
Save rickwillcox/605685dd563dca96938a4d6b980ce411 to your computer and use it in GitHub Desktop.
A script that can watch a .c file for changes and if it detects a change it will recompile and run it again. Modified from a Stackoverflow answer.
#!/bin/bash
read -p 'Which c file to watch?: ' file_name
read -p 'Which gcc flags? Type them all with a space between: ' opt_flags
old_file_sig=$(stat -c %Z $file_name)
echo "Watching $file_name for changes..."
while true
do
new_file_sig=$(stat -c %Z $file_name)
if [[ "$new_file_sig" != "$old_file_sig" ]]; then
echo ""
echo "$file_name changed... compiling and running"
echo ""
gcc $file_name -O $opt_flags -o $file_name.o && ./$file_name.o
old_file_sig=$new_file_sig
echo ""
echo "Watching $file_name for changes..."
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment