#!/bin/bash ##! Author: Pieter van der Star (info@pietervanderstar.nl) ##! Modifications by: (unmodified) ##! ##! This program is free software: you can redistribute it and/or modify ##! it under the terms of the GNU Affero General Public License as ##! published by the Free Software Foundation, either version 3 of the ##! License, or (at your option) any later version. ##! ##! This program is distributed in the hope that it will be useful, ##! but WITHOUT ANY WARRANTY; without even the implied warranty of ##! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##! GNU Affero General Public License for more details. ##! ##! You should have received a copy of the GNU Affero General Public License ##! along with this program. If not, see <https://www.gnu.org/licenses/>. # # #Changelog: #┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓ #┃ 11 jun 2022 │ First release │ Pieter van der Star ┃ #┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨ #┃ 24 feb 2024 │ Changed documentation comments to be │ Pieter van der Star ┃ #┃ │ compatible with my doxygen-bash-filter. │ ┃ #┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨ #┃ 9 may 2024 │ Readded spaces between the arguments. │ Pieter van der Star ┃ #┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛
##! @file ##! @brief This script can be used to automatically run a command as soon as a file is updated/saved. echo"When waiting press: q to quit"; echo" r to rerun"; echo" f to show the file to wait on"; echo" c to show the command to run";
if [ $# -le 0 ]; then echo"Please enter a file to wait on as first argument."; exit; fi if [ $# -le 1 ]; then echo"Please enter bash script to execute as second argument."; exit; fi #get the arguments, first the file ##! Name of the file to use as trigger.
file="$1"; shift; #then the command ##! The command to execute on file modifications.
command="$1"; shift; #string together all other arguments to make and run, these are the arguments #to the program given in command ##! Arguments to pass to the command.
arguments="";
for arg in "$@"; do
arguments+="$arg "; done
echo"arguments: $arguments"; ##! Last modified date when we previously ran the command. prevlastmod=0;
while [ 1 -eq 1 ]; do#using [ 1 -eq 1 ] instead of true for wider compatibility lastmod=$(date -r "$file" +%s); if [ $? -ne 0 ]; then echo"file not found"; else if [ "$lastmod"-gt"$prevlastmod" ]; then eval"$command $arguments"; prevlastmod=$lastmod; echo"---------------------------RUN_ON_SAVE---------------------------"; fi fi read -t 1 -n 1 key; if [ "$key"== 'q' ]; then echo""; exit; fi if [ "$key"== 'r' ]; then prevlastmod=0; echo""; fi if [ "$key"== 'f' ]; then echo; echo"waiting on update of $file"; fi if [ "$key"== 'c' ]; then echo; echo"after update run: \"$command $arguments\""; fi done