LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2008, 01:59 AM   #1
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Rep: Reputation: 1
case esac in shell script


Hi folks,

Need a help here. case esac in shell script is like switch case of C.

switch case traverses all the case statements if break is not specified.

case 1) printf("1") ;
case 2) printf("2") ;

break;

if 1 matches it prints 12 if 2 is matched then it prints only 2

How do I achieve the same in shell script with case esac.

Tried doing it like this


case $patter in

1) echo "1"

2) echo "2"
;;

This gives a syntax error.. says extra ')' after 1)


Hope someone has achieved it.



Thanks,
Vadi
 
Old 04-23-2008, 02:26 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
case $pattern in
1)
   echo "1"
   ;;
2)
   echo "2"
   ;;
esac
good day
 
Old 04-23-2008, 03:04 AM   #3
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
Nope it wont print 12 for 1 it print 1 if 1 is matched and 2 if 2 is matched.

I want to execute 1 and 2 if 1 is matched and only 2 if 2 is matched..
Like the example I gave in C switch case.


Any other???
 
Old 04-23-2008, 03:49 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
i think there's no way you can do that since case-esac is not continuous.
perhaps you can do it with only these two:
Code:
case $pattern in
1)
   echo -n "1"
   ;;
1|2)
   echo -n "2"
   ;;
esac
or
Code:
if [[ "$pattern" == "2" ]]; then
   if [[ "$pattern" == "1" ]]; then
      echo -n "1"
   fi
   echo -n "2"
fi
 
Old 04-23-2008, 04:04 AM   #5
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
Still wont solve my purpose..


I was trying to avoid some duplicate statements on my script. Its just not 1 or 2 case some n cases.


Hoping that someone would have got it.
 
Old 04-23-2008, 04:35 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As konsolebox already pointed out, the case/esac construct does not work that way in bash. If you're not stuck with case, you can try a different approach like this
Code:
#!/bin/bash
pattern=1
limit=4
while [ $pattern -le $limit ]
do
    printf "$pattern"
    pattern=$((pattern + 1))
done
echo
Anyway, what are you trying to achieve? Will pattern be always numeric?

Edit: the same result can be achieved in recent bash with a single piped statement, as
Code:
echo {1..4} | sed 's/ //g'
but again it depends on what you're trying to achieve.

Last edited by colucix; 04-23-2008 at 04:38 AM.
 
Old 04-23-2008, 06:24 AM   #7
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
You guys got the whole thing wrong..


I dont want to print 123 or any thing.

her it goes.

case $pattern in

1) echo " statements for pattern 1 match will be executed \n "
;;

2) echo " statements for patter 2 match will be executed \n"

;;

3) echo " statement for patter 3 match will be executed "

;;
esac;


now how do I make this script print

Statement for pattern 1 match will be executed
Statement for pattern 2 match will be executed

when the patther=1

i.e when I give patter=1 then two cases should be executed 1 and 2 Like in C language switch() case statements.

You dont give a break; statements the statements gets executed for the perticular case untill a break is found.

if I give

switch(var1)
{

case 1: printf(" Statement for pattern 1 match will be executed") ;

case 2: printf ("Statement for pattern 2 match will be executed ") ;
break;
case 3: printf ("Statement for pattern 3 match will be executed ") ;
break ;

}


I achive what I want from the above C snippet. I want to achive the same in shell script.
 
1 members found this post helpful.
Old 04-23-2008, 06:50 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What I am seeing here is you wanting the BASH "case" construct to work the same as the C "switch"---and several people telling you that it is different. Have you looked this up in the Advanced Bash Scripting Guide (ABS)? ...free at http://tldp.org
 
Old 04-23-2008, 07:39 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yeah... just in case it's not clear enough: case in bash is different from switch in C/C++

Anyway you can mimic the behaviour of C/C++ (which depends on the presence or absence of the break statement) in this way
Code:
#!/bin/bash

pattern=1

while [ $pattern != BREAK ]
do
   case $pattern in
    1)
      echo 1
      pattern=2
      ;;
    2)
      echo 2
      pattern=3
      ;;
    3)
      echo 3
      pattern=BREAK
      ;;
    4)
      echo 4
      pattern=5
      ;;
    5)
      echo 5
      pattern=BREAK
      ;;
    *)
      pattern=BREAK
      ;;
   esac
done
The case statement is inside a while loop which will be executed until the pattern is equal to a keyword (BREAK). At each matching condition the pattern is updated to the next one. In this way the test is repeated and the result is exactly what you're trying to get. Note that you can insert BREAK at any point in the case construct.
 
Old 04-24-2008, 12:29 AM   #10
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
Thanks a lot colucix. This looks simpler.
 
Old 08-09-2011, 05:55 AM   #11
r_vigneswaran
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 5
In place of ;; we can use ;& (unconditional follow up) and ;;& (conditional follow up). So, the following will print 12, if the pattern is 1 and will print 2, if the pattern is 2.

case $pattern in
1)
echo "1"
;&
2)
echo "2"
;;
esac

Though this is an old thread, the answer may help others. Credit goes to my boss who found it for me from bash man page.
 
1 members found this post helpful.
Old 08-09-2011, 06:12 AM   #12
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
Awesome!!

Thank you very much for your reply. The thread was almost dead. This shows the answer can never die. Thanks again for the reply, and it works great.

Cheers,
Vadi
 
Old 08-09-2011, 06:30 AM   #13
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Original Poster
Rep: Reputation: 1
Another point to note. I think it is a new feature in the newer versions of bash. I'm sure this would not have been existing back in 2008. Though I dont have when this feature got into bash but I can say that bash-3.0 dint have it.
 
Old 08-09-2011, 07:10 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@r_vigneswaran: Cool I never knew that. Do you know what versions of bash it applies with?
 
Old 08-09-2011, 11:48 PM   #15
r_vigneswaran
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 5
As you people said, it may not be available in previous versions. I am using Fedora 15 and the bash is bash-4.2.10-4.fc15.i686.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script for converting filenames to proper case? Yalla-One Programming 9 02-03-2010 02:37 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM
i want to make a script with case.esac Alexander.s Linux - Newbie 3 12-14-2004 05:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration