1

Overview:

I am trying to code an error for a function that requires multiple arguments.

  • example:

    myfunction arg1 arg2 arg3 arg4

If any of these arguments are null I want to trow the user a syntax error.

Attempts:

Without writing the individual arguments I tried:

# Error Checking
for i in "{1.4}"
do
    # Check for null values of Bash variables:
    [[ -z $"$i" ]] && {
        printf "Syntax Error: Missing Operator see:\nmyfunction --help\n"
        exit
        }
done

-and-

# Error Checking
for i in ${1..4}
do
    # Check for null values of Bash variables:
    [[ -z $i ]] && {
        printf "Syntax Error: Missing Operator see:\nmyfunction --help\n"
        exit
        }
done

Attempted syntax check:

enter image description here enter image description here

Question:

Is what I am trying to do possible with a for loop?

Any help would be much appreciated.

Thanks in advance!

Edit:

Also tried the following before I started messing with quotes and dollar signs:

# Error Checking
for i in {1..4}
do
    [[ -z $"$i" ]] && {
        printf "Syntax Error: Missing Operator see:\nansi-color --help\n"
        exit
        }
done
Robert J
  • 300
  • 1
  • 3
  • 15
  • Yes I tried that iteration, which is the correct iteration looking at how it works now and still no dice. – Robert J Feb 12 '16 at 23:23
  • `for i` it defaults to iterate through the parameter list. – cdarke Feb 12 '16 at 23:27
  • 1
    Do you just need to check that there is number of arguments? – Caleb Adams Feb 12 '16 at 23:27
  • @CalebAdams yes if there is an error later I direct them to --help – Robert J Feb 12 '16 at 23:29
  • 1
    @RobertJ try reading this post: [checking-number-of-arguments-bash-script](http://stackoverflow.com/questions/18568706/checking-number-of-arguments-bash-script). This post may also be helpful [how-to-check-if-there-are-no-parameters-provided-to-a-command](http://unix.stackexchange.com/questions/25945/how-to-check-if-there-are-no-parameters-provided-to-a-command). – Caleb Adams Feb 12 '16 at 23:35

3 Answers3

2

There are several ways to tackle this:

die () { echo "$@" >&2; exit 1; }

myfn () {
        [[ $# == 4 ]] || die 'wrong number of args'

        for d in "$@"; do [[ $d ]] || die 'null arg'; done
}

myfn2 () {
        [[ $# == 4 ]] || die 'wrong number of args'

        for ((i = 1; i <= $#; i++)); do
                [[ ${!i} ]] || die 'null arg'
        done
}

The "$@" is probably more idiomatic. The indirection with ! is very handy though not common.

dan4thewin
  • 1,134
  • 7
  • 10
0

Caleb Adams pointed out that I could try something like this:

# Error Checking
[[ $# != 4 ]] && {
    printf "Syntax Error: Missing Operator see:\n\tansi-color --help\n"
    exit
    }
Robert J
  • 300
  • 1
  • 3
  • 15
0

Personally, I'd use a bash array and then check its length:

  args=($*)
  echo ${#args[*]}

But you can do this:

myfunc() { for i in {1..4}; do if [[ -z ${!i} ]]; then echo error at $i return -1 fi done ...do other stuff }

nortally
  • 347
  • 2
  • 9
  • The array won't work if some of the arguments contain whitespace. Consider `myfunction "a b" c "" d`. The third argument is null, but your array has 4 elements. – chepner Feb 13 '16 at 02:01
  • Point taken. More embarrassingly, the Caleb Adams suggestion to use the $# operator obviates the need for the array. The whitespace issue is handled by the for loop, but not null arguments. For that, the best suggestion I've seen uses 'parameter expansion for an alternate value' – nortally Feb 18 '16 at 17:35