Scripting - Asking user for input.

Hi all,

I’m making an ‘install script’ to install another script on routerboards. Currently it’s setup as a function, and the user can pass arguments to the function.

Is there any way for a script to ask for user input via the console? E.G

$Please enter WAN interface-
$_

There’s the “/terminal inkey” command, which asks the user for a single key, and returns the key code of the pressed key, which you can then decode into an ASCII character or a keyboard key.

It’s as cumbersome to work with as it sounds… You need to invoke it as many times as the number of characters you need, or more precisely, keep invoking it until a new line is entered (character code 13). Oh, and you better support backspace (character code 8 ) too. If you manage to elegantly abstract away such stuff, you may as well create a function for that… One which wouldn’t need prompts upon its install :wink: . But at that point, I think we can agree that’s overkill.

boen_robot, this is the second time you’ve assisted with my scripting this week.

Thanks, it’s much appreciated.

Try out the following:

# function to prompt user to enter a value
:global read do={:return}

# storing the value entered by the user
:local userinput [$read]

It is interesting, that if the user enters 192.168.10.0/24, [:typeof $userinput] will be ip-prefix, not str.

You may want to have a look at this ones too:
http://forum.mikrotik.com/t/how-to-handle-y-n-response-in-rsc-script/37083/1
and
http://forum.mikrotik.com/t/functions-in-cmd-scripts/34902/1

I’m trying to make a initial configuration script and need to parse user input to set router name, WAN ip and GW, local ip and GW and so on…
When I’m trying to use

# function to prompt user to enter a value
:global read do={:return}

# storing the value entered by the user
:local userinput [$read]

put $read
showing me “;(eval (eval /return))” as answer :frowning:
What I’m doing wrong?
FW from 6.39 up to 6.42.3.

You need to :put $userinput, not :put $read :slight_smile:

Or use:
:put [$read] - to run the script (as for user input) and immediately put it on the screen without “storing it” in a variable :slight_smile:.



:put $userinput

is also empty for me after asked for a value (v6.47).

If you get actual code then use an extra pair square brackets.

:local userinput [$read]; :put [$userinput];

or use :set, instead of local/global

:set [userinput [$read]]; :put $userinput

{:local userinput [:return]; :put [$userinput]; } nice

This do not work, the second put is not executed, the terminal print the result regardless the :put

This is a 6.47.10 working example: (not tested on other versions)

{
:local readinput do={:return}
:put "\r\nInsert a string you want use as"
:local input [$readinput]
:put "\r\nYou have insert: $input"
:put "Which is a $[:typeof $input]\r\n"
}

Works on 6.48.4 as well.

You are right. Thank you