Ask Your Question
0

[Solved] Run bash script in BASIC macro [closed]

asked 2019-07-18 18:13:27 +0200

geny gravatar image

updated 2019-07-21 09:11:16 +0200

Hello, I'm a novice in Basic macro language. I can't run a bash script from a macro on Manjaro.

The macro should launch a nodejs script and open a console window. In the konsole (kde), the command 'bash run.sh' works like a charm.

I tried:

Shell "bash -c '/home/geny/Testing/run.sh'"

and

Shell "bash 'home/geny/Testing/run.sh'"

and

x = Shell("/home/geny/Testing/run.sh")

and

Shell("/home/geny/Testing/run.sh", 1,, "true")

(in the last command, x return 0), but nothing works. I mean, the script isn't executed.

The script 'run.sh' consists in only two rows:

#!/bin/bash
node "home/geny/Testing/myapp.js"

What am I doing wrong? Thanks in advance.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by erAck
close date 2019-08-01 12:29:55.444894

Comments

node "home/geny/Testing/myapp.js"

looks for me missing a leading "/" and I'd expect node /home/geny/Testing/myapp.js and second thing is about the environment instantiated by Shell(), which is not quite clear to me and therefore I made it a personal practice to always use full paths to programs (here: <full_path_to_node_command>)

Opaque gravatar imageOpaque ( 2019-07-18 23:15:54 +0200 )edit

I tried to add /home, but the result is always no execution. I added the full path (bin/node), but no results. In the konsole, command works.

geny gravatar imagegeny ( 2019-07-19 15:54:27 +0200 )edit

bin/node

? - This is not a full path.

Opaque gravatar imageOpaque ( 2019-07-19 16:07:39 +0200 )edit

The line

"/bin/node" "/home/geny/Testing/run.sh"

works if I run this command manually in the konsole.

Node is installed in /bin and there is only a file named "node" 21,3 MB. I retrieved the path with

'which node'
geny gravatar imagegeny ( 2019-07-19 16:22:53 +0200 )edit

Sorry - but you wrote bin/node instead of /bin/node in your comment before your last comment. Read once more. So I need to assume a typo and I do not doubt, that /bin/node works on the command line.

Opaque gravatar imageOpaque ( 2019-07-19 16:31:29 +0200 )edit

No problem, Opaque! :-) Yes, it was a typo.

And... in my VBA macro, the macro launchs a file batch into a prompt window and then, when finished, closed it. Can I do it in LibreOffice? I mean, Shell() can open a console instance and run the bash script?

geny gravatar imagegeny ( 2019-07-19 17:01:07 +0200 )edit

In your description you wrote

bash run.sh works like a charm.

Are you aware that this work even if run.sh is not executable since it is beind sourced into bash, while your call from within macro's Shell() requires run.sh to have the executable flag? Please check in a terminal just to use

user@system: /home/geny/Testing/run.sh

If this throws something like "permission denied" execute: chmod 755 /home/geny/Testing/run.sh

Opaque gravatar imageOpaque ( 2019-07-19 17:25:03 +0200 )edit

Yes, I already did it. It is executable, and the command:

/home/geny/Testing/run.sh

run the node program perfectly.

geny gravatar imagegeny ( 2019-07-19 17:39:33 +0200 )edit

Ok - I have just installed everything on a Manjaro test system and I just can tell you that it works for me, using:

Macro

Sub NodeBash
  Shell("/tmp/home/geny/Testing/run.sh")
End Sub

Bash Script

user@system: cat run.sh
#!/bin/bash
/bin/node /tmp/home/geny/Testing/myapp.js >>/tmp/home/geny/Testing/run.sh.log

JS Hello World

user@system: cat myapp.js
console.log("Hello World!");

So I'm left with at least 2 questions:

  1. Is your verson of myapp.js somehow interactive?
  2. How would you determine a successful run run.sh or better of node myapp.js?
Opaque gravatar imageOpaque ( 2019-07-19 17:48:49 +0200 )edit

Ok, it quite works. Thanks Opaque. And yes, myapp.js is interactive.

Works but it stops at the first few lines. If I run manually the script sh, it works until to the end. It seems that the macro can't wait the execution.

I say it because, if I run the macro on debug mode, pressing F8, the blink curson exit from the Shell(...) line very fast, and doesn't wait as expected. But it is only my assumption.

Then, second problem is that my app shows many console.log commands, but I can see them only on log file.

I would determine a successful when the app retrieve a table with chrome headless and save it on the a xls file. Then, it insert the file on the active ods file.

I think I'm asking to Basic too much.

Maybe can I use a macro python? I ...(more)

geny gravatar imagegeny ( 2019-07-19 18:26:59 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-07-18 21:26:39 +0200

erAck gravatar image
x = Shell("/home/geny/Testing/run.sh")

should work, specifically if x is 0 after that then run.sh was executed. Why the call to your node program doesn't succeed is a different problem. Try with run.sh containing

#!/bin/bash
echo 'I was here!' $(date) >>/home/geny/Testing/testdata.dat

testdata.dat then should contain a corresponding line.

edit flag offensive delete link more

Comments

Ok, erAck, testdata.dat contain the running date and hour. So Shell works, but I can't know why the node command doesn't. I tried to change node with the full path of node (/bin/node), but nothing works. In the konsole, manaully, the command works.

And, I see that your line doesn't open the console. Is there a way to open the konsole and execute the commands?

geny gravatar imagegeny ( 2019-07-19 16:03:22 +0200 )edit

Shell() is not supposed to open a terminal window, it executes an executable. If you want to open a terminal window then specify so, for example

x = Shell("/usr/bin/gnome-terminal -- /home/geny/Testing/run.sh")

and in run.sh to see some terminal output

#!/bin/bash
echo 'I was here!' "$(date)" | tee -a /home/geny/Testing/testdata.dat
sleep 5
echo 'I was here again!' "$(date)" | tee -a /home/geny/Testing/testdata.dat
sleep 5

Or have run.sh open a terminal instead with its script parameters, YMMV..

erAck gravatar imageerAck ( 2019-07-19 19:54:02 +0200 )edit

Oh, I understand now,. Tomorrow I will try your solution and will write here the results.

Thanks for your assistance.

geny gravatar imagegeny ( 2019-07-19 20:39:06 +0200 )edit

Ok, erAck, we are nearly there. This command works on KDE:

x = Shell("/usr/bin/konsole --hold -e /home/geny/Testing/run.sh"

It opened a console, but immediately closed. Then, I add --hold to hold the window opened after the execution.

An error appeared from nodejs:

Error: Failed to launch chrome!
[0720/160345.963509:FATAL:zygote_host_impl_linux.cc(116)] No usable sandbox!

I know this error. To fix it I already had to setup a setuid sandbox (see this page) but I suppose the konsole opened from the macro doesn't read the exported CHROME_DEVEL_SANDBOX env variable in /home/.bashrc, or something similar. Anyway, the .bashrc already contain this line:

export CHROME_DEVEL_SANDBOX=/usr/local/sbin/chrome-devel-sandbox

Any idea?

geny gravatar imagegeny ( 2019-07-20 16:29:44 +0200 )edit

.bashrc is read only for interactive shells. man bash, search for INVOCATION. Also, I think this isn't a LibreOffice problem anymore..

erAck gravatar imageerAck ( 2019-07-20 21:41:09 +0200 )edit

Thanks, erAck! Your info is very useful.

geny gravatar imagegeny ( 2019-07-21 09:08:35 +0200 )edit

Only for future reference, I solved the issue add -i to run.sh script:

#!/bin/bash -i
/bin/node /home/geny/Testing/myapp.js
exit

This run the bash konsole as interactive.

geny gravatar imagegeny ( 2019-07-21 13:08:40 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2019-07-18 18:13:27 +0200

Seen: 54 times

Last updated: Jul 21