I am writing a bash script and I am using a for cycle to check my arguments.
for var in "$@"
do
test_arg "$var"
done
And this is my test_arg function
function test_arg {
[ -n "$1" ] || err "Empty argument"
[ -f "$1" ] || err "Argument '$1' is not a file"
[ -r "$1" ] || err "Data file '$1' is not readable"
[ -s "$1" ] || err "Data file '$1' is empty"
egrep -v '^-?([0-9]+|[0-9]*\.[0-9]+)$' "$1" && { echo "Bad data format in '$1'"; exit 1; }
}
However, when any of these conditions are not met, script only writes out "script.sh: line XX: err: command not found". I am not quite sure about the testing, I am a bash begginer.
Thank you very much for your answers
err "Empty argument"--erris not a built-in command, so unless you define it as a function, it won't be found.functionkeyword; it serves no purpose but to make your code incompatible with baseline POSIX shells. Justtest_arg() { ...}will do.-s, or with the[ortestbuiltins).functionkeyword (which I agree with Charles about), don't givedoit's own line. It serves no purpose but to make your code harder to read.for var in "$@"; do