Case question

Hi,
ksh script.

I have a shell script which use while loop and case statment in order to execute several functions.
Each step is working fine .
I want to add a new dynamic option : from step X to step Y which go through the selected steps .
for example : from step 2 to 3 , will execute option 2+3 only (function 2 and function 3) in the menu bellow.

Please advise.
Thanks

while :
do
  clear
  echo "----------------------------------------------"
  echo " * * * * * * * Steps * * * * *  * * "
  echo "----------------------------------------------"
  echo "[1]  Step1 "
  echo "[2]  Step2 "
  echo "[3]  Step3 "
  echo "[0]  Exit  "
  echo "----------------------------------------------"
  echo -n "Choose Option [0-3]:"
  read yourch
  case $yourch in
    0) exit 0
       ;;
    1)  function1 ;;
    2)  function2 ;;
    3)  function3 ;;
    *) echo " Please select choice 1,2,3 OR all"
    echo "Press a key. . ."
       ;;
  esac
done

You could insert the following code in red:-

while :
do
  clear
  echo "----------------------------------------------"
  echo " * * * * * * * Steps * * * * *  * * "
  echo "----------------------------------------------"
  echo "[1]  Step1 "
  echo "[2]  Step2 "
  echo "[3]  Step3 "
  echo "[0]  Exit  "
  echo "----------------------------------------------"
  echo -n "Choose Option [0-3]:"
  read yourch
  until [ $yourch -ge 4 ]
  do
    case $yourch in
      0) exit 0        ;;
      1)  function1 ;;
      2)  function2 ;;
      3)  function3 ;;
      *) echo " Please select choice 1,2,3 OR all"
      echo "Press a key. . ."
         ;;
    esac
    ((yourch=$yourch+1))
  done
done

You would need to make sure that yourch is numeric, else you will get all sorts of horrible errors.

Does this help?

Robin

Hi Robin,
Thank you for your quick feedback.
Its will work fine in case i will go through all the steps.
In reality i have more than 10 steps. To keep this senario simple i have mentioned steps 1-3 .
Its will not solve the problem in case i need to run just between steps 2-3 ( or between any other steps which are not all the steps).
Thanks again for sharing you time.
Yoav

So basically you want to be able to type say

2 4

and the script will execute steps 2,3 and 4?

Thats right. I want to be able to run range of steps :
1 to 4 , 2 to 4 , 3 to 4 and each
Thanks Again

In ksh93 and bash4 you can use fall through, using ;& :

case $yourch in
    0)  exit 0 ;;
    1)  function1 ;&
    2)  function2 ;&
    3)  function3 ;&
    4)  function4 ;;
    *)  echo " Please select choice 1,2,3 OR all"
        echo "Press a key. . ."
        ;;
esac
3 Likes

I wasn't aware that feature existed. Thank you for the introduction.

Regards,
Alister

Your description of what you want to do leaves some questions unanswered. And, none of your comments request changes to your code to support the "all" that your default case statement suggests.

All of your sample ranges essentially specify a starting step and then execute that step and all higher steps available in your sample case statement. Do you want to allow a user to specify that steps 1 and 2 should be performed (without performing later steps)?

Do the steps to be executed have to be contiguous? Should there be a way to specify that steps 1 and 3 are to be performed without step 2?

Do you want "all" to be treated as a request to run steps 1 through 10?

Am I correct in assuming that you still want to allow the user to request that any single step be performed?

My changes in red:

max=3
while :
do
  clear
  echo "----------------------------------------------"
  echo " * * * * * * * Steps * * * * *  * * "
  echo "----------------------------------------------"
  echo "[1]  Step1 "
  echo "[2]  Step2 "
  echo "[3]  Step3 "
  echo "[0]  Exit  "
  echo "----------------------------------------------"
  echo -n "Choose Option [0-3]:"
  read yourch fin
  if [ ${fin:=$yourch} -gt $max ]
  then exit 1
  fi
  if [ ${fin} -le 0 ]
  then exit 2
  fi
  until [ $yourch -gt ${fin} ]
  do
    case $yourch in
      0) exit 0        ;;
      1)  function1 ;;
      2)  function2 ;;
      3)  function3 ;;
      *) echo " Please select choice 1,2,3 OR all"
      echo "Press a key. . ."
         ;;
    esac
    ((yourch=$yourch+1))
  done
done

If no value is entered for $fin only one iteration is made. If a value is entered for $fin it will loop until $yourch is the same value as $fin . The (*) clause in the case statement should never be met and could be removed. Change the value of $max appropriately.

Andrew