(silent) during runtime?

Started by kanen, November 14, 2014, 10:38:34 AM

Previous topic - Next topic

kanen

I'd like to run something like (exec "ls") and also have the result return to a variable, but not display to the screen at runtime.



This works;
#!/usr/bin/newlisp
(silent (setf res (exec "ls")))
(println "Exiting")
(exit)


But, this displays output to the screen:


#!/usr/bin/newlisp

(setf tarme (string "zip -r -X /tmp/foo.zip /etc/") )
(silent (exec tarme))

(println "Exiting")
(exit)


What am I missing?
. Kanen Flowers http://kanen.me[/url] .

ryuo

#1
I assume that newLISP only captures the standard output of the command. Therefore, if the command sends output to the screen, it is probably writing to standard error. I also assume that this command is sent to the system UNIX shell for interpretation. Therefore, you can redirect standard error to standard output to get it to show up in the return value of the exec function by appending " 2>&1" to the end of the command. I hope this is helpful.

kanen

#2
Very helpful, on *nix systems (of course)!



I was hoping for something in newLisp that was more like (silent) and would allow me to do this across all platforms (like Windows, Linux, BSD, OS X).



I suppose I could suppress output conditionally by platform...


Quote from: "ryuo"I assume that newLISP only captures the standard output of the command. Therefore, if the command sends output to the screen, it is probably writing to standard error. I also assume that this command is sent to the system UNIX shell for interpretation. Therefore, you can redirect standard error to standard output to get it to show up in the return value of the exec function by appending " 2>&1" to the end of the command. I hope this is helpful.
. Kanen Flowers http://kanen.me[/url] .

ryuo

#3
This method should work for all the UNIX-like systems, as this behavior is defined by POSIX IIRC. As for Windows, it also appears to do the same thing. I wrote a simple C program that is compiled with mingw to test this:



#include <stdio.h>

int main()
{
        fprintf(stdout, "Standard Outputn");
        fprintf(stderr, "Standard Errorn");
        return 0;
}


After compiling it as "text.exe", I proceed to test it at the newLISP prompt:

newLISP v.10.6.0 32-bit on Win32 IPv4/6 libffi, options: newlisp -h

> (exec {test.exe})
Standard Error
("Standard Output")
> (exec {test.exe 2>&1})
("Standard Output" "Standard Error")
> (exit)


It appears through my tests that windows also redirects standard error to standard output in this case, using the same syntax for redirection as UNIX shells. Notice how in the first usage  newLISP does not include the output sent to standard error in the return value of exec, but it does in the second usage when you append " 2>&1". I hope this helps.