How to execute a command?

Discuss and share scripts and script files...
MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

How to execute a command?

Post by MBaas »

I have a scenario in a script where I process a file and end up with $cmd containing a command the user wants to execute. That command can be a simple as $foo=1+2; or I might load another script that assigns a value $foo somehow.

I can't find a way to deal with that correctly - despite attempts to read up on "Interpolation", searching the "Scripting Commands" for various keyswords that came to mind etc.

A simple repro is this:

Code: Select all

  
  global $foo;
  $foo = "";
  $cmd = '$foo=1+2';  // this comes from file normally...
  load $cmd,,"s";      // how to execute what's in $cmd???
  text $foo;  


How can I get it to execute this - it seems I'm unable to find the appropriate command?
______________________________________________
Happy user ;-)

highend
Posts: 13260
Joined: 06 Feb 2011 00:33

Re: How to execute a command?

Post by highend »

eval()

But
01. It may need correct quoting of what to eval
02. You need to assign it to a real variable, '$foo=<expression>' wouldn't work
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

jupe
Posts: 2749
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: How to execute a command?

Post by jupe »

If you are just asking how to make that script work as I think you intend, then this should work.

Code: Select all

  global $foo;
  $foo = "";
  $cmd = 'global $foo; $foo=1+2';  // this comes from file normally...
  load $cmd,,"s";      // how to execute what's in $cmd???
  text $foo;  

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

Thanks jupe! I thought the "global" in the parent made the var global once and for all, but if a child wants to act on that var, it also needs to globalize it, right? That wasn't obvious after RTFMing.

But still, something doesn't work quite as I expect - it just doesn't "keep" the value. (Unfortunately the tracer does not show globals - is there perhaps a tweak to achieve that?).
I think the screenshot of my actual in the tracer shows a use-case that corresponds to the repro I gave - but I must be having a dumb moment, because, as you see, the var $MyMenu is empty after executing it.
19-01-_2021_07-05-36.png
19-01-_2021_07-05-36.png (17.54 KiB) Viewed 2236 times
______________________________________________
Happy user ;-)

jupe
Posts: 2749
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: How to execute a command?

Post by jupe »

Yes globals need defining in each script.

I am a little unclear again what you are asking, but you are defining $MyMenu in the child script, but expecting the variable response in the main script within which you just unset the variable, so the resolution should be the same as last time but the inverse, the var needs to be globalized in the main script, did you do that? I can't tell from that screenshot, but globals do show up in the "tracer" if they are defined properly, no tweak necessary.

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

Well, I have a script that should execute user-defined code. I don't know if there are "best practices" to do that, but I figured I'd have a global (first line in the script is "global $MyMenu;" actually) and the user-code could manipulate that. Since it happend in a loop, I thought it would be best to unset it in every loop and check if it was set after executing user-code.

If the "global"-command in my script is all that was required to properly set it up, I am rightfully expecting to see it in the "stepper" (since you quoted my word "tracer", what is proper XY-lingo for that tool?), right?
Not happening here :(
19-01-_2021_08-41-42.png
19-01-_2021_08-41-42.png (14.89 KiB) Viewed 2223 times
______________________________________________
Happy user ;-)

jupe
Posts: 2749
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: How to execute a command?

Post by jupe »

If you unset a variable, even if it is subsequently made global in a child script, it is still kept unset in the main script. Just based on your screenshot, this is a quick mock up that I think should do what you are trying to do.

Code: Select all

	global $MyMenu;
	$cmd = 'global $MyMenu = <get list_recentlocations>;';
	load $cmd, , "s";
	text "Executed " . $cmd . <crlf> . 'menu=' . $MyMenu;
I quoted tracer because I had never heard that term and wasn't sure if you meant the variables or the step dialog., I wasn't indicating your term was in any way wrong, personally I call it the stepping dialog, and the offshoot the variables dialog, but I am not to say which is right or wrong.

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

Wow, thanks! It was really as simple as replacing the unset with $MyMenu="";
It was "just" that stupid misunderstanding - and the hlp even says that the var will be "destroyed". It's understandeable that it wouldn't be global any more when it gets back to live, but I just did not see that before. :oops:
______________________________________________
Happy user ;-)

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: How to execute a command?

Post by klownboy »

Many times when dealing with variables and menus it's best to use a perm variable especially if you need the variable in a menu title. Just make sure you unset the perm when the script terminates. Again I'm not sure what you are after in the end for output, but something like this works:

Code: Select all

	perm $MyMenu = "My Recent Locations Menu";
	$cmd = $MyMenu<crlf><get list_recentlocations>;
	load $cmd, , "s";
	text "Executed " . $cmd . <crlf> . 'menu=' . $MyMenu; 
	unset $MyMenu;
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

To be honest, the word "permanent" sounded frightening when I RTFM'ed while studying other code-samples. The least I would want to do is leave something permanent in the environment! But maybe that's a too traditional viewpoint inspired by frameworks and conditions which do not apply here.

But, as I learned (the hard way), as "unset" can erase the var, it's not as "dangerous" as I thought :wink: But ofc, determining the appropriate context for its use is something that overchallenges me. We'll see - I will definitely post the final result (next year, when I learned and asked enough to get it working...🤣)

Thanks!

Michael
Last edited by MBaas on 19 Jan 2021 17:44, edited 1 time in total.
______________________________________________
Happy user ;-)

klownboy
Posts: 4089
Joined: 28 Feb 2012 19:27

Re: How to execute a command?

Post by klownboy »

Yes honestly I try to shy away from using any permanent variable in situations where they are carried over and left permanent in the system after the script is done, but using them within a script and clearing them out when it exits, that 's no problem for me. Sometimes it's best to use the special hidden sub "_Terminate"; in the script. It's always run when the script ends (i.e., it doesn't have to be called).
So in this case...

Code: Select all

"_Terminate";
   unset $MyMenu;
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

Ah, sweet! 8)
______________________________________________
Happy user ;-)

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

So I have adopted the approach to execute $cmd via load $cmd,,s; and while it works nicely in most cases, I now came across one example where it didn't.
Repro:

Code: Select all

$cmd="#117;";load $cmd,,s;
This should copy the active file content to the clipboard - but it doesn't. (I had selected/highlighted/focussed a file and pasted that code into the address bar for the easiest repro I could imagine - but didn't get the filecontent to the clipboard)
Probably I am missing something terribly simple - but I don't see what it might be :cry:
Would appreciate some insight :wink:
______________________________________________
Happy user ;-)

highend
Posts: 13260
Joined: 06 Feb 2011 00:33

Re: How to execute a command?

Post by highend »

Does #117; from the address bar work?
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

MBaas
Posts: 572
Joined: 15 Feb 2016 21:08

Re: How to execute a command?

Post by MBaas »

Yes, it does.
______________________________________________
Happy user ;-)

Post Reply