Conditional Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Conditional Script
# 1  
Old 06-30-2014
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

Last edited by vrupatel; 06-30-2014 at 07:27 AM..
# 2  
Old 06-30-2014
This spec is very vague. More guessing than reading I'd propose
Code:
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.
# 3  
Old 06-30-2014
You have to use case statement like this
Code:
#!/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
# 4  
Old 06-30-2014
Thanks Vishal & Rudic for your inputs. Vishal I tried you code and its gibing me below error:

Code:
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
# 5  
Old 06-30-2014
try
Code:
dos2unix update.sh update.sh

then run the script
Code:
update.sh 1

This User Gave Thanks to Makarand Dodmis For This Post:
# 6  
Old 06-30-2014
Thank you very much Rudic, Makarand and Vishal for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with conditional clauses for script output

Hello. I am new this site as well as new to shell scripting and this is my first form... Please help me with the following shell script. I am executing a shell script to run every 15 min (scheduled in cronjob) and it gives an output in an output file which is e-mailed. CONCCOUNT=`cat... (1 Reply)
Discussion started by: Jamessteevens
1 Replies

2. Shell Programming and Scripting

How to add conditional check of whether a process is running before doing rest of script?

Hi, I'm looking at doing a cron job for a script that could take a very short time to complete or a very long time to complete based on variable activity on a server. I don't want to do a weird schedule, but I don't want to be so aggressive that the process attempts to start before another... (14 Replies)
Discussion started by: nbsparks
14 Replies

3. Shell Programming and Scripting

Smarter conditional script with Table

Hi, currently I have a script with conditional checking. But it's not flexible to use, because i must create a more conditional script if there are more keyword (see table) required. Question is: Can I create a more smarter script that will be more flexible? I appreciate any help anyone can give... (8 Replies)
Discussion started by: jazzyzha
8 Replies

4. Shell Programming and Scripting

Conditional Execution of a Script.

I have a unix shell script Test.sh more Test.sh echo "Calling dbquery1.sh...." ./dbquery1.sh echo "Calling dbquery2.sh...." ./dbquery2.sh more dbquery1.sh sqlplus -s user1/password1@DB_SID @/tmp/storedprocedures/Hello.rcp I run Test.sh However, I do not want dbquery2.sh to be... (3 Replies)
Discussion started by: mohtashims
3 Replies

5. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

6. Shell Programming and Scripting

Help on shell script conditional execution when CPU Idle > 60%

I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails. If... (2 Replies)
Discussion started by: pancona99
2 Replies

7. Shell Programming and Scripting

unix script for conditional execution

Below is my shell script. I am trying to execute two different BTEQ scripts depending on the day of the week. So on a saturday I will execute a certain BTEQ script and on other weekdays I will run the other script. #!/bin/ksh dt=`date +"%a"` if then bteq > final_output <<- EOF .run... (3 Replies)
Discussion started by: Mihirjani
3 Replies

8. Shell Programming and Scripting

Conditional Date Script

I am learning Unix for the first time. I am playing around with my default profile file to add some script in the KSH. The time format is GMT and I am in eastern time zone. I am trying to script some greetings based upon the time of day using the IF/ELIF conditions. I am getting errors. I have... (2 Replies)
Discussion started by: cpd259
2 Replies

9. Shell Programming and Scripting

Confusion over a small conditional script

Hi, I was going through a file containing hundreds of lines of codes having conditional statement written as: log() { echo $@ } num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w) (( num > 0 )) && log "db info:" so,if here the return value(stored in... (2 Replies)
Discussion started by: amit4g
2 Replies

10. Shell Programming and Scripting

creating a script using the case conditional

hello, i am a newbie who is just starting to use liunx, and I need to accept a string from the terminal and use case to echo a suitable message if the string doesn't have at least 10 characters. Also, how would I do the same thing using expr? (3 Replies)
Discussion started by: soccerstr1
3 Replies
Login or Register to Ask a Question