is it possible to pass conditions that are checked in if clauses (https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html) as arguments to a function? The following does not work.
print_message_if_condition_holds() {
local CONDITION="$1"
local MESSAGE="$2"
if $CONDITION; then
echo "$MESSAGE"
fi
}
print_message_if_condition_holds [ 0 -eq 0 ] "Success"
can it be done with somewhat different syntax?
[ 1 -eq 0 ] || [ 0 -eq 0 ]– matthias_buehlmann Feb 20 '21 at 23:56COND="( $a -eq $b -o $c -ne $d ) -a -n $e", then pass that string to thetestor[command such asif [ "$COND" ]. – berndbausch Feb 21 '21 at 02:03[to the function:print_message_if_condition_holds() { local msg=$1; shift; [ "$@" ] && echo "$msg"; }-- thenprint_message_if_condition_holds "Success" "$a" -eq "$b"– glenn jackman Feb 21 '21 at 18:06[and]— but it prevents you from doing things likeprint_message_if_condition_holds "Files are identical" cmp -s file1 file2. – Scott - Слава Україні Apr 01 '21 at 00:24