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
Charles Duffy
299k43 gold badges437 silver badges494 bronze badges
asked May 8, 2016 at 16:44
1 Answer 1
Your code depends on a function named err
. Consider defining it like so:
err() { echo "$*" >&2; exit 1; }
answered May 8, 2016 at 16:46
-
yes - now it works like a charm - thank you very much sir - will accept as an answer when possibleJesse_Pinkman– Jesse_Pinkman2016年05月08日 16:50:58 +00:00Commented May 8, 2016 at 16:50
lang-bash
err "Empty argument"
--err
is not a built-in command, so unless you define it as a function, it won't be found.function
keyword; it serves no purpose but to make your code incompatible with baseline POSIX shells. Justtest_arg() { ...}
will do.-s
, or with the[
ortest
builtins).function
keyword (which I agree with Charles about), don't givedo
it's own line. It serves no purpose but to make your code harder to read.for var in "$@"; do