问题描述
我在 kermit
脚本中编写了以下内容以连接到我的串行设备:
I wrote the following in a kermit
script to connect to my serial device:
#!/usr/bin/env kermit
set port /dev/ttyUSB8
set speed 115200
set carrier-watch off
set flow-control none
set prefixing all
set input echo on
上为所有
set输入回显加上前缀。现在,我想使它成为通用脚本,并希望从用户那里获取他想连接哪个端口的输入。因此,我认为将输入作为命令行参数是最好的方法。我以以下方式修改了上面的内容:
It does the job pretty well. Now, I want to make this a generic script and would like to take the input from the user which port he wants to connect. So, I thought taking input as a commandline argument is the best way to do. And I modified the above in the following way:
#!/usr/bin/env kermit
port_num="/dev/ttyUSB"+$1
set port port_num
set speed 115200
set carrier-watch off
set flow-control none
set prefixing all
set input echo on
上设置输入回显,但是,我得到以下错误:
But, I get the following error:
user4@user-pc-4:~/Scripts$ ./test.script 8
?Not a command or macro name: "port_num="/dev/ttyUSB$1""
File: /home/Scripts/test.script, Line: 2
port_num
?SET SPEED has no effect without prior SET LINE
"8" - invalid command-line option, type "kermit -h" for help
我尝试替换
port_num="/dev/ttyUSB"+$1
与
port_num="/dev/ttyUSB$1"
。
第二个脚本中有一个明显的缺陷。如何获取脚本以接受用户输入并使用 kermit
连接到串行端口?
There is an obvious flaw in my second script. How can I get the script to accept the user input and connect to the serial port using kermit
?
推荐答案
kermit脚本语言与bash完全不同。命令行中传递的参数以bash中的美元符号扩展(如 $ 1
)。在kermit中,它们使用反斜杠百分比表示法进行扩展,例如 \%1
The kermit script language is completely different from bash. Arguments passed on the command line are expanded by dollar signs in bash (as in $1
). In kermit, they are expanded with the backslash-percent notation, as in \%1
脚本引擎的命令行参数,您必须使用 +
参数调用 kermit
。
To pass subsequent command line arguments to the scripting engine, you must invoke kermit
with a +
argument.
要告诉操作系统您的脚本必须由 kermit
解释,您使用了所谓的 env shebang #!/ usr / bin / env
,它与 +
参数不兼容。这意味着您必须在系统上找到 kermit
,发出命令
To tell the operating system that your script has to be interpreted by kermit
, you used the so-called env shebang #!/usr/bin/env
, which is incompatible with the +
argument. This means that you have to locate kermit
on your system, issuing the command
$ type kermit
kermit is /usr/bin/kermit
(另一个常见位置是 / usr / local / bin / kermit
)。现在,将正确的位置放置在shebang中,添加 +
参数,即可完成操作:
(another common location is /usr/local/bin/kermit
). Now, place the correct location in the shebang, add the +
argument, and you are done:
#!/usr/bin/kermit +
set port /dev/ttyUSB8\%1
set speed 115200
set carrier-watch off
set flow-control none
set prefixing all
set input echo on
如果您想定义一个宏(用户定义变量的别名),这是一种定义默认值的方法:
If you want to define a macro (kermit name for user-defined variables) you can, and this is a way to define default values:
assign port_num \%1
if not defined port_num assign port_num 8
set port /dev/ttyUSB\m(port_num)
这篇关于我如何获取kermit脚本以接受参数并连接到串行设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!