1

I try to make a script and first of all I want to get parameters. I am using getopts, but I can't make it work :-(

#!/bin/bash

# evaluate entry args
# make args an array, not a string
args=( )

# replace long arguments
for arg; do
    case "$arg" in
        --dir)           args+=( -d ) ;;
        --quality)       args+=( -q ) ;;
        --size)          args+=( -s ) ;;
        --time)          args+=( -t ) ;;
        --help)          args+=( -h ) ;;
        *)               args+=( "$arg" ) ;;
    esac
done
printf 'args before update : '; printf '%q ' "$@"; echo
set -- "${args[@]}"
printf 'args after update  : '; printf '%q ' "$@"; echo

# init_variables

while getopts "d:q:s:t:h" OPTION; do
    : "$OPTION" "$OPTARG"
    echo "optarg : $OPTARG"
    case $OPTION in
    h)
      #usage
      exit 0
      ;;
    d)
      shift
      DIR=("$OPTARG")
      ;;
    q)
      shift
      QUALITY=("$OPTARG")
      ;;
    s)
      shift
      SIZE=("$OPTARG")
      ;;
    t)
      shift
      TIME=("$OPTARG")
      ;;      
    esac
done

echo "TIME=$TIME"
echo "DIR=$DIR"

If I execute it I only get first parameter (d or t, depending on order). What do I miss?

./script.sh  -d dir -t 10
args before update : -d dir -t 10 
args after update  : -d dir -t 10 
optarg : dir
TIME=
DIR=dir
user2670996
  • 2,654
  • 6
  • 29
  • 45

0 Answers0