Jesse_Pinkman Jesse_Pinkman - 7 months ago 24
Bash Question

Bash script - "err: command not found"?

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

Answer

Your code depends on a function named err. Consider defining it like so:

err() { echo "$*" >&2; exit 1; }