Mixing nounset with $ @ in ksh

I have a script that should redirect all its parameters to another program. In addition, for clarity, I support the shell option nounset.

Consider the following script example:

#!/bin/ksh
set -o nounset

# Pass all of our parameters (plus a few extra) to our target
printf "<%s>\n" "Parameter count: $#" "$@"

The above works fine as long as the main script call has at least one parameter. If it has no parameters, this results in ksh: @: parameter not set, and the script exits.

$ ./dollar_at.sh "Hello" "World"
<Parameter count: 2>
<Hello>
<World>


$ ./dollar_at.sh ""
<Parameter count: 1>
<>


$ ./dollar_at.sh
./dollar_at.sh[5]: @: parameter not set

If I disable the option nounset, we get the following:

$ ./dollar_at.sh
<Parameter count: 0>

By the way, nounsetin it bashprocesses empty $@without errors, but the environment for this script does not have bashas an option.


ksh $@?
, "$@" , , bash ksh nounset .

, :

  • if (( $# > 0 )) $@. , , , .
  • nounset , $@. nounset.
+4
2

set -o nounset, 1 : .

function forwarder {
   target=$1
   shift
   if [ $# -eq 0 ]; then
      "${target}"
   else
      "${target}" "$@"
   fi
}

set -u
forwarder ./dollar_at.sh hello world
forwarder ./dollar_at.sh hello
forwarder ./dollar_at.sh ""
forwarder ./dollar_at.sh
0
printf "${@:-}"

Colon after variable . . KSH.

0

Source: https://habr.com/ru/post/1626887/


All Articles