Re: getopts appears to not be shifting $@ when consuming options

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

set -- -a foo -a bar # for testing
while getopts :a: arg; do
...

You can use typical array cryptic emulation with dash for this :)

#!/bin/dash

main() {
  local i=0 l= oIFS="$IFS" n=

  while getopts :a:u:n: arg; do
    case $arg in
     a) local l$((i=i+1))="$OPTARG"; l="${l}${l:+ }"'attr="$l'$i\";;
     n) n=$OPTARG;;
    esac
  done
  shift "$((OPTIND - 1))"
  IFS=' '; eval set -- $l '"$@"'; IFS=$oIFS

  echo '$@:' "$@"
  echo "n=$n"
}

main -a "foo 1" -a '$((ls))' -u unused -n x -n last_n_val a1 a2



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux