Choose multiple conditions in case?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Choose multiple conditions in case?
# 1  
Old 08-01-2012
Choose multiple conditions in case?

Hi all,

I am attempting to create a shell script to optimize some routine process.
I want this script can let user select the actions they want to do.
But I met a problem that my script can only read one input and then do one action.
Is it possible to let my script to run more than one action? Thanks. Smilie
(Ex: user input 1 & 3, script can really perform action 1 & 3)

Following are part of my script:

Code:
echo -e "
1. Action 1
2. Action 2
3. Action 3
.
.
.
0. Exit"

read -p "Enter which Actions you want to perform?" ac

 case $ac in
  1)
    echo "Action 1"
    wget -c -nH -r -np ftp://$FTPIP/folder1
    cd /folder1
    tar zxvf *.tgz
    ;;
  2)
    echo "Action 2"
    wget -c -nH -r -np ftp://$FTPIP/folder 2
    cd /folder2
    tar zxvf *.tgz
    ;;
  3)
    echo "Action 3"
    wget -c -nH -r -np ftp://$FTPIP/folder 3
    cd /folder3
    tar zxvf *.tgz
    ;;
  0)
    exit
    ;;
 esac


Last edited by Scrutinizer; 08-01-2012 at 07:02 AM.. Reason: code tags
# 2  
Old 08-01-2012
Use loop. Here's an example:

Code:
#! /bin/bash
echo "Enter values:"
read choices

for x in $choices
do
    case $x in
        1) echo "Option 1" ;;
        2) echo "Option 2" ;;
        3) echo "Option 3" ;;
        *) echo "Get lost!"
    esac
done

Test runs:
Code:
[user@host ~]# ./test.sh
Enter values:
1 2
Option 1
Option 2
[user@host ~]# ./test.sh
Enter values:
1
Option 1
[user@host ~]# ./test.sh
Enter values:
1 2 3
Option 1
Option 2
Option 3
[user@host ~]# ./test.sh
Enter values:
1 2 3 4
Option 1
Option 2
Option 3
Get lost!
[user@host ~]#

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 08-01-2012
It works.
Thanks very very much, balajesuri!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple If conditions

I am analyzing one of the scripts written by another person.script is having multiple if conditions and everything are nested.The code is not formatted properly.Is there any way to identify in Unix to identify begin and end of a particular if block? (6 Replies)
Discussion started by: vamsi.valiveti
6 Replies

2. Shell Programming and Scripting

Multiple conditions in IF

Fellas, Am new to unix os/ and here the situation , I am trying to write multiple condition statement inside if but it throws me a error here is my piece of code , if ] && ] && ] then commands fi error : line 15 : ` can someone please advise me how to fix it Please use... (7 Replies)
Discussion started by: xeccc5z
7 Replies

3. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

4. Shell Programming and Scripting

Convert lowercase to upper case based on certain conditions

Hello Unix Gurus : It would be really great if a solution can be found Following is the condition on Solaris Change all the records to upper case ignore the case for records having "A2B2 " in them . how can i use nawk or any other command Following is the SAMPLE INPUT... (6 Replies)
Discussion started by: tsbiju
6 Replies

5. Shell Programming and Scripting

Multiple conditions in a CASE statement

is it possible to use multiple conditions in a CASE statement? And if so, what is the syntax? I'm trying to use one but can't seem to get it right. I want the statement to be CASE $vendor OR $alias condition 1) statements; condition 2) statements; etc. esac but I keep... (25 Replies)
Discussion started by: Straitsfan
25 Replies

6. Shell Programming and Scripting

Help regarding multiple conditions

Hi All, I am new to shell scripting. Can any one say what is wrong in this if statement, that uses multiple conditions if then *************** else if ( -z $pcs && "$night_time_calc" > "$night_time" ) then ******************************** ... (4 Replies)
Discussion started by: ssenthilkumar
4 Replies

7. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

8. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies

9. IP Networking

How to choose Multiple process or Multiple threads?

Hi All, Please explain me when i have to use multiple process and when I have to use Multiple threads? Please give me an example.It will be very helpful for me. Thanks in advance. (0 Replies)
Discussion started by: ashleykumar
0 Replies

10. Shell Programming and Scripting

multiple conditions in if statements

Hi all, I'm confused about the proper syntax for multi-conditional if then statements. I'm trying to set limitations on info input on the command line.. i.e. if ] ;then $x=$vr1 else print "You have entered an invalid option." Can someone please clue me in on what is wrong with my syntax;... (3 Replies)
Discussion started by: tim mauger
3 Replies
Login or Register to Ask a Question