How to use shell from basic macro (solved)

Having trouble making the SHELL call work from basic macro. I have a bash file hsf.sh that runs gnuplot, to load a control file and plot a data file. It needs no other input or control. Andrew Pitonyak makes it look easy, but I cannot get the SHELL call to do anything useful.

sFileName = sLocation & "hsf.sh"
i = shell(sFileName)
print i

Exit code is 0 implying success, but nothing happens. I’ve also tried variations (with appropriate file names):

i = shell ("gnuplot",,sFileName)
i = shell("/usr/bin/gnuplot",1, sFileName,true)
i = shell ("bash", 1, sFileName,true)

Perhaps I should shell to the the terminal, but I’m not sure how to do that.

Any suggestions what I am doing wrong?

What do think should happen? … poor old basic isn’t able to plot something

No, indeed. I am trying to export results to gnuplot and run that from the macro.

If I read your question correctly, the following works on Mint 18:

Shell "bash -c 'sh /home/YOUR_DIRECTORY/TestSH.sh'"

Just a couple of other different Shell commands. This one opens the file in text editor:

Shell "bash -c 'xed /home/YOUR_DIRECTORY/TestSH.sh'"

Dictionary lookup through Firefox:

sString = "Hello"
Shell "firefox http://www.thefreedictionary.com/" & sString

Edit:

Sub used in Calc to run these commands:

Sub shellTest
    oSheet=ThisComponent.CurrentController.ActiveSheet
    Cell = oSheet.GetCellRangeByName("A1")
    ipAddr = Cell.getString

    'Works on Mint 18 to open root terminal'
    'Shell ("gksu /usr/bin/x-terminal-emulator",4)'

    'This line works - redirects output to .txt file'
    'Shell "bash -c 'xed /home/linuxnow/a/testSH.sh'"

    'Same as previous with a return code'
    x = Shell ("bash -c 'xed /home/linuxnow/a/testSH.sh'")
    MsgBox x
    '
    'This works on Mint 18'
    'Shell "bash -c 'sh /home/linuxnow/a/TestCurlSH.sh'"
    '
    'Works on Mint 18'
    sString = "Hello"
    Shell "firefox http://www.thefreedictionary.com/" & sString

    'Shell "firefox https://ask.libreoffice.org/c/english/5"'
end Sub

Also added another text edit showing Shell with return code.

1 Like

OK, but these are bash command lines. I am trying to use shell out of lo basic.

That is not true. These are straight out of a basic Sub. Each is a line in BASIC. The first should work with your hsf.sh mentioned in the question. My example actually contacts a weather service for a file download.

My sincere apologies,Ratslinger. I was not familiar with that form of the shell call, only shell(command …). I am about to try your suggestions, but wanted to correct my mistake first. I am working with LO 5.1 on Ubuntu 16.04. The macro is called from base, but makes no use of the datasource (yet).

Apology not necessary - I just got a bit ticked you didn’t even try it. BTW as in the sample, if you want a return code you must include the open & close parentheses Shell(command) otherwise you will get an error.

Thanks, Ratslinger.
The first produces ‘file not found’, but without gksu it opens the terminal. But I can’t pass a command to the terminal.
The second and third still won’t work.
So none of these achieve what I want.
shell “bash -c ‘sh " & sFileName & "’”
shell “bash -c '” & sFileName & “’”
i = shell ("/usr/bin/x-terminal-emulator “&sFileName, 1, , TRUE)’
i = shell (”/usr/bin/x-terminal-emulator ", 1, sFileName, TRUE)’
(sFileName is the full URL of my bash file)

Ok, just ran a test with variable.

    x = Shell ("bash -c 'sh " & sFileName & "'")
    MsgBox x

Correction - this also worked for me:

   Shell "bash -c 'sh " & sFileName & "'"

Just copied you statement (the one immediately above) and pasted into my Sub and it worked fine.

Have used this to run ping on Xubuntu but not on Mint 18. May need modifications:

Shell ("exo-open --launch TerminalEmulator ping -c 5 " & ipAddr & "")

It opened a terminal & ran ping.

Switched over to Ubuntu v16.04 (really, really, really don’t like) and my sh line wasn’t working. Some manipulations and this finally worked:

     x = Shell ("bash",1,sFileName,True)
     MsgBox x

Also, switch xed out for gedit for the text editing statement.

1 Like

Another note on the subject. If the URL is wrong in any way, the return code is still zero. This may be a possible problem on your end from the beginning. Just a guess since I can’t figure anything else. My working statement is identical to one of your originals. I found the URL thing because of switching systems and forgot to change it.

Aargh! Gotcha. My bash file called a gnuplot control file without specifying the url. Running the terminal in the local directory it worked fine. Calling the shell from basic it did not. ‘A possible problem from the beginning.’ Yes, but not that url. thanks for your trouble and sorry it was a wild goose chase. (I have also discovered notice that the terminal doesn’t find a bash script referred to by its full url (beginning file:///), but wants it to start /home.)

Glad you found your problem. Some education for myself so not a lost cause. Still only Xubuntu so far can send & execute commands directly to Terminal with exo-open. Trying to solve that one for my own knowledge to be usable in other environments.