Community discussions

MikroTik App
 
marrold
Member
Member
Topic Author
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

Scripting - Asking user for input.

Mon Aug 04, 2014 6:01 pm

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-
$_
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Scripting - Asking user for input.

Mon Aug 04, 2014 6:05 pm

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 ;) . But at that point, I think we can agree that's overkill.
 
marrold
Member
Member
Topic Author
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

Re: Scripting - Asking user for input.

Mon Aug 04, 2014 6:19 pm

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

Thanks, it's much appreciated.
 
A9691
newbie
Posts: 25
Joined: Sat May 14, 2016 10:58 am

Re: Scripting - Asking user for input.

Thu Mar 01, 2018 10:19 pm

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.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: Scripting - Asking user for input.

Thu Mar 01, 2018 11:09 pm

You may want to have a look at this ones too:
viewtopic.php?t=41042
and
viewtopic.php?t=38683#p284764
 
DiVersant
just joined
Posts: 1
Joined: Wed Mar 07, 2018 12:08 pm

Re: Scripting - Asking user for input.

Mon Jun 04, 2018 11:21 am

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 :(
What I'm doing wrong?
FW from 6.39 up to 6.42.3.
 
cantanko
newbie
Posts: 39
Joined: Mon Apr 05, 2010 12:53 am

Re: Scripting - Asking user for input.

Wed Jun 20, 2018 4:40 pm

You need to :put $userinput, not :put $read :)
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Scripting - Asking user for input.

Wed Jun 20, 2018 5:39 pm

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 :).
 
dke
newbie
Posts: 47
Joined: Tue Dec 10, 2019 11:30 pm
Location: Austria

Re: Scripting - Asking user for input.

Thu Jun 04, 2020 7:55 am

You need to :put $userinput, not :put $read :)
:put $userinput
is also empty for me after asked for a value (v6.47).
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Scripting - Asking user for input.

Mon Nov 23, 2020 3:35 pm

You need to :put $userinput, not :put $read :)
: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
 
liuyao
just joined
Posts: 9
Joined: Wed Sep 04, 2019 9:14 am
Location: China

Re: Scripting - Asking user for input.

Thu Nov 18, 2021 6:13 pm

{:local userinput [:return]; :put [$userinput]; } nice
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Scripting - Asking user for input.

Thu Nov 18, 2021 8:02 pm

This do not work, the second put is not executed, the terminal print the result regardless the :put
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Scripting - Asking user for input.

Thu Nov 18, 2021 8:10 pm

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"
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Scripting - Asking user for input.

Thu Nov 18, 2021 8:58 pm

Works on 6.48.4 as well.
 
liuyao
just joined
Posts: 9
Joined: Wed Sep 04, 2019 9:14 am
Location: China

Re: Scripting - Asking user for input.

Fri Nov 19, 2021 12:27 pm

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"
}
You are right. Thank you

Who is online

Users browsing this forum: No registered users and 27 guests