From 0921c9d28c7abfb64c0225ce29831939bd3966d0 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 3 May 2023 17:07:12 -0300 Subject: bin/ifnew: Add -v flag --- bin/ifnew | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bin/ifnew b/bin/ifnew index 2a72922..f4a4120 100755 --- a/bin/ifnew +++ b/bin/ifnew @@ -4,7 +4,7 @@ set -eu usage() { cat <<-'EOF' Usage: - ifnew FILE + ifnew [-v] FILE ifnew -h EOF } @@ -14,13 +14,15 @@ help() { Options: + -v notify when new -h, --help show this message FILE the target to compared against Compare STDIN to FILE, and only write to FILE if STDIN content - differs from FILE content. + differs from FILE content. When -v is given, notify on STDERR + that FILE has new content (useful for debugging purposes). Examples: @@ -41,10 +43,11 @@ help() { # timestamp 2 - Only change file when content changes in a Makefile: + Only change file when content changes in a Makefile, and print + to STDERR when it does: i18.cfg: ALWAYS - gen-cfg | ifnew $@ + gen-cfg | ifnew -v $@ po4a $@ EOF } @@ -65,8 +68,12 @@ for flag in "$@"; do esac done -while getopts 'h' flag; do +VERBOSE=false +while getopts 'vh' flag; do case "$flag" in + v) + VERBOSE=true + ;; h) usage help @@ -89,5 +96,12 @@ trap 'rm -f "$STDIN"' EXIT cat - > "$STDIN" if ! cmp -s "$STDIN" "$FILE"; then + if [ "$VERBOSE" = true ]; then + printf 'ifnew: "%s" will be updated.\n' "$FILE" >&2 + fi mv "$STDIN" "$FILE" +else + if [ "$VERBOSE" = true ]; then + printf 'ifnew: "%s" left unchanged.\n' "$FILE" >&2 + fi fi -- cgit v1.2.3