Add a -c' option for
check only' to determine if a value is set
(get command) or if the current value is the same as what is desired (set command). In those cases, the return status is success. Otherwise, if the variable is unset (get command) or set to a different value that what is desired (set command), returns with error status. This is useful for puppet integration as well as everyday scripting.
This commit is contained in:
parent
3e74984c3a
commit
4cc57221da
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=257827
@ -45,6 +45,7 @@ SYSRC_VERSION="6.0 Nov-07,2013"
|
|||||||
#
|
#
|
||||||
# Options
|
# Options
|
||||||
#
|
#
|
||||||
|
CHECK_ONLY=
|
||||||
DELETE=
|
DELETE=
|
||||||
DESCRIBE=
|
DESCRIBE=
|
||||||
IGNORE_UNKNOWNS=
|
IGNORE_UNKNOWNS=
|
||||||
@ -100,6 +101,8 @@ help()
|
|||||||
"Dump a list of all non-default configuration variables."
|
"Dump a list of all non-default configuration variables."
|
||||||
f_err "$optfmt" "-A" \
|
f_err "$optfmt" "-A" \
|
||||||
"Dump a list of all configuration variables (incl. defaults)."
|
"Dump a list of all configuration variables (incl. defaults)."
|
||||||
|
f_err "$optfmt" "-c" \
|
||||||
|
"Check. Return success if no changes needed, else error."
|
||||||
f_err "$optfmt" "-d" \
|
f_err "$optfmt" "-d" \
|
||||||
"Print a description of the given variable."
|
"Print a description of the given variable."
|
||||||
f_err "$optfmt" "-D" \
|
f_err "$optfmt" "-D" \
|
||||||
@ -222,10 +225,11 @@ jail_depend()
|
|||||||
#
|
#
|
||||||
# Process command-line flags
|
# Process command-line flags
|
||||||
#
|
#
|
||||||
while getopts aAdDef:Fhij:nNqR:vxX flag; do
|
while getopts aAcdDef:Fhij:nNqR:vxX flag; do
|
||||||
case "$flag" in
|
case "$flag" in
|
||||||
a) SHOW_ALL=${SHOW_ALL:-1};;
|
a) SHOW_ALL=${SHOW_ALL:-1};;
|
||||||
A) SHOW_ALL=2;;
|
A) SHOW_ALL=2;;
|
||||||
|
c) CHECK_ONLY=1;;
|
||||||
d) DESCRIBE=1;;
|
d) DESCRIBE=1;;
|
||||||
D) RC_CONFS=;;
|
D) RC_CONFS=;;
|
||||||
e) SHOW_EQUALS=1;;
|
e) SHOW_EQUALS=1;;
|
||||||
@ -286,6 +290,12 @@ if [ "$DELETE" -a "$SHOW_ALL" ]; then
|
|||||||
[ "$DELETE" = "2" ] || die "$errmsg"
|
[ "$DELETE" = "2" ] || die "$errmsg"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
#
|
||||||
|
# Pre-flight for `-c' command-line option
|
||||||
|
#
|
||||||
|
[ "$CHECK_ONLY" -a "$SHOW_ALL" ] &&
|
||||||
|
die "$pgm: \`-c' option incompatible with \`-a'/\`-A' options"
|
||||||
|
|
||||||
#
|
#
|
||||||
# Process `-e', `-n', and `-N' command-line options
|
# Process `-e', `-n', and `-N' command-line options
|
||||||
#
|
#
|
||||||
@ -312,6 +322,7 @@ if [ "$JAIL" -o "$ROOTDIR" ]; then
|
|||||||
$( [ "$DELETE" = "2" ] && echo \ -X )
|
$( [ "$DELETE" = "2" ] && echo \ -X )
|
||||||
$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
|
$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
|
||||||
$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
|
$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
|
||||||
|
${CHECK_ONLY:+-c}
|
||||||
${DESCRIBE:+-d}
|
${DESCRIBE:+-d}
|
||||||
${SHOW_EQUALS:+-e}
|
${SHOW_EQUALS:+-e}
|
||||||
${IGNORE_UNKNOWNS:+-i}
|
${IGNORE_UNKNOWNS:+-i}
|
||||||
@ -422,7 +433,7 @@ if [ "$SHOW_ALL" ]; then
|
|||||||
EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
|
EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
|
||||||
EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
|
EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
|
||||||
EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
|
EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
|
||||||
EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE"
|
EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY"
|
||||||
EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
|
EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -515,6 +526,7 @@ fi
|
|||||||
#
|
#
|
||||||
# Process command-line arguments
|
# Process command-line arguments
|
||||||
#
|
#
|
||||||
|
costatus=$SUCCESS
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
NAME="${1%%=*}"
|
NAME="${1%%=*}"
|
||||||
|
|
||||||
@ -528,7 +540,7 @@ while [ $# -gt 0 ]; do
|
|||||||
# first describe, then attempt to set
|
# first describe, then attempt to set
|
||||||
#
|
#
|
||||||
|
|
||||||
if [ "$SYSRC_VERBOSE" ]; then
|
if [ "$SYSRC_VERBOSE" -a ! "$CHECK_ONLY" ]; then
|
||||||
file=$( f_sysrc_find "$NAME" )
|
file=$( f_sysrc_find "$NAME" )
|
||||||
[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
|
[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
|
||||||
file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
|
file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
|
||||||
@ -545,6 +557,20 @@ while [ $# -gt 0 ]; do
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
#
|
||||||
|
# If `-c' is passed, simply compare and move on
|
||||||
|
#
|
||||||
|
if [ "$CHECK_ONLY" ]; then
|
||||||
|
if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
|
||||||
|
costatus=$FAILURE
|
||||||
|
else
|
||||||
|
value=$( f_sysrc_get "$NAME" )
|
||||||
|
[ "$value" = "${1#*=}" ] || costatus=$FAILURE
|
||||||
|
fi
|
||||||
|
shift 1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
#
|
#
|
||||||
# If `-N' is passed, simplify the output
|
# If `-N' is passed, simplify the output
|
||||||
#
|
#
|
||||||
@ -572,9 +598,16 @@ while [ $# -gt 0 ]; do
|
|||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
if ! IGNORED="$( f_sysrc_get "$NAME?" )"; then
|
if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
|
||||||
[ "$IGNORE_UNKNOWNS" ] \
|
[ "$IGNORE_UNKNOWNS" ] ||
|
||||||
|| echo "$pgm: unknown variable '$NAME'"
|
echo "$pgm: unknown variable '$NAME'"
|
||||||
|
shift 1
|
||||||
|
costatus=$FAILURE
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
# The above check told us what we needed for `-c'
|
||||||
|
if [ "$CHECK_ONLY" ]; then
|
||||||
shift 1
|
shift 1
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
@ -623,3 +656,5 @@ while [ $# -gt 0 ]; do
|
|||||||
esac
|
esac
|
||||||
shift 1
|
shift 1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
[ ! "$CHECK_ONLY" ] || exit $costatus
|
||||||
|
@ -32,13 +32,13 @@
|
|||||||
.Nd safely edit system rc files
|
.Nd safely edit system rc files
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl dDeFhinNqvx
|
.Op Fl cdDeFhinNqvx
|
||||||
.Op Fl f Ar file
|
.Op Fl f Ar file
|
||||||
.Op Fl j Ar jail | Fl R Ar dir
|
.Op Fl j Ar jail | Fl R Ar dir
|
||||||
.Ar name Ns Op = Ns Ar value
|
.Ar name Ns Op = Ns Ar value
|
||||||
.Ar ...
|
.Ar ...
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl dDeFhinNqvx
|
.Op Fl cdDeFhinNqvx
|
||||||
.Op Fl f Ar file
|
.Op Fl f Ar file
|
||||||
.Op Fl j Ar jail | Fl R Ar dir
|
.Op Fl j Ar jail | Fl R Ar dir
|
||||||
.Fl a | A
|
.Fl a | A
|
||||||
@ -57,6 +57,12 @@ Dump a list of all non-default configuration variables.
|
|||||||
.It Fl A
|
.It Fl A
|
||||||
Dump a list of all configuration variables
|
Dump a list of all configuration variables
|
||||||
.Pq incl. defaults .
|
.Pq incl. defaults .
|
||||||
|
.It Fl c
|
||||||
|
Check if the value will change when assigning a new value.
|
||||||
|
If verbose
|
||||||
|
.Pq see Dq Fl v
|
||||||
|
prints a message stating whether a change would occur.
|
||||||
|
Exits with success if no change is necessary, else returns error status.
|
||||||
.It Fl d
|
.It Fl d
|
||||||
Print a description of the given variable.
|
Print a description of the given variable.
|
||||||
.It Fl D
|
.It Fl D
|
||||||
|
Loading…
Reference in New Issue
Block a user