Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-16-2007
Registered User
 

Join Date: May 2007
Posts: 21
help on switch command

hi

can anybody help me with the switch syntax?

set exam(AAA BBB CCC)
foreach ii ($exam)
switch ($ii)
case $ii=="AAA"
echo aaa
breaksw
case $ii=="BBB"
echo bbb
breaksw
case $ii=="CCC"
echo ccc
endsw
end

I couldnt print out aaa.bbb and ccc as I want on screen. I guess it might be the problem of switch syntax. Can anyone help me?
Sponsored Links
  #2 (permalink)  
Old 05-16-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,148
try:
Code:
#! /usr/bin/csh

set exam=(AAA BBB CCC)
foreach ii ($exam)
        switch ($ii)
        case "AAA"
                echo aaa
        breaksw
        case "BBB"
                echo bbb
        breaksw
        case "CCC"
                echo ccc
        endsw
end
  #3 (permalink)  
Old 05-17-2007
Registered User
 

Join Date: May 2007
Posts: 21
if I have 2 nested foreach loop and I have two conditions to satisfy in switch case, how should I correct the syntax below to the correct one?

#! /usr/bin/csh

set exam=(AAA BBB CCC)
set son=(aa bb cc)
foreach ii ($exam)
foreach jj ($son)
switch ($ii $jj)
case "AAA"&&"aa"
echo aaa
breaksw
default
echo BBB CCC
endsw
end
end
  #4 (permalink)  
Old 05-17-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,148
You can only use a single variable in a switch. But you could make it a combined variable:
Code:
#! /usr/bin/csh

set exam=(AAA BBB CCC)
set son=(aa bb cc)
foreach ii ($exam)
        foreach jj ($son)
                set result="${ii}${jj}"
                switch ($result)
                case "AAAaa"
                        echo aaa
                breaksw
                default
                        echo BBB CCC
                 endsw
        end
end
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
The Switch to DST 2007 --- How Did It Go? Perderabo What's on Your Mind? 6 03-14-2007 11:18 PM
Switch abey Programming 12 06-27-2006 08:11 PM
switch from csh to ksh veeracer Shell Programming and Scripting 8 11-05-2004 02:13 PM
switch from csh to ksh veeracer UNIX for Dummies Questions & Answers 1 11-03-2004 06:28 PM
can you switch neer45 Shell Programming and Scripting 3 12-05-2001 08:54 PM



All times are GMT -4. The time now is 02:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0