Pieters bash scripts
Documentation for the bash scripts I have published
Loading...
Searching...
No Matches
run_on_save.sh
Go to the documentation of this file.
1#!/bin/bash
2##! Author: Pieter van der Star (info@pietervanderstar.nl)
3##! Modifications by: (unmodified)
4##!
5##! This program is free software: you can redistribute it and/or modify
6##! it under the terms of the GNU Affero General Public License as
7##! published by the Free Software Foundation, either version 3 of the
8##! License, or (at your option) any later version.
9##!
10##! This program is distributed in the hope that it will be useful,
11##! but WITHOUT ANY WARRANTY; without even the implied warranty of
12##! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13##! GNU Affero General Public License for more details.
14##!
15##! You should have received a copy of the GNU Affero General Public License
16##! along with this program. If not, see <https://www.gnu.org/licenses/>.
17#
18#
19#Changelog:
20#┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┓
21#┃ 11 jun 2022 │ First release │ Pieter van der Star ┃
22#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
23#┃ 24 feb 2024 │ Changed documentation comments to be │ Pieter van der Star ┃
24#┃ │ compatible with my doxygen-bash-filter. │ ┃
25#┠───────────────┼──────────────────────────────────────────┼──────────────────────────┨
26#┃ 9 may 2024 │ Readded spaces between the arguments. │ Pieter van der Star ┃
27#┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┛
28
29##! @file
30##! @brief This script can be used to automatically run a command as soon as a file is updated/saved.
31echo "When waiting press: q to quit";
32echo " r to rerun";
33echo " f to show the file to wait on";
34echo " c to show the command to run";
35
36if [ $# -le 0 ]; then
37 echo "Please enter a file to wait on as first argument.";
38 exit;
39fi
40if [ $# -le 1 ]; then
41 echo "Please enter bash script to execute as second argument.";
42 exit;
43fi
44#get the arguments, first the file
45##! Name of the file to use as trigger.
46file="$1";
47shift;
48#then the command
49##! The command to execute on file modifications.
50command="$1";
51shift;
52#string together all other arguments to make and run, these are the arguments
53#to the program given in command
54##! Arguments to pass to the command.
55arguments="";
56for arg in "$@"; do
57 arguments+="$arg ";
58done
59
60echo "arguments: $arguments";
61##! Last modified date when we previously ran the command.
62prevlastmod=0;
63
64while [ 1 -eq 1 ]; do #using [ 1 -eq 1 ] instead of true for wider compatibility
65 lastmod=$(date -r "$file" +%s);
66 if [ $? -ne 0 ]; then
67 echo "file not found";
68 else
69 if [ "$lastmod" -gt "$prevlastmod" ]; then
70 eval "$command $arguments";
71 prevlastmod=$lastmod;
72 echo "---------------------------RUN_ON_SAVE---------------------------";
73 fi
74 fi
75 read -t 1 -n 1 key;
76 if [ "$key" == 'q' ]; then
77 echo "";
78 exit;
79 fi
80 if [ "$key" == 'r' ]; then
81 prevlastmod=0;
82 echo "";
83 fi
84 if [ "$key" == 'f' ]; then
85 echo;
86 echo "waiting on update of $file";
87 fi
88 if [ "$key" == 'c' ]; then
89 echo;
90 echo "after update run: \"$command $arguments\"";
91 fi
92done
if(func_num_args() -le 0) if(func_num_args() -le 1) $file
Name of the file to use as trigger.
$arguments
Arguments to pass to the command.
$prevlastmod
Last modified date when we previously ran the command.
$command
The command to execute on file modifications.