help on switch command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help on switch command
# 1  
Old 05-16-2007
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?
# 2  
Old 05-16-2007
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  
Old 05-17-2007
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  
Old 05-17-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Switch to su

Hi, I've put the correct root password but why do I get this below? huamin@SOL11I:~$ su Password: su: Sorry huamin@SOL11I:~$ Many Thanks & Best Regards, HuaMin (16 Replies)
Discussion started by: HuaMin
16 Replies

2. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Linux

How to switch from command to SSH mode?

Hi all, I am new to CentOS and have been always using the server remotely using puTTy. I needed to access the machine from the console so I did a Alt+F2. Now I would like to go back to using the machine remotely via SSH. Can you please let me know what keys on the console I should hit to be... (6 Replies)
Discussion started by: tezarin
6 Replies

4. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

5. Shell Programming and Scripting

need help for cp with -p switch

Guys, I need to copy files from source to destination with datetime preserved I did it with cp -p <source>/file <destinaltion>/file But when I do stat command on copied file , it seems the copied file has "change time" modified. Please guide me in understanding (2 Replies)
Discussion started by: mohan_xunil
2 Replies

6. UNIX for Dummies Questions & Answers

Expect command to switch user

Hi I have written a script to switch user and do some operations. I used expect command it doesn't work. It switches the user and waits for the Password to be entered manually. Also, i tried to fetch the pasword from passwd file, it didn't work.The script is as below: ... (4 Replies)
Discussion started by: Sapna_Sai
4 Replies

7. Programming

Switch

using switch can we match for more than one values.. eg: switcha(a) { case 1, 2, 3: printf("ddd"); break; case 4, 5, 6: printf("mmm"); break; } In this case wat i found was only for the last value, i.e 3 and 6 the switch works. ... (12 Replies)
Discussion started by: abey
12 Replies

8. HP-UX

name service switch

hy, when i change the policy of name service switch on HPUX 10.X the effect is immediat but in release 11 and 11.11 the changes of /etc/nsswith.conf are efficient only after reboot even i restart the nis daemons or named.do you know why? (2 Replies)
Discussion started by: denisJ
2 Replies

9. Shell Programming and Scripting

can you switch

hi, i am try to run following script in c-shell, using switch command. #!/bin/csh choice=0 while do echo "system monitor" echo " 1) system paging 2) system file inf. 3) system disk inf. 9) exit " echo "select an option: \c" read choice case $choice in 1)... (3 Replies)
Discussion started by: neer45
3 Replies
Login or Register to Ask a Question