How can I get kermit script to accept arguments an

2019-09-07 08:27发布

问题:

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

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

I tried replacing

port_num="/dev/ttyUSB"+$1

with

port_num="/dev/ttyUSB$1"

as well.

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?

回答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

To pass subsequent command line arguments to the scripting engine, you must invoke kermit with a + argument.

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

(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/ttyUSB8\m(port_num)


回答2:

Don't need the "+" sign. Just say

port_num="/dev/ttyUSB$1"