TCL set Special Characters in a string

前端 未结 3 1057
名媛妹妹
名媛妹妹 2020-12-20 05:24

I want to set in TCL the below error message as a variable and compare with the error message from a network switch, by-passing the special characters

<
相关标签:
3条回答
  • 2020-12-20 05:51

    Well, you can use wrap it in curly brackets as follows:

    set x {Use [slot/port] or ["portname"] or [slot/] or [].}
    

    Alternatively, you can escape special characters with backslash.

    0 讨论(0)
  • 2020-12-20 05:59

    If you use double quotes, you also have to escape "inner" quotes:

    set x "Use \[slot/port] or \[\"portname\"] or \[slot/*] or \[*]."
    

    Or use braces and avoid escaping altogether

    set x {Use [slot/port] or ["portname"] or [slot/*] or [*].}
    

    See Tcl's 12 syntax rules, numbers 4 and 6

    0 讨论(0)
  • 2020-12-20 06:02

    Escape the double quotes in your quoted string as well. Or use curly-braces to group the string without any command or variable substitution occurring.

    set s {Use [slot/port] or ["portname"] or [slot/*] or [*].}
    puts $s
    set s "Use \[slot/port\] or \[\"portname\"\] or \[slot/*\] or \[*\]."
    puts $s
    

    You need to keep reading the documentation on quoting until you understand why this is ok and your example is broken or you will suffer endless problems in your scripting.

    Note that the code colouring implemented on stackoverflow will show the curly-braced version poorly. Tcl is quite content with this however.

    0 讨论(0)
提交回复
热议问题