Jump to content

evaluate a program's output as a command


Recommended Posts

hello there,

i just wondering is there a windows command that will take one program's output and execute it as a command, for example i have a small program(let's call a.exe) which will output "set abc=test"(using normal print statement),, i just wonder is there a command that will take the a.exe's output and execute it as command (i am trying to do in here is set environment variables), a command like:

eval `a.exe`

or i need to write a script to wrapper it, if so how do i do it?

thanks in advance for any help ( i know there are better way to set environment variables, but i like to do it this way, so please don't offer me alternative way of setting environment variables)

Link to comment
Share on other sites

What about a call function? I'm not exactly sure on what you are trying to accomplish.

Also, what language is this? C++, etc...

i am using tcl, i know there is a function call in tcl which allow you to change the environment variables in registry, but that's not what i want to do.

what i am trying to do is when user open a command prompt, and execute my command(something like myprogram.exe load perl, it will load the appropriate environment variable for perl to work in the current command prompt) and it will change the environment variable in the current command prompt session(the only way to do this is using set command in the current command prompt session), because i am using tcl, and because when my program runs it run as a child of the current command prompt and i child process can't modify its parent's environment variable.

so what i am trying to do is let the program to print out something like "set path=%path%;c:/to/stuff", and they using another command/script to evaluate the that output as command, hence accomplish change environment variable.

hence my questions will be is there a command that will evaluate the program's output and execute it(something like linux's eval command), or do i need to write a script for it? if so can someone point me some directions on how to implement it.

many thanks,

Link to comment
Share on other sites

i google for windows equivalent to linux's eval command, but no luck so far.

so i was thinking to write an script to do that, but i don't know much about windows scripting:

my idea is set the output of the program to an variable, something like: var Wscript.run("myprogram.exe")

because the myprogram is output (set abc=test), then i would like to run the variable as command.

i not quite sure if this will work or not, as i said i not familiar with windows scripting, so could someone help me please.

Link to comment
Share on other sites

I have not worked with tcl, but I do know most programs support variables. Making your program output a variable that can be interpreted by CMD.exe is up to the program's capabilities. As I said I have not used tcl, so I cannot help much.

From what I am assuming this cannot be done. You are trying to make a variable with your program, then input this variable into another program. As far as I know you cannot pass variables between programs, especially not programs coded in different languages.

I would assume the following would work (if it could?)

run some code

output a variable %1.

output a variable %2

pass these to another program (cmd.exe)

execute code "set %1=%2" from previous program

continue previous program

I am totally lost at this point. I may not understand exactly what you are trying to do. Perhaps someone else has an idea?

Link to comment
Share on other sites

I have not worked with tcl, but I do know most programs support variables. Making your program output a variable that can be interpreted by CMD.exe is up to the program's capabilities. As I said I have not used tcl, so I cannot help much.

From what I am assuming this cannot be done. You are trying to make a variable with your program, then input this variable into another program. As far as I know you cannot pass variables between programs, especially not programs coded in different languages.

I would assume the following would work (if it could?)

run some code

output a variable %1.

output a variable %2

pass these to another program (cmd.exe)

execute code "set %1=%2" from previous program

continue previous program

I am totally lost at this point. I may not understand exactly what you are trying to do. Perhaps someone else has an idea?

what i want to do it write a program that can dynamically load and unload a command prompts(cmd.exe) environment variables( these environment variables are not system or user wide variables, is a process environment variable.see more about environment variables), so that each time user can open a customized cmd.exe and run my program to dynamically load and unload specific environment varialbes, when the command prompt is closed all the environment variables will be lose, so no matter what user did in the command prompt(cmd.exe) session, it will not affect system/user wide environment variable.

when user open the command prompt(cmd.exe), they can use set command to set process wide environment variables which will lose when the cmd.exe is close( this is what i need), however i can't tell user to use millions of set command to set environment variable themself, so i want to write a program that can do something like: envload.exe load perl-5.8(this will set the PATH to perl5.8 directory), and when they want to switch to use perl5.7, i will need something like: envload.exe unload perl-5.8, then envload.exe load perl-5.7 (these commands will take perl5.8 of path environment variable, and add the path toperl5.7 to the PATH environment variable).

the problem is when my program is execute in a command prompt(cmd.exe), the system will say my program is a child of command prompt(cmd.exe), because for some security reason, a child process can not modify its parents environment variables. and my method of get around with this problem is let my program write a batch file and output its path, and then use script to take the output and execute it in the current command prompt session. so i like to do in my program is:

run some code with user's command like arguments

make a new batch file mainly with SET command in it ( something like: SET PATH=%PATH%;NEW/PATH; etc)

output the path of this batch file

execute this file in the current command prompt(cmd.exe)

my program now can generate a batch file, and i need some kind of script to glue all these steps together, because i don't want the users to execute the my program first and then run the batch file. i just want something that glue this 2 part together?

i hope someone can understand me.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy