RunCommand not returning program output string lazarus / 34-Windows ---------------------------------------------------------------------- JonBondy · 2025-08-19 20:24 When I run "magick compare" at the command prompt on Windows 11, it displays some numbers on the screen (see screen shot).  I need those numbers inside my program. When I do the same using RunCommandInDir from Lazarus, the program runs to completion successfully, but the string returned is empty.         s := 'compare -metric mae -subimage-search "' + wFiles + '" "' + appPath + 'vline.png" "' + appPath + 'out.png"'';         Memo1.Append('magick ' + s);         if RunCommandInDir(appPath, 'magick', s, sOut) then begin            Memo1.Append('Magick Output: ' + sOut);            // output is like "0 (0) @ 685,605" I tried adding " > xxxx.txt" to redirect the output to a file, but then the program fails to run at all, and the output file says "This file is too big to preview.  Max file size is 500KB".  Same thing using " 2> " Any thoughts? Jon ---------------------------------------------------------------------- Thausand · 2025-08-19 21:44 Quote from: JonBondy on August 19, 2025, 10:24:29 pmAny thoughts? Command example is not how runcommandindir work. Documentation/freepascal website now down for me but can read also here (https://dsiders.gitlab.io/lazdocsnext/fcl/process/runcommandindi r.html). Quoteconst commands: array of TProcessString; Is use array of string, not one string for argument. Also runcommandindir not work good when use quotes not correct (because is automate add/remove). Only work when know how is used correct. ---------------------------------------------------------------------- JonBondy · 2025-08-19 22:45 Thank you. There is no problem passing a simple string in as the command, and the quotes work fine.  The program runs as expected.  That is not my problem My problem is that the string that is normally presented on the line after the command (when running from the command prompt) is not in the output string (which IS defined as a string).  The output string is empty. Jon ---------------------------------------------------------------------- Thausand · 2025-08-20 01:37 Quote from: JonBondy on August 20, 2025, 12:45:06 amThank you. You welcome, sorry for bad English. QuoteThere is no problem passing a simple string in as the command, and the quotes work fine.  The program runs as expected.  That is not my problem I not know what tell because do is problem. When use single line exitstatus return 256 and mean there is error. magick return error "compare: unrecognized option `-metric mae' @ error/compare.c/CompareImagesCommand/848" because wrong separate argument. QuoteMy problem is that the string that is normally presented on the line after the command (when running from the command prompt) is not in the output string (which IS defined as a string).  The output string is empty. Make sure have include options = poStderrToOutPut, maybe (not 100% sure) usePipes also. ---------------------------------------------------------------------- n7800 · 2025-08-20 06:15 I can recommend you this very useful guide on running external programs. I hope it will help you. Quote from: JonBondy on August 19, 2025, 10:24:29 pmI tried adding " > xxxx.txt" to redirect the output to a file, but then the program fails to run at all, and the output file says "This file is too big to preview.  Max file size is 500KB".  Same thing using " 2> " Redirection ">", "|" (and other characters like "&&") - this is not the command line of the program, but the syntax of your shell (terminal), which runs commands in turn and itself passes the output between them (or writes to a file). Although, it seems that "ShellExecute" on Windows can do this. ---------------------------------------------------------------------- Thausand · 2025-08-20 10:51 Quote from: n7800 on August 20, 2025, 08:15:45 amRedirection ">", "|" (and other characters like "&&") - this is not the command line of the program, but the syntax of your shell (terminal), which runs commands in turn and itself passes the output between them (or writes to a file). Is true but can make work Linux (maybe also Windows ?) when use shell for exename (sh, bash, fish, etc) . For bash/sh can use -c option (then yes indeed must use single line argument for -c option and redirect/pipe). ---------------------------------------------------------------------- marcov · 2025-08-20 11:28 Quote from: Thausand on August 20, 2025, 03:37:44 amMake sure have include options = poStderrToOutPut, maybe (not 100% sure) usePipes also. That's also what I would try, btw pousepipes is added by tprocess.runcommandloop, the main code for the runcommand variants, so you don't need to add that manually. ---------------------------------------------------------------------- Aruna · 2025-08-20 11:52 It seems for "magick compare", the text output is written to stderr, not stdout. You can redirect stderr to stdout like this: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ COMP=$(magick compare in1.png in2.png -metric SSIM NULL: 2>&1 ) READ THIS PLEASE EDIT: Below is what chatGPT generated when I asked. This is untested but may help. The root issue: ImageMagick’s compare -metric ... does not write its numbers to standard output (stdout), it writes them to standard error (stderr). That’s why: When you run it manually in a terminal, you see the numbers (the terminal shows both stdout and stderr by default).When you run it from Lazarus with RunCommandInDir, only stdout is captured into your sOut variable, so you get an empty string.✅ Solution 1 – Tell RunCommandInDir to also capture stderr In Lazarus/Free Pascal, you can pass an option to RunCommandInDir: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Process; ... if RunCommandInDir(appPath, 'magick', s, sOut, [poUsePipes, poStderrToOutPut]) thenbegin  Memo1.Append('Magick Output: ' + sOut);end;  Notice the extra poStderrToOutPut in the options list. This merges stderr into stdout, so both normal output and error output are captured in sOut. That way, your metric numbers (0 (0) @ 685,605) will appear in sOut. ---------------------------------------------------------------------- JonBondy · 2025-08-20 14:04 Aruna: perfect!  That did the trick! Thank you! ---------------------------------------------------------------------- Thausand · 2025-08-21 23:16 Quote from: marcov on August 20, 2025, 01:28:18 pm... btw pousepipes is added by tprocess.runcommandloop, the main code for the runcommand variants, so you don't need to add that manually. Thank you marcov for explain because I did not know. Use poStderrToOutPut for option and not poUsePipes work expected with test. ---------------------------------------------------------------------- Thausand · 2025-08-21 23:19 Quote from: Aruna on August 20, 2025, 01:52:01 pmThis is untested but may help. When use multiple argument can not use one string for commands because not cross-platform. Reason explained previous, solution not work for Linux. AI never learn that or maybe did not ask.