(silent) during runtime?

Q&A's, tips, howto's

(silent) during runtime?

Postby kanen » Fri Nov 14, 2014 6:38 pm

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

Re: (silent) during runtime?

Postby ryuo » Fri Nov 14, 2014 7:48 pm

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.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: (silent) during runtime?

Postby kanen » Fri Nov 14, 2014 9:22 pm

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

Re: (silent) during runtime?

Postby ryuo » Sat Nov 15, 2014 4:13 am

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.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 3 guests

cron