Shell library to validate user inputs.
bpkg install fabasoad/sh-validators
More information on installation options you can find here.
Use script by running file:
mkdir "this-dir-exists"
./fabasoad-validate-dir-exists "dir-param" "this-dir-exists"
echo $?
# Output
0
Use script by running function:
. "./validate-dir-exists"
mkdir "this-dir-exists"
fabasoad_validate_dir_exists "dir-param" "this-dir-exists"
echo $?
# Output
0
Negative use case:
./fabasoad-validate-dir-exists "dir-param" "this-dir-does-not-exist"
# Output
"dir-param" parameter is invalid. "<full-path>/this-dir-does-not-exist" is not a directory or does not exist.
If you want to redirect error message to your internal logic:
set +e
err_msg=$(./fabasoad-validate-dir-exists "dir-param" "this-dir-does-not-exist" 2>&1 >/dev/null)
if [ "$?" -ne 0 ]; then
echo "[error] $(date +%s) $err_msg"
fi
# Output
[error] 1720946407 "dir-param" parameter is invalid. "<full-path>/this-dir-does-not-exist" is not a directory or does not exist.