OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

shell command - How do I see the results of my shell command

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
malone
Guest





PostPosted: Thu Jun 03, 2004 9:53 am    Post subject: shell command - How do I see the results of my shell command Reply with quote

I have a macro attached to a button that looks like this ;

Sub LISTDIR()
shell "'ls'"
End Sub

I run it and see nothing. Where does the output of the ls command go ?
How do I display it ?
Back to top
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Thu Jun 03, 2004 2:28 pm    Post subject: Reply with quote

I'm not sure the command is actually running.

In Windows, shell "dir" results in a file not found error but shell "command.com /C dir" (or /K) works so I expect you need to speak to bash , assuming that's what your using, in the same manner. Running Cygwin on Windows and using "bash &ls" gives me a directory but I don't think that's exactly the right way to do it.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Jun 04, 2004 7:11 am    Post subject: Reply with quote

Since there are only two short messages above, I won't quote from each one.

In Windows, there is not a DIR.EXE, the DIR command is a "built in" command of the COMMAND.COM program. (Or CMD.EXE or whatever they call it in the current flavor of Windows.) Therefore, you cannot run a "DIR" command as a shell command, because there is no such DIR.EXE command.

In Linux, there IS a command "ls". In fact, the counterpart for many of DOS's built in commands are actually separate executables on a Unix flavored system.

Where does the output to "ls" go? Well, to standard output. The Shell command is creating a process with standard in, standard out, and standard error connected to something. They might possibly be connected to /dev/null so that the process you start from Shell has no input and its output goes nowhere.

You could try a "command" like this...

bash -c "ls > myoutput.txt"

This single command runs the "bash" program (the counterpart of DOS's command interpreter, as suggested above).

Then the -c option tells bash to run a command. The command is: ls > myoutput.txt. This command ls runs, sending its output into a text file, which you can later recover from OOo Basic.

Now I must ask the question, why do you want to do this? If you are just trying to run "ls" and then pick up its text output, why not use a platform-independant way to get information about files? Then your macro can run on any platform without modification. OOo's API provides a service that lets you get the names of files and folders, and information about them.

See this example program which creates a directory listing into a Writer document by using the com.sun.star.ucb.SimpleFileAccess service.

Directory listing into a Writer Table
http://www.oooforum.org/forum/viewtopic.php?t=9454

Because the method used in the above example returns an array of the names within a directory (with or without folders at your option), you could even buld a recursive function that could walk through files recursively within a subdirectory. And such a macro would work on any version of OOo. Linux. Windows. Mac OS X. Or even on a platform that doesn't exist today, but to which OOo is later ported!

And, oh yeah, I shouold mention, because the technique uses OOo's API rather than an OS specific command, it works in any programming language not just in Basic. (OOo's API is the same no matter what language you use to write your programs.)
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Jun 09, 2004 3:08 pm    Post subject: Reply with quote

The following seems to work fine for me in Linux....
Code:
Sub Main
   cQuote = Chr( 34 )
   Shell( "bash -c " + cQuote + "ls -la > /home/danny/test.txt" + cQuote )
End Sub


http://www.oooforum.org/forum/viewtopic.php?t=9594
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group