0

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?

skrowten_hermit
  • 437
  • 3
  • 11
  • 28

2 Answers2

2

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/ttyUSB\m(port_num)
Dario
  • 2,673
  • 20
  • 24
  • That did the trick! I tried the _macro_ too. Unfortunately, it didn't work for my case (like you said, it was not needed too). Could you share any links where I can learn more about `kermit` scripts (preferably with examples)? [This](http://www.columbia.edu/kermit/ckututor.html) one is not of much help. – skrowten_hermit Mar 31 '17 at 12:29
  • 1
    @skrowten_hermit I edited the macro part, which had a mistake. Now it works with a default value if you don’t provide one. The kermit project’s business model was that the software was gratis for noncommercial use and you had to pay for the docs. Now the source code is free, but there is not much doc around. The project’s new home is [here](http://www.kermitproject.org/) and, under __scripts,__ you find a rather extensive collection, but it is essentially only an updated version of the link you posted. – Dario Mar 31 '17 at 16:06
  • @nhed You are right. I corrected the answer. Thank you! – Dario Sep 17 '19 at 08:44
0

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

port_num="/dev/ttyUSB$1"
sureshvv
  • 4,234
  • 1
  • 26
  • 32
  • I tried that too. Didn't work. Does this mean `kermit` scripts are different from `bash` scripts? – skrowten_hermit Mar 31 '17 at 06:00
  • yes, an X script, is typically a script written for and executed by an interpreter program of name X. The hashbang at the first line (`#!`), while formatted as a comment, allows a file with the execute bit set to run under the specified interpreter. – nhed Dec 31 '19 at 00:10