1

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
4
  • 2
    err "Empty argument" -- err is not a built-in command, so unless you define it as a function, it won't be found. Commented May 8, 2016 at 16:46
  • 3
    By the way, don't use the function keyword; it serves no purpose but to make your code incompatible with baseline POSIX shells. Just test_arg() { ...} will do. Commented May 8, 2016 at 16:47
  • (...anyhow, this has nothing to do with -s, or with the [ or test builtins). Commented May 8, 2016 at 16:47
  • If we are going to point out the function keyword (which I agree with Charles about), don't give do it's own line. It serves no purpose but to make your code harder to read. for var in "$@"; do Commented May 8, 2016 at 17:32

1 Answer 1

2

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
1
  • yes - now it works like a charm - thank you very much sir - will accept as an answer when possible Commented May 8, 2016 at 16:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.