# Conditional Script

**URL:** <https://community.unix.com/t/conditional-script/344066>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [June 30, 2014, 6:16am UTC](https://community.unix.com/t/conditional-script/344066 "2014-06-30T06:16:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vrupatel](https://community.unix.com/letter_avatar/vrupatel/32/6_449c533f586939b6e5f246632a46421f.png) [@vrupatel](https://community.unix.com/u/vrupatel)\
**Post date:** [June 30, 2014, 6:16am UTC](https://community.unix.com/t/conditional-script/344066/1 "2014-06-30T06:16:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 30, 2014, 6:30am UTC](https://community.unix.com/t/conditional-script/344066/2 "2014-06-30T06:30:35Z")

</div>

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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [June 30, 2014, 6:34am UTC](https://community.unix.com/t/conditional-script/344066/3 "2014-06-30T06:34:56Z")

</div>

You have to use case statement like this

```nohighlight
#!/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`

---

<div class="post-metadata">

**Author:** ![vrupatel](https://community.unix.com/letter_avatar/vrupatel/32/6_449c533f586939b6e5f246632a46421f.png) [@vrupatel](https://community.unix.com/u/vrupatel)\
**Post date:** [June 30, 2014, 7:40am UTC](https://community.unix.com/t/conditional-script/344066/4 "2014-06-30T07:40:57Z")

</div>

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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Makarand\_Dodmis](https://community.unix.com/letter_avatar/makarand_dodmis/32/6_449c533f586939b6e5f246632a46421f.png) [@Makarand\_Dodmis](https://community.unix.com/u/Makarand_Dodmis)\
**Post date:** [June 30, 2014, 7:50am UTC](https://community.unix.com/t/conditional-script/344066/5 "2014-06-30T07:50:44Z")

</div>

try

```nohighlight
dos2unix update.sh update.sh

```

then run the script

```nohighlight
update.sh 1

```

---

<div class="post-metadata">

**Author:** ![vrupatel](https://community.unix.com/letter_avatar/vrupatel/32/6_449c533f586939b6e5f246632a46421f.png) [@vrupatel](https://community.unix.com/u/vrupatel)\
**Post date:** [June 30, 2014, 9:02am UTC](https://community.unix.com/t/conditional-script/344066/6 "2014-06-30T09:02:08Z")

</div>

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