如何检查传递给expect脚本的参数的字符串值?

huangapple go评论56阅读模式
英文:

How to check the string value of an argument passed to expect script?

问题

我正在编写一个期望脚本,该脚本接受命令行参数。我想要能够检测第一个参数是否为“--help”,如果是,就打印一个使用说明,否则将参数用作具有特定默认值(假设为1818)的端口号。

我尝试了下面的代码,但失败了:

#!/usr/bin/expect 

if {[llength $argv] != 1} {
    puts "未指定端口号,将默认使用端口1818。"
    set port 1818
} else {
    if {[lindex $argv 0] eq "--help"} {
        puts "使用说明:testit [--help] [端口号]"
        exit
    } else {
        set port [lindex $argv 0]
    }
}

错误是:

无效的命令名称 "--help"
    在执行
"--help"
    在内部调用
"if {[llength $argv] != 1} {
    puts "未指定端口号,将默认使用端口1818。"
    set port 1818
} else {
    if {[lindex $argv 0] eq "--he..."

显然,它试图解释“--help”字符串的内容,而我试图让脚本比较参数0的值与“--help”。

上述逻辑或语法有什么问题?

我尝试使用其他字符串,如“help”而不是“--help”,但结果是一样的。

我对期望和tcl不是很熟悉,但我在tclsh中尝试了这个表达式,结果也是一样的。因此,这个问题与无效的tcl代码有关。以下是tcsh会话,显示了“if {$variable=="--help"} {...}”语法是正确的,但去掉上面的字符串比较尝试中的空格并不能解决问题。

这是tcsh会话:

% set v1 "--help"
--help
% if [v1 == "--help'] { puts "allo";}
extra characters after close-quote
% if [v1 == "--help"] { puts "allo";}
invalid command name "v1"
% if [$v1 == "--help"] { puts "allo";}
invalid command name "--help"
% if $v1 == help {puts "allo";}
invalid bareword "help"
in expression "--help";
should be "$help" or "{help}" or "help(...)" or ...
% if $v1 == "--help" {puts "allo";}
invalid bareword "help"
in expression "--help";
should be "$help" or "{help}" or "help(...)" or ...
% if {$v1=="--help"} {puts "allo";}
allo
% 
英文:

I'm writing a expect script that takes command line arguments. I would like to be able to detect whether the first argument is "--help" and print a Usage string then. Otherwise use the argument as a port number with a specific default (let's say 1818).

I tried this code that fails:

#!/usr/bin/expect 

if {[llength $argv] != 1} {
    puts "No Port number specified, defaulting to port 1818."
    set port 1818
} else {
    if {[lindex $argv 0] eq "--help"} {
        puts "Usage: testit [--help] [port]"
        exit
    } else {
        set port [lindex $argv 0]
    }
}

The error is:

invalid command name "--help"
    while executing
"--help"
    invoked from within
"if {[llength $argv] != 1} {
    puts "No Port number specified, defaulting to port 1818."
    set port 1818
} else {
    if {[lindex $argv 0] eq "--he..."

Obviously it is trying to interpret the content of the "--help" string while I'm trying to make the script compare the value of argument 0 to "--help".

What is wrong in the above logic or syntax?

I tried using other strings, like "help" instead of "--help" but the outcome is the same.

I'm not that familiar with expect and tcl, but I tried the expression in tclsh and the same thing happens there. So this issue has to do with invalid tcl code. The following tcsh session shows that the syntax if {$variable=="--help"} {...} is OK, but removing the white space in my string comparison attempt above does not solve the problem.

Here's the tcsh session:

% set v1 "--help"
--help
% if [v1 == "--help'] { puts "allo"}
extra characters after close-quote
% if [v1 == "--help"] { puts "allo"}
invalid command name "v1"
% if [$v1 == "--help"] { puts "allo"}
invalid command name "--help"
% if $v1 == help {puts "allo"}
invalid bareword "help"
in expression "--help";
should be "$help" or "{help}" or "help(...)" or ...
% if $v1 == "--help" {puts "allo"}
invalid bareword "help"
in expression "--help";
should be "$help" or "{help}" or "help(...)" or ...
% if {$v1=="--help"} {puts "allo"}
allo
% 

答案1

得分: 1

以下是翻译好的部分:

问题出在这一行:

puts "Usage: testit [--help] [port]"

问题是在这种情况下 [...] 会进行命令替换。你需要在其中添加一些反斜杠以防止这种情况,像这样:

puts "Usage: testit \[--help] \[port]"

或者你可以用大括号括起字符串以抑制 所有 替换:

puts {Usage: testit [--help] [port]}

两种方法都可以使用(它们会被编译成完全相同的东西,所以可以选择喜欢的方法)。

英文:

The problem is this line:

    puts "Usage: testit [--help] [port]"

And the problem with it is that [...] does command substitution in that situation. You need to add a couple of backslashes in there to prevent that, like this:

    puts "Usage: testit \[--help] \[port]"

Or you can enclose the string in braces to inhibit all substitutions:

    puts {Usage: testit [--help] [port]}

Either will work (and they'll get compiled to exactly the same thing so use whichever you prefer).

huangapple
  • 本文由 发表于 2023年2月16日 06:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定