Sponsored Content
Full Discussion: Tcl switch statement
Top Forums Shell Programming and Scripting Tcl switch statement Post 302316898 by chihung on Sunday 17th of May 2009 07:14:19 AM
Old 05-17-2009
The "$foo" inside the switch was not replaced and therefore it fall through to the default branch. However, if you set foo to be $foo, it works.

Code:
switch {$foo} {
a - b { puts 1 }
$foo { puts 2 }
default { puts 3 } 
}

I think you got the thing wrong
Code:
% switch
wrong # args: should be "switch ?switches? string pattern body ... ?default body
?"

You should treat the 2nd argument as the input string and match the pattern. In the below example, I use glob to do matching. You can also use regular expression (-regexp) if you want

Code:
switch -glob -- $input {
{to be*} { puts 1 }
{*Not to be} { puts 2 }
{*or*} { puts 3 }
default { puts 4 }
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using getopt with a switch statement

hi all, i have been trying to get a script working that can take in more than one option using getopt. But for some reason, even when i type in a covered option, it skips directly to my error message of "no match." Any ideas of what might be wrong? set - 'getopt frd*: $*' for i in $* do... (6 Replies)
Discussion started by: gammarays
6 Replies

2. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

3. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

4. Programming

Comapilation error with Switch statement

Hello , How to resolve below compilation error.activity_type is a member of structure and the output from databse will be stored in structure.Expected output wil l be either D or N or C . sample struct format: struct a{ char acAtivity_type; } code: switch (a->activity_type)... (1 Reply)
Discussion started by: jagan_kalluri
1 Replies

5. 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

6. Shell Programming and Scripting

Something is wrong with this switch( case statement.

I started writing a script to save the files from a camera I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: while : do ... (2 Replies)
Discussion started by: snakemasterAK
2 Replies

7. Programming

tcl-argv with switch & while

I was trying to deciper someone else code. I'm just learning tcl. I was a litte confused about the $argv. I was thinking it would be only a certain value in argv..but if say someone give the switch -cell and -path_to_ezqb is it right to have the second one $argv??? while ($#argv > 0) ... (2 Replies)
Discussion started by: carbuncle11
2 Replies

8. 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

9. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

10. Programming

Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again: for (j = 0; data != 0; j++){ switch(data){ case 'c': output = ranit(r_brace_array); break; case 'h': output = ranit(pipe_array); break; ... (6 Replies)
Discussion started by: Azrael
6 Replies
if(n)							       Tcl Built-In Commands							     if(n)

__________________________________________________________________________________________________________________________________________________

NAME
if - Execute scripts conditionally SYNOPSIS
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN? _________________________________________________________________ DESCRIPTION
The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument). The value of the expression must be a boolean (a numeric value, where 0 is false and anything is true, or a string value such as true or yes for true and false or no for false); if it is true then body1 is executed by passing it to the Tcl interpreter. Otherwise expr2 is evaluated as an expression and if it is true then body2 is executed, and so on. If none of the expressions evaluates to true then bodyN is executed. The then and else arguments are optional "noise words" to make the command easier to read. There may be any number of elseif clauses, including zero. BodyN may also be omitted as long as else is omitted too. The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN. EXAMPLES
A simple conditional: if {$vbl == 1} { puts "vbl is one" } With an else-clause: if {$vbl == 1} { puts "vbl is one" } else { puts "vbl is not one" } With an elseif-clause too: if {$vbl == 1} { puts "vbl is one" } elseif {$vbl == 2} { puts "vbl is two" } else { puts "vbl is not one or two" } Remember, expressions can be multi-line, but in that case it can be a good idea to use the optional then keyword for clarity: if { $vbl == 1 || $vbl == 2 || $vbl == 3 } then { puts "vbl is one, two or three" } SEE ALSO
expr(n), for(n), foreach(n) KEYWORDS
boolean, conditional, else, false, if, true Tcl if(n)
All times are GMT -4. The time now is 06:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy