Jump to content
Sign in to follow this  
jazzyjeff

Understanding $CmdLine

Recommended Posts

jazzyjeff

Hello,

I am trying to understand the $CmdLine array. My goal is to run scriptA.exe have it create a variable $varA and then run scriptB.exe and have scriptB.exe output $varA to a Msgbox. It isn't working though, so I am not getting how this works.

This is what I am trying:

ScriptA -

$varA = 'Test'
Run('d:\(x86)\AutoIt3\AutoIt3.exe "D:\AutoIT Script\CMDLINE\scriptB.au3"')

ScriptB -

If $CmdLine[0] = 0 Then
MsgBox(64, "Result", "No variable was found.")
Else
MsgBox(64, "Variable", $CmdLine[1] & $varA)
EndIf

If someone can help me understand why this isn't working, I'd appreciate it.

Share this post


Link to post
Share on other sites
water

You have to pass $varA to the Run command as parameter.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jazzyjeff

How do I do that?

If I use this:

$varA = 'Test'
Run('d:(x86)AutoIt3AutoIt3.exe "D:AutoIT ScriptCMDLINEscriptB.au3" & $varA')

I just get an error opening the file.

Share this post


Link to post
Share on other sites
BrewManNH

Run(@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA)

Should work. Especially if the first program is already compiled, and if not will look for the autoit3.exe file and run that instead.

EDIT: forgot the code block :)

BTW, don't put variables inside the quotes, otherwise they become strings and not variables.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
water

A space is necessary between "scriptB.au3" and $varA as BrewManNH showed in his example.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jazzyjeff

Thanks for your help BrewManNH and Water.

So I compiled both scripts, because running them from Scite still wasn't working. However, even compiled the variable is never passed from scriptA to ScriptB.

Here are my compiled scripts:

ScriptA:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA = 'Test'
Run(@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA)
Run('"D:AutoIT ScriptCMDLINEscriptB.exe "' & $varA)

ScriptB:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA
If $CmdLine[0] = 0 Then
MsgBox(64, "Result", "No variable was found.")
Else
MsgBox(64, "Variable", $CmdLine[1] & @CR & $varA)
EndIf
MsgBox(0,"",$varA)

Maybe if I understood what is happening better I could figure this out.

So I first declare a variable in Script A

I then run ScriptB using Run("ScriptB.exe " & varA) and at the end I place the variable that I want transferred.

Now I'm confused. How does this variable become recognised by ScriptB? What does CmdLine do to store the variable in it's array, or how does it store it in it's array?

Maybe it doesn't and that may explain why this isn't working because I am way off track.

Edited by jazzyjeff

Share this post


Link to post
Share on other sites
jazzyjeff

Thanks Water. i did notice that after typing this and I did put a space in, but the variable is still never transferred to scriptB. In fact if I don't compile ScriptA, scriptB doesn't appear to even run.

Share this post


Link to post
Share on other sites
BrewManNH

What does the msgbox in script B show when you run it?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
water

$varA in ScriptB will always be empty because you never assign a value to the variable.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jazzyjeff

The first box I see says "No variable found" which must be because there are values stored in $CmdLine. The 2nd Msgbox is empty because $varA doesn't have a value.

:-) I get what your saying Water and I see why $varA has no value because I don't assign anything to it. What's missing to get $varA from scriptA to scriptB? I've tried assign $cmdline[1] to the value of $varA, but that doesn't work either.

I know I could always do a FileWrite and have the variable sent to a txt file and have the other script read the txt file and then delete it. It just seems like it'd be cooler to just have the variable passed between the scripts though.

Share this post


Link to post
Share on other sites
BrewManNH

Small problem in the post I made, the spaces/quotes are in the wrong place. I hadn't tested it because I'm at work and in the middle of something, but got a few minutes to tweak it.

Here's the corrected code.

; ScriptA
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA = 'Test'
Run(@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA) ; space needed AFTER the double quote and before the single quote
Run('"D:AutoIT ScriptCMDLINEscriptB.exe" ' & $varA)

; ScriptB
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA
If $CmdLine[0] = 0 Then
     MsgBox(64, "Result", "No variable was found.")
Else
     $varA = $CmdLine[1] ; assign the passed parameter to $varA
     MsgBox(64, "Variable", "$CmdLine[1] = " & $CmdLine[1] & @CR & "$varA = " & $varA)
EndIf
MsgBox(0, "", $varA)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jazzyjeff

Ah man that was such a basic oversight on my part! I'm sorry you spent time on that, but I totally appreciate it.

It works.

Water, BrewmanNH, thank you both. I feel like I understand how this works now. This will be really cool for developing/improving my other scripts.

Share this post


Link to post
Share on other sites
water

BrewmanNH was too fast. Just wanted to post a similar code.

Glad it works for you now!

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
BrewManNH

When dealing with quotes for a command line for Run/ShellExecute etc., I find that the easiest way to tell if the command line is correct is to use a ConsoleWrite on the command's parameters and run it from SciTE, if it looks ok there, then I know I've gotten the quotes/spaces right. Which is how I found out that the space was in the wrong place originally.

This:

@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA = F:PortableAutoIt3.3.8Appautoit3.exe /AutoIt3ExecuteScript "scriptB.au3" Test

Is what is should look like.

This:

@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA = F:PortableAutoIt3.3.8Appautoit3.exe/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "Test

Is what it looked like from what I posted the first time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
water

Another suggestion:

Write the Return Value plus the content of the macros @error and @extended to the console after each function call.

Global $iResult = Run(@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA)
ConsoleWrite("Return value: " & $iResult & ", @error: " & @error & ", @extended: " & @extended & @CRLF)

Run gets the PID of the started process. If you get 0 then there was an error. This makes sure you start to search at the right location and get all the information necessary for debugging.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
jazzyjeff

Thanks for the tips fellas. All good things to know.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • Skysnake
      By Skysnake
      Hi
      Problem is this
      myscript -p <zumerkelen> -x something else myscript is my AutoIt compiled CUI.  
      -p <> is first param - fails, see below
      -x [text] second param, works.
      When I run this with the param enclosed in <> I get the response
      The syntax of the command is incorrect.
      Experimentation indicates that the <> tags are the problem.
      _ArrayDisplay($CmdLine, "1 Array") MsgBox(16, "1 Raw", $CmdLineRaw) The problem is with the reading of the command line.  It fails BEFORE it gets into the variables...

      I am not dealing with AutoIt, or even my own command line, this is legacy software.  It seems that AutoIt "interprets" the <> before loading the command line...  I need to be able to read that as text without it being interpreted.
      The only part I can control is my script, the myscript, which I can change at will.
      The standard command line functions appear all to interpret params on read.  I want those params read raw.   If I could just get that as a string it would be fine.
      Where to look?
      Skysnake
       
    • jsmcpn
      By jsmcpn
      Hello all!  I'm trying to make a little script that sends a message over TCP.  An example payload would be:
      MY_PAYLOAD#<ATTRS><ATTR><NAME>MAILSERVER</NAME><OPERATION>set_value(ServiceStatus)</OPERATION><VALUE><![CDATA["Not Running"]]></VALUE><TIMESTAMP>1349297031</TIMESTAMP></ATTR></ATTRS> Note how the payload contains special characters, and how the CDATA contains a SPACE (i.e.  "Not Running")
      My compiled AU3 script expects exactly two command line arguments:
      1) destination server/port expressed as 127.0.0.1:80
      2) the payload I want to send to my server application (which may have multiple ATTRs, this example only has one ATTR)
      MyApp.exe 127.0.0.1:80 "MY_PAYLOAD#<ATTRS><ATTR><NAME>MAILSERVER</NAME><OPERATION>set_value(ServiceStatus)</OPERATION><VALUE><![CDATA["Not Running"]]></VALUE><TIMESTAMP>1349297031</TIMESTAMP></ATTR></ATTRS>"  
      My problems (plural) are with parsing of the second argument:
      1)  If I don't wrap second argument in double-quotes, then Windows bails out before even launching EXE with "< was unexpected at this time"
      2)  If I DO wrap second argument in double-quotes, then two problems can arise when my EXE parses the arguments:
                   aa) The double-quotes are stripped from the CDATA value. ["Running"] is stored in the array as [Running]
                   bb) If CDATA value DOES have a space, such as ["Not Running"], then I get an extra unwanted $CmdLine[3] because the SPACE between Not and Running is treated as a separator.  My second argument is thus split into two separate arguments, [2] and [3] instead of being treated as one arg stored in [2]
                              cc) Additionally, my script exits because it has an "IF $CmdLine[0] <>2 THEN Exit (1)" to validate the command line args.
      I've tried launching the EXE with the second argument wrapped and escaped in a multitude of ways.  Double double-quotes, triple double-quotes, single-quotes on the outside with double-quotes inside (i.e.  '"ETC"'), starting the second arg with a caret (i.e. ^"ETC") and one of three things happens:
      1) "< was unexpected at this time"
      2) my CDATA value is stripped of its double-quotes
      3) my payload argument gets split at the space in the CDATA value and the script bails out due to the unexpected 3rd argument.
       
      Any ideas?  How can I preserve the second argument literally, including all special characters, quotes and spaces?
      I tried adding some string manipulation code to grab the 2nd argument from $CmdLineRaw and it works (preserves the "Not Running" with quotes), but my number of arguments is still 3 instead of 2 if there is a space in the CDATA value.
       
      Any assistance would be greatly appreciated!
    • Chris86
×