Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle shell getopts with parameter containing blank spaces

Tags:

shell

getopts

I'm looking for a way to handle arguments containing blank spaces that has to be parsed by shell getopts command.

while getopts ":a:i:o:e:v:u:" arg
  do
  echo "ARG is: $arg" >> /tmp/submit.log
  case "$arg" in
  a) arg1="$OPTARG" ;;
  i) arg2="$OPTARG" ;;
  o) arg3="$OPTARG" ;;
  ...
  u) argn="$OPTARG" ;;
  -) break ;;
  \?) ;;
  *) echo "unhandled option $arg" >> /tmp/submit.log ;;
  ?) echo $usage_string
     exit 1 ;;
  esac
done

Now if -u has argument like "STRING WITH WHITE SPACE" than just the first part of the string is triggered and the while loop doesn't go to the end.

many thanks.

like image 335
DrFalk3n Avatar asked May 16 '11 14:05

DrFalk3n


1 Answers

a trap for young players (ie me!)

beware a line like this:

main $@

what you really need is:

main "$@"

otherwise getopts will mince up your options into little pieces

http://www.unix.com/shell-programming-scripting/70630-getopts-list-argument.html

like image 150
ErichBSchulz Avatar answered Oct 04 '22 06:10

ErichBSchulz