(silent) during runtime?

Q&A's, tips, howto's
Locked
kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

(silent) during runtime?

Post by kanen »

I'd like to run something like

Code: Select all

(exec "ls")
and also have the result return to a variable, but not display to the screen at runtime.

This works;

Code: Select all

#!/usr/bin/newlisp
(silent (setf res (exec "ls")))
(println "Exiting")
(exit)
But, this displays output to the screen:

Code: Select all

#!/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 .

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: (silent) during runtime?

Post by 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
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: (silent) during runtime?

Post by kanen »

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...
ryuo wrote: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 .

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: (silent) during runtime?

Post by ryuo »

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:

Code: Select all

#include <stdio.h>

int main()
{
        fprintf(stdout, "Standard Output\n");
        fprintf(stderr, "Standard Error\n");
        return 0;
}
After compiling it as "text.exe", I proceed to test it at the newLISP prompt:

Code: Select all

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.

Locked