Line numbers

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
#!/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