| View previous topic :: View next topic |
| Author |
Message |
lmloftus Newbie

Joined: 07 Jun 2004 Posts: 1 Location: Newport, R.I.
|
Posted: Mon Jun 07, 2004 6:20 am Post subject: Does the shell command work (in a Macro) on Linux ? |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
avantman42 Super User

Joined: 28 Jul 2003 Posts: 751 Location: Staffordshire, UK
|
Posted: Mon Jun 07, 2004 10:22 am Post subject: Re: Does the shell command work (in a Macro) on Linux ? |
|
|
| 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 |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Thu Jun 10, 2004 6:46 am Post subject: |
|
|
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 |
|
 |
kggmvmv Newbie

Joined: 16 Mar 2007 Posts: 2
|
Posted: Fri Mar 16, 2007 7:03 pm Post subject: What about piping? |
|
|
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 |
|
 |
Mark B Super User


Joined: 16 Feb 2007 Posts: 852 Location: Lincolnshire, UK
|
Posted: Sat Mar 17, 2007 2:24 am Post subject: |
|
|
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 |
|
 |
kggmvmv Newbie

Joined: 16 Mar 2007 Posts: 2
|
Posted: Sat Mar 17, 2007 6:40 am Post subject: |
|
|
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?  |
|
| Back to top |
|
 |
M.Pruefer Newbie

Joined: 15 Mar 2007 Posts: 2
|
Posted: Tue Mar 20, 2007 3:07 am Post subject: |
|
|
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 |
|
 |
srees Newbie

Joined: 17 Oct 2009 Posts: 3
|
Posted: Sat Oct 17, 2009 9:03 am Post subject: |
|
|
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 |
|
 |
pir OOo Advocate


Joined: 20 May 2009 Posts: 200 Location: France
|
Posted: Mon Oct 19, 2009 12:44 am Post subject: |
|
|
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 |
|
 |
srees Newbie

Joined: 17 Oct 2009 Posts: 3
|
Posted: Mon Oct 19, 2009 10:27 pm Post subject: |
|
|
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 |
|
 |
srees Newbie

Joined: 17 Oct 2009 Posts: 3
|
Posted: Tue Oct 20, 2009 10:06 pm Post subject: |
|
|
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 |
|
 |
nvgishere General User

Joined: 10 Feb 2010 Posts: 38
|
Posted: Thu Mar 04, 2010 6:23 am Post subject: |
|
|
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 |
|
 |
|