Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable assignment inside a case statement (bash) [duplicate]

I am trying to parse incoming options in my bash script, and save the values in variables. This is my code:

#!/bin/bash 

while getopts "H:w:c" flag
do 
#  echo $flag $OPTIND $OPTARG
    case $flag in
    H) host = "$OPTARG"
    ;;
    w) warning = "$OPTARG"
    ;;
    c) critical = "$OPTARG"
    ;;
    esac
done

However, the statements inside 'case' must be command-line commands, so I can't make the wanted assignment. What is the right way to do this?

like image 935
hizki Avatar asked Mar 04 '12 16:03

hizki


1 Answers

Remove the spaces around the = operators:

case "$flag" in
  H) host="$OPTARG" ;;
  w) warning="$OPTARG" ;;
  c) critical="$OPTARG" ;;
esac
like image 107
Adam Liss Avatar answered Sep 25 '22 19:09

Adam Liss