HackerTrans
TopNewTrendsCommentsPastAskShowJobs

databasher

no profile record

comments

databasher
·il y a 2 ans·discuss
With lines.txt:

  printf '(%s)\n' "one two" "three four five"
  echo "How you doin'?"
Running:

  while read -r line; do
    eval command=($line)
    command+=("arg with spaces" "other arg")
    "${command[@]}"
  done < lines.txt
Yields

  (one two)
  (three four five)
  (arg with spaces)
  (other arg)
  How you doin'? arg with spaces other arg
databasher
·il y a 2 ans·discuss
Bash handles spaces just fine.

  # store the command in an array
  X=(echo "A B")
  # execute the command
  "${X[@]}"
Yields:

  A B
databasher
·il y a 2 ans·discuss
Meanwhile, UPS's web site has mostly gone down. Their home page now reads "Sorry! We Can’t Find That Page. Looks like the page you’re trying to find may have been moved or deleted."
databasher
·il y a 3 ans·discuss
The video in the linked page says that getopts only supports short options.
databasher
·il y a 3 ans·discuss
If you refer to my example above, you'll find that Bash's native "getopts" handles long options just fine. It accepts short option `-`, with an argument. This handles --long-options and --long-options=with-arguments.

Feel free to use your own formatting preferences.
databasher
·il y a 3 ans·discuss
Handling command-line arguments in Bash is easy. Bash's `getopts` handles short and long arguments gnu-style without any problem, out of the box, without any need for libraries or complicated packages.

This pattern handles lots of styles of options: short and long options (-h, --help), `--` for separating options from positional args, with GNU-style long options (--output-file=$filename).

  while getopts :o:h-: option
  do case $option in
         h ) print_help;;
         o ) output_file=$OPTARG;;
         - ) case $OPTARG in
                 help ) print_help;;
                 output-file=* ) output_file=${OPTARG##*=};;
                 * ) echo "bad option $OPTARG" >&2; exit 1;;
             esac;;
         '?' ) echo "unknown option: $OPTARG" >&2; exit 1;;
         : ) echo "option missing argument: $OPTARG" >&2; exit 1;;
         * ) echo "bad state in getopts" >&2; exit 1;;
     esac
  done
  shift $((OPTIND-1))
  (( $# > 0 )) && printf 'remaining arg: %s\n' "$@"