Jump to content

Run command output


Recommended Posts

Hello,

I have a function that runs an application with command line options than waits for the output to see if it ran correctly. But it doesn't seem to run and all I get is Error code 1, with no output for stderrRead. I've checked and the syntax of the command is correct. Anyone know how to find out why this isn't running?

Thanks,

Joe

CODE

Func AddADK()

Local $addkeycmd, $addkeyPID, $pswd, $Line

$pswd = InputBox("Enter Password","Enter password for an existing PGP passphrase user.","","*M")

If $pswd <> "" Then

$addkeycmd = $workingdir & "\PGPwde.exe -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase " & $pswd

MsgBox(0,"",$addkeycmd)

$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_MERGED)

While 1

$Line = StdoutRead($addkeyPID)

If @error Then

MsgBox(0,"Error","Could not add ADK." & @CRLF & "Error code: " & @error & @CRLF & "Output: " & StderrRead($addkeyPID))

ExitLoop

EndIf

Select

Case StringInStr($Line, "Add user completed") > 0 And ADKEXists() = "Yes"

MsgBox(0,"ADK Added","ADK Key has been successfully added.")

ExitLoop

Case StringInStr($Line,"bad passphrase") > 0

MsgBox(0,"Error","Bad Passphrase")

ExitLoop

Case $Line = ""

ContinueLoop

Case Else

MsgBox(0,"Error","Unknown Error" & @CRLF & $Line)

ExitLoop

EndSelect

WEnd

ElseIf @error = 1 Then

Return ;need to return something to know cancel button was pressed

EndIf

EndFunc

Link to comment
Share on other sites

Try this

Func AddADK()
Local $addkeycmd, $addkeyPID, $pswd, $Line

$pswd = InputBox("Enter Password","Enter password for an existing PGP passphrase user.","","*M")

If $pswd <> "" Then
$addkeycmd = $workingdir & "\PGPwde.exe -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase " & $pswd
MsgBox(0,"",$addkeycmd)
$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_MERGED)

While 1
$Line = StdoutRead($addkeyPID)
If @error Then ExitLoop
MsgBox(0,"Error","Could not add ADK." & @CRLF & "Error code: " & @error & @CRLF & "Output: " & $addkeyPID)
EndIf

Select
Case StringInStr($Line, "Add user completed") > 0 And ADKEXists() = "Yes"
MsgBox(0,"ADK Added","ADK Key has been successfully added.")
ExitLoop
Case StringInStr($Line,"bad passphrase") > 0
MsgBox(0,"Error","Bad Passphrase")
ExitLoop
Case $Line = ""
ContinueLoop
Case Else
MsgBox(0,"Error","Unknown Error" & @CRLF & $Line)
ExitLoop
EndSelect
WEnd
ElseIf @error = 1 Then
Return;need to return something to know cancel button was pressed
EndIf

EndFunc
Link to comment
Share on other sites

Nothing will wait for the process to run, so it may be that StdOut is empty. ProcessWaitClose will wait till it's closed before continuing. Also, StdErr_merged will put the output of StdErr in Stdout. You may way Stderr_child+stdout_child

$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($addkeyPID)
Link to comment
Share on other sites

After running your command line, you should ensure that the child process has written everything that it's going to write before making any tests against the string that holds the child output, either by doing as TurionAltec suggested and verifying that the child has exited, or by calling StdoutRead repeatedly in a loop and saving everything returned, a la:

While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

By the way, the @error tested for in the loop has nothing to do with the success/failure of your child program; setting @error is StdoutRead's way of signalling that there's no more data to be read from the child.

Basically the pitfall is trying to act on data that your child process may not have written yet.

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

I tried your code, the only difference is it showed the PID for the process. But still same results.

Try this

Func AddADK()
Local $addkeycmd, $addkeyPID, $pswd, $Line

$pswd = InputBox("Enter Password","Enter password for an existing PGP passphrase user.","","*M")

If $pswd <> "" Then
$addkeycmd = $workingdir & "\PGPwde.exe -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase " & $pswd
MsgBox(0,"",$addkeycmd)
$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_MERGED)

While 1
$Line = StdoutRead($addkeyPID)
If @error Then ExitLoop
MsgBox(0,"Error","Could not add ADK." & @CRLF & "Error code: " & @error & @CRLF & "Output: " & $addkeyPID)
EndIf

Select
Case StringInStr($Line, "Add user completed") > 0 And ADKEXists() = "Yes"
MsgBox(0,"ADK Added","ADK Key has been successfully added.")
ExitLoop
Case StringInStr($Line,"bad passphrase") > 0
MsgBox(0,"Error","Bad Passphrase")
ExitLoop
Case $Line = ""
ContinueLoop
Case Else
MsgBox(0,"Error","Unknown Error" & @CRLF & $Line)
ExitLoop
EndSelect
WEnd
ElseIf @error = 1 Then
Return;need to return something to know cancel button was pressed
EndIf

EndFunc
Link to comment
Share on other sites

TurionAltec,

I added ProcessWaitClose, and replaced stderr_merged. Still the same results.

Nothing will wait for the process to run, so it may be that StdOut is empty. ProcessWaitClose will wait till it's closed before continuing. Also, StdErr_merged will put the output of StdErr in Stdout. You may way Stderr_child+stdout_child

$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($addkeyPID)
Link to comment
Share on other sites

Dave,

I already have it in a while loop waiting for the results, the reason I check for @error first is because if it has finished running before any of the other ouptut was matched then something went wrong. I don't even get any output.

One questions why the "&" before "=", I thought you didn't need that.

After running your command line, you should ensure that the child process has written everything that it's going to write before making any tests against the string that holds the child output, either by doing as TurionAltec suggested and verifying that the child has exited, or by calling StdoutRead repeatedly in a loop and saving everything returned, a la:

While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend

By the way, the @error tested for in the loop has nothing to do with the success/failure of your child program; setting @error is StdoutRead's way of signalling that there's no more data to be read from the child.

Basically the pitfall is trying to act on data that your child process may not have written yet.

Link to comment
Share on other sites

  • 4 weeks later...

Hello,

I have a function that runs an application with command line options than waits for the output to see if it ran correctly. But it doesn't seem to run and all I get is Error code 1, with no output for stderrRead. I've checked and the syntax of the command is correct. Anyone know how to find out why this isn't running?

Thanks,

Joe

CODE

Func AddADK()

Local $addkeycmd, $addkeyPID, $pswd, $Line

$pswd = InputBox("Enter Password","Enter password for an existing PGP passphrase user.","","*M")

If $pswd <> "" Then

$addkeycmd = $workingdir & "\PGPwde.exe -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase " & $pswd

MsgBox(0,"",$addkeycmd)

$addkeyPID = Run($addkeycmd,$workingdir,@SW_HIDE,$STDERR_MERGED)

While 1

$Line = StdoutRead($addkeyPID)

If @error Then

MsgBox(0,"Error","Could not add ADK." & @CRLF & "Error code: " & @error & @CRLF & "Output: " & StderrRead($addkeyPID))

ExitLoop

EndIf

Select

Case StringInStr($Line, "Add user completed") > 0 And ADKEXists() = "Yes"

MsgBox(0,"ADK Added","ADK Key has been successfully added.")

ExitLoop

Case StringInStr($Line,"bad passphrase") > 0

MsgBox(0,"Error","Bad Passphrase")

ExitLoop

Case $Line = ""

ContinueLoop

Case Else

MsgBox(0,"Error","Unknown Error" & @CRLF & $Line)

ExitLoop

EndSelect

WEnd

ElseIf @error = 1 Then

Return ;need to return something to know cancel button was pressed

EndIf

EndFunc

This line

$addkeycmd = $workingdir & "\PGPwde.exe -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase " & $pswd

looks like it might be a problem. Try

$addkeycmd = '"' & $workingdir & '\PGPwde.exe" -d 0 --add-user --keyid 0xXXXXXXXX --admin-passphrase ' & $pswd
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...