2

I'm calling a function and I want to pass up to 100 paramters onto another function. I do not want to pass on the first 3 params, I start with param4 being the first param for the other program.

I am currently allowing for passing on up to 19 additional with

$function_under_test "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" 
  "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}"

but this is not very substanable for larger sets of params.

I tried

  declare -a pass_on_params
  for ((a=2; a<$#; a++)); do
    pass_on_params+=(${@[a]})  # line 8
  done
  echo "->" $pass_on_params

but I get

do_test.sh: line 8: ${@[a]}: bad substitution

Full code is:

do_test () {
  function_under_test=$1
  line_number=$2
  expected="$3"
  param1="$4"
  declare -a pass_on_params
  for ((a=2; a<$#; a++)); do
    pass_on_params+=(${@[a]})
  done
  echo "ppppppppp" $pass_on_params
  $function_under_test "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}"
  if [ $result -eq $expected ]; then
    printf '.'
  else
    printf 'F'
    error_messages=$error_messages"Call to '$function_under_test $param1' failed: $result was not equal to $expected at line $line_number\n"
  fi
}

Shell is bash

6

"${@:4}" works for me in bash. You can also assign to another array and do indexing on it:

foo=("$@")
second_function "${foo[@]:4}"
3

Just use another function:

fn(){
    pass(){
        shift "$shift"
        "$other" "$@"
    }
    other=fn2 shift=3 pass "$@"
    unset -f pass
}

It would be better, though, to iterate over the args in an organized way. Like...

while [ "$#" -gt 0 ]
do    : something with "$1"
shift;done

..or..

for arg do : something with "$arg"; done

...but it's hard to tell what you're doing.

You can safely evaluate math expressions into positional parameters if you handle the quoting right:

eval "fn2 $(i=3
    until [ "$#" -le 3 ]
    do    printf '"${%d}" ' "$((i+=1))"
    shift;done
)"
2

You want to shift three times and pass $@

Full code is:

do_test () {
  function_under_test=$1;shift
  line_number=$1;shift
  expected="$1";shift
  echo "ppppppppp" $@
  $function_under_test "$@"
  if [ $result -eq $expected ]; then
    printf '.'
  else
    printf 'F'
    error_messages=$error_messages"Call to '$function_under_test $param1' failed: $result was not equal to $expected at line $line_number\n"
  fi
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.