Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple commands in a case?

Tags:

bash

Is it possible to do this:

 case $ans1_1 in
     y)fedoraDeps;;
      echo "something here";;
      make -j 32;;
     n)echo "Continuing"...;;
      ;;
     *) echo "Answer 'y' or 'n'";;
  esac

Where fedoraDeps is a function with yum commands.

I'm trying to replicate this with cases:

if [[ $ans1_1 = y ]]; then
    fedoraDeps
    echo "something here"
    make -j 32
elif [[ $ans1_1 = n ]]; then
      echo "Continuing..."
      :
else
    echo "Answer 'y' or 'n'"
fi
like image 674
dominique120 Avatar asked Jan 25 '14 01:01

dominique120


2 Answers

; or line feed is used to end a command. ;; is used to end the case branch. Just don't try to end the case branch after every command, and it's fine:

case $ans1_1 in
    y)
      fedoraDeps
      echo "something here"
      make -j 32 ;;
    n)
      echo "Continuing"... ;;
    *) 
      echo "Answer 'y' or 'n'" ;;
esac
like image 199
that other guy Avatar answered Oct 14 '22 16:10

that other guy


Due to variations in shell behaviors I suggest using spaces before semicolons... Single ';' allow for what you want. Double ';' 'end' the case 'match', i.e.

This should work:

case $ans1_1 
in
     y) fedoraDeps ;
        echo "something here" ;
        make -j 32 ;; ## last command for case 'match'
     n) echo "Continuing"... ;;
        ## if you want a blank line then just use one
     *) echo "Answer 'y' or 'n'" ;;
esac
like image 37
Dale_Reagan Avatar answered Oct 14 '22 17:10

Dale_Reagan