Exec & Shell

Post your Gambas programming questions here.
Post Reply
User avatar
Quincunxian
Posts: 132
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Exec & Shell

Post by Quincunxian » Saturday 31st July 2021 4:41am

Greetings all.
I'm trying to run an Open office command line instruction using Exec or Shell.
It's not working, but if I run exactly the same command from the terminal, it works as required.
The process uses 'soffice' to convert a writer document (.odt) file to a txt file.

Terminal command:
soffice --headless --convert-to txt "{path}/myfile.odt"
Exec["soffice","--headless","--convert-to","txt","{path}/myfile.odt"] Wait

Shell("soffice --headless --convert-to txt " & chr(34) & "{path}/myfile.odt" & chr(34)) Wait
The quote characters are required as the path has a comma in it - not ideal, but it is what it is.
Again, this works from the command line but not from Gambas.

Any thoughts would be appreciated.

I've also use to "To" parameter to see if there is a resultant error message, but in both cases it returns nothing.

Useful fact: You can use the same command with html instead of txt to convert a odt document to html.
Cheers - Quin.
I code therefore I am

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: Exec & Shell

Post by BruceSteers » Saturday 31st July 2021 8:57am

I find glitches where my usual shell is bash but gambas uses sh by default.

I start my progs with ...
System.Shell = System.Find("bash")
I find this has fixed many a glitch.


Also what's with Chr(34) ?
Will this not work ?
  Shell("soffice --headless --convert-to txt '{path}/myfile.odt'" Wait
Or...
  Shell("soffice --headless --convert-to txt \"{path}/myfile.odt\"" Wait

hmm what's that {path} ? should that not be some gambas code providing an actual path?

did you try ..
  Debug Quote("soffice --headless --convert-to txt " & chr(34) & "{path}/myfile.odt" & chr(34))
just to check the command line looks right, valid paths n all?
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 756
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Exec & Shell

Post by cogier » Saturday 31st July 2021 9:07am

I have used this tool using LibreOffice to convert .ods spreadsheet to a CSV file using this code:-
Shell "cd " & sFolder & " && " & "libreoffice --headless --convert-to csv temp.ods" Wait
The following works to change an .odt file with a silly name into a text file: -
 Public Sub Form_Open()

  Dim sFolder As String = User.Home &/ "Documents"

  Shell "cd " & sFolder & " && " & "libreoffice --headless --convert-to txt " & Chr(34) & sFolder &/ "A very, very silly file name.odt" & Chr(34) Wait

End
The changing of the folder at the beginning of the Shell command will create the new file in the same folder, you can remove it if you are happy for the file to be created in your Home folder.

I suggest your code checks that you have created the new file and if not pops up a warning to close LibreOffice (OpenOffice) as you can't do this conversion if LibreOffice (OpenOffice) is open

Image

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: Exec & Shell

Post by BruceSteers » Saturday 31st July 2021 9:22am

Another possible solution (without seeing your code)...

If {path} is an Env variable the shell is looking for you can supply it with "With"
This could be one way to do it..

Shell("soffice --headless --convert-to txt \"$path/myfile.odt\"" With ["path=" & sMyPathString] Wait

Or something like that :)
Last edited by BruceSteers on Saturday 31st July 2021 9:43am, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: Exec & Shell

Post by BruceSteers » Saturday 31st July 2021 9:41am

Also consider this code......
shell must have $ char to access variables

Shell "echo \"this is {path}\"" With ["path=hello there"] Wait

Shell "echo \"this is ${path}\"" With ["path=hello there"] Wait

Prints...
this is {path}
this is hello there

Doing functions on strings in bash is the only time i use {} for just the path text you'd use $path using ${path} makes sense when perfoming action like mid string functions like ${path##*/}

Like this ..(gets path part and filename part)...
Shell "echo \"this is $path\"\necho \"this is ${path%/*}\"\necho \"this is ${path##*/}\"" With ["path=/my/path/to/file.txt"] Wait
Prints..
this is /my/path/to/file.txt
this is /my/path/to
this is file.txt


thought it worth a mention
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
Quincunxian
Posts: 132
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Exec & Shell

Post by Quincunxian » Saturday 31st July 2021 9:55pm

Thanks guys.
@ Bruce - The Gambas Quote function has failed on every SQL statement I've used it on so far, so my confidence is somewhat lacking.
I'll have another look to cover all bases, but I'm not confident.

@Cogier. I'm using LibreOffice but have always used the soffice application for command line stuff.
I found this: libreoffice is a shell script that sets up the environment and passes the command line arguments to the soffice.bin binary. Here

So I might play around with using the command libreoffice instead of soffice.

Also, you can do multiple files at the same time by using libreoffice --headless --convert-to txt {path}/*.odt
This will run the conversion process against all .odt files in the target folder.

Gut feel is that the folder and file name both having a apostrophe in them is the causing the failure. Lesson learnt about doing that again !.
Cheers - Quin.
I code therefore I am

Post Reply