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

Does the shell command work (in a Macro) on Linux ?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
lmloftus
Newbie
Newbie


Joined: 07 Jun 2004
Posts: 1
Location: Newport, R.I.

PostPosted: Mon Jun 07, 2004 6:20 am    Post subject: Does the shell command work (in a Macro) on Linux ? Reply with quote

I'm using OpenOffice with Linux and have a macro attached to a button where I want to issue a Linux command and see the results. How do I do this ?
I tried
shell (bash -c :ls > myoutput.txt") (as suggested) This does not work.
Any ideas ? How do I do this ?

This is just a test - later I want to start a program/process from within an OpenOffice document. Any clue on how to do that ?
Thanks for any help.
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Mon Jun 07, 2004 8:54 am    Post subject: Reply with quote

I encounter this problem the first time with a macro from DannyB but never examine it deeply. The shell command doesn't work with my linux OS... and have no answer to give to youat the moment.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
avantman42
Super User
Super User


Joined: 28 Jul 2003
Posts: 751
Location: Staffordshire, UK

PostPosted: Mon Jun 07, 2004 10:22 am    Post subject: Re: Does the shell command work (in a Macro) on Linux ? Reply with quote

lmloftus wrote:
I'm using OpenOffice with Linux and have a macro attached to a button where I want to issue a Linux command and see the results. How do I do this ?
I tried
shell (bash -c :ls > myoutput.txt") (as suggested) This does not work.
Any ideas ? How do I do this ?

This is just a test - later I want to start a program/process from within an OpenOffice document. Any clue on how to do that ?
Thanks for any help.


The code you supplied doesn't work for me either, although it's worth noting that it didn't work on the command line until I removed the colon:
Code:

bash -c ls > myoutput.txt


However, I can run a program or bash script using shell. Both the following work for me:

Code:

shell ("/home/russ/my-bash-script")

shell ("kmail")


If you want to start a program, then I'd try passing the command to shell. If you want to run a script, you could either create it from OOo (using Basic's Write command) or have a stored script that you call with Shell.

Russ
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:05 pm    Post subject: Reply with quote

The following seems to work fine for me on 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=9503
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Thu Jun 10, 2004 5:33 am    Post subject: Reply with quote

yes it works fine : thank you Danny
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


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

PostPosted: Thu Jun 10, 2004 6:46 am    Post subject: Reply with quote

Still want to watch out for the embedded command having quotes.

The most general possibility would be to write a new Basic function that does this...
1. Create a temp text file.
2. Write the first line as "#!/bin/bash"
3. Write the second line as whatever command you want to execute.
4. Use the above Shell() command to do a "chmod" on the file created in step 1 to make it executable.
5. Use a Shell() command to execute the file created in step 1, using the above technique to capture its output into a second temp file.
6. Use the com.sun.star.ucb.SimpleFileAccess service to read the entire output temp file in as a string, and return it from this macro function.
7. Delete the temp files created in step 1 and step 5.

It would work something like this....

cOutput = MyShell( "....some complex commnad with wierd syntax and involving quotes...." )

The entire output of the command would return into the variable cOutput.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
kggmvmv
Newbie
Newbie


Joined: 16 Mar 2007
Posts: 2

PostPosted: Fri Mar 16, 2007 7:03 pm    Post subject: What about piping? Reply with quote

This is a bit of a digression ...
Is there a way to "pipe" the selected text (or something) *out* to a program,
and capture the output,
and replace the original text with that output?
Back to top
View user's profile Send private message
Mark B
Super User
Super User


Joined: 16 Feb 2007
Posts: 852
Location: Lincolnshire, UK

PostPosted: Sat Mar 17, 2007 2:24 am    Post subject: Reply with quote

Hi

Do you mean something like:
Code:

Sub send_params
myParams = "z y z"
myScript = "/home/myHome/MyScript"
shell ("bash -c """ & myScript & " " & myParams & """"
End Sub

or do you mean using the pipe on the Linux end:
Code:

Sub use_pipe
myScript="ps -ef | grep myName > /tmp/result.tmp"
shell ("bash -c """ & myScript & """"
End Sub

Mark
_________________
Mark B's Articles
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
kggmvmv
Newbie
Newbie


Joined: 16 Mar 2007
Posts: 2

PostPosted: Sat Mar 17, 2007 6:40 am    Post subject: Reply with quote

I suspect that I meant the latter.

This:
Code:
myParams = "z y z"
myScript = "/home/myHome/MyScript"
shell ("bash -c """ & myScript & " " & myParams & """"

looks to me like (a) I would have to know the parameters in advance,
and (b) they would have to be small enough to pass on the command line.

Whereas this:
Code:
myScript="ps -ef | grep myName > /tmp/result.tmp"
shell ("bash -c """ & myScript & """"

looks like "myScript" gets its input from STDIN.
However, I may or may not want the result to go to a file
such as "/tmp/result.tmp";
more likely I would want it to just come back to me and become part of the text I'm editing.

So, in a sense, neither example quite does what I'm thinking of.
The first one is a net *generator* of text;
the second one is a net *consumer* of text.

The fact that "myScript" in the second example
contains a pair of commands or programs piped one into the other
isn't really the issue.

Perhaps something like this would explain the concept:
Code:
cat < selected-text | external-command-or-script | cat > selected-text

Does this clarify what I'm looking for? Rolling Eyes
Back to top
View user's profile Send private message
M.Pruefer
Newbie
Newbie


Joined: 15 Mar 2007
Posts: 2

PostPosted: Tue Mar 20, 2007 3:07 am    Post subject: Reply with quote

Hello,

I had the same problem. I wanted to have a return string from my shell command. This solution is based on several articles, which I have read here in this forum.
Here is the solution, which works for me:

first I have a function, calling an external python program.
Code:
Function pythonShell( command As String ) As String
  Dim oScriptFactory
  Dim oScriptProvider
  Dim oScript

  oScriptFactory = createUnoService( "com.sun.star.script.provider.MasterScriptProviderFactory" )
  oScriptProvider = oScriptFactory.createScriptProvider( "" )
  oScript = oScriptProvider.getScript( "vnd.sun.star.script:Utilities.py$pythonShell?language=Python&location=share" )
  pythonShell = oScript.invoke( Array( command ), Array( ), Array( ) )
End Function



and on top of that, I have a python program (my filename is Utilities.py), which is in C:\Programme\OpenOffice.org 2.1\share\Scripts\python

remark: I had to change to Admin in order to write this file. And I had to convert this txt with dos2unix.

Code:
# Python shell command
import os, string
def pythonShell( theCommand ):
    output = os.popen( theCommand ).readlines()
    return "".join( output )


now I can call:
Code:
myString = pythonShell("mycommand.exe parameters")


works fine for me.

Still one question remains: this seems to be quite a lot of overhead. I was looking for "the StarBasic-way" to solve this just with StarBasic commands, but I was not able to find a better soloution yet.

Hope this helps.

Martin
Back to top
View user's profile Send private message
srees
Newbie
Newbie


Joined: 17 Oct 2009
Posts: 3

PostPosted: Sat Oct 17, 2009 9:03 am    Post subject: Reply with quote

using the below mentioned code, I am able to run normal executable.

Sub Main
cQuote = Chr( 34 )
Shell( "bash -c " + cQuote + "ls -la > /home/danny/test.txt" + cQuote )
End Sub

I have a perl script, which concatenates two xml files. Script takes two input files. and stores the output in one of the files. I am able to execute successfully from terminal, but not able to achieve the same from the above mentioned code.

Also have one more scenario, perl script generates two output files.

Could anyone please help me how to achieve both the scenarios using macros.
Back to top
View user's profile Send private message
pir
OOo Advocate
OOo Advocate


Joined: 20 May 2009
Posts: 200
Location: France

PostPosted: Mon Oct 19, 2009 12:44 am    Post subject: Reply with quote

Shell( "/usr/bin/perl -w myscript.pl")
or
Shell( "bash -c " + cQuote + "perl -w myscript.pl" + cQuote )
_________________
OpenOffice.org, teh best firewall
Back to top
View user's profile Send private message
srees
Newbie
Newbie


Joined: 17 Oct 2009
Posts: 3

PostPosted: Mon Oct 19, 2009 10:27 pm    Post subject: Reply with quote

Both the ways that has been mentioned is tried, but no output.

Attached is the reference code of concatenating two xml files. When I call this from open office macro, no error is observed and also no output .

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

my $result_twig;

foreach my $file ( 'sample1.xml', 'sample2.xml')
{ my $current_twig= XML::Twig->new( comments => 'process')->parsefile( $file);
if( !$result_twig)
{ $result_twig= $current_twig; }
else
{ $current_twig->root->move( last_child => $result_twig->root)
->erase;
}
}
open(FH,">sample1.xml");
$result_twig->print(\*FH);

kindly help

Regards,
Sree
Back to top
View user's profile Send private message
srees
Newbie
Newbie


Joined: 17 Oct 2009
Posts: 3

PostPosted: Tue Oct 20, 2009 10:06 pm    Post subject: Reply with quote

Thanks a lot for helping me to execute perl script from macros.

As in my previous post, I posted xml reference code, small change needs to be done for xml code inorder to work from macro and now it is working perfectly
Back to top
View user's profile Send private message
nvgishere
General User
General User


Joined: 10 Feb 2010
Posts: 38

PostPosted: Thu Mar 04, 2010 6:23 am    Post subject: Reply with quote

Hi ,

I have the following issue,

I used the shell command to copy the script from my current path to a user defined path mypath,
Shell ("cp myscript " & mypath & "myscript",1)
so my script gets copied in mypath. --- working fine

Now , I want to run the script from mypath.
The command i used is
Shell( mypath & "myscript",1). --- not working

Is the command which i am using for running the script from "mypath" correct?
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
Goto page 1, 2  Next
Page 1 of 2

 
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