Conditional Script

Hi,

I have a script file which has some simple commands. I want these commands to be executed based on the input. Ia m good with IF statement also. At the end it has to be based on incoming value. Example

CASE 1 :

Execute some commands where Input value as 1

CASE 2 :

Execute commands where Input value as 2

CASE 3 :

Execute commands where Input value as 3

How we can write a case statement within a script file and pass input to this script file. I need to have only one .sh file. While calling this.sh file, I will pass the number. The number which satisfies the case number the command in that block gets executed. I am currently on AIX version 6.1

Regards,
Vrushank Patel

This spec is very vague. More guessing than reading I'd propose

case $1 in 
   (1)    command_list1;;
   (2)    command_list2;;
   (3)    command_list3;;
   (*)    error handling;;
esac

, given you call that script like this.sh x , where x is 1, 2, or 3.

You have to use case statement like this

#!/bin/bash

if [ $# -lt 1 ]
then
        echo "Usage : $0 Input value"
        exit
fi

case "$1" in

1)  echo "process for input value 1"
    echo $1
    ;;
2)  echo "process for input value 2"
    echo $1
    ;;
3)  echo "process for input value 3"
    echo $1
    ;;
*) echo "Input $1 is not valid"
   ;;
esac

---------- Post updated at 05:04 PM ---------- Previous update was at 05:03 PM ----------

usage : scriptname inputvalue

Thanks Vishal & Rudic for your inputs. Vishal I tried you code and its gibing me below error:

update.sh[2]: ^M:  not found
' unexpected: syntax error at line 10 : `in

I am calling script as
sh update.sh 1
sh update.sh '1'
sh update..sh "1"

all 3 commands are giving me same error

try

dos2unix update.sh update.sh

then run the script

update.sh 1

Thank you very much Rudic, Makarand and Vishal for your help.