3

i want to open a shell of a different program from the bash script (specifically root - a software for physicists) and execute several commands in a row.

I know to enter one command:

echo ".L mymacro.C" | root -l  

but i need to enter several commands one by one without closing the root shell (root is not a root user, but an interactive shell for a different program)

I have tried with the parentheses, but it didn't succeed:

(echo ".L mymacro.C"; echo "myClass a";echo "a.Loop") | root -l

I need those 3 commands entered in a root shell one by one:

mymacro.C
myClass a
a.Loop

How could i do this from a bash script?

Thank you very much.

4 Answers 4

3

Perhaps a here document might work:

$ root -l <<EOF
.L mymacro.C
myClass a
a.Loop
EOF
Sign up to request clarification or add additional context in comments.

3 Comments

Definitely better than my answer, I like how you added a link to Wikipedia's here document page.
@ShellFish: thanks. I'm not sure that this is actually correct because, if it does work, the OP's bracketed expression (echo ".L mymacro.C"; echo "myClass a";echo "a.Loop") | root -l should also work.
Good point, I'll guess we'll have to wait for the OP to reply then! Although we know from experience that this can take a while.
1

Can you not simply do:

echo -e ".L mymacro.C\nmyClass a\na.Loop" | root -l

This will send the data line by line to the root shell.

Or if you really want to do it one by one, you can always loop as follows:

Code removed thanks to mhawke's remark

2 Comments

Note that the second example might not work because root is started on each iteration and processes just one command. From the look of the commands, the first loads a macro, and the following commands might depend on that.
Good point mhawke, I should have been more careful here. I will update immediately, thank you!
0

Looks like you need to enclose with curly brackets. Try like this:

cat <<'EOF' | root -l
{
.L mymacro.C
myClass a
a.Loop
}
EOF

Comments

0

thanks for the answers, but unfortunately none of it worked for me. What worked was to create another file run.cxx with:

{
gROOT->ProcessLine(".L AnalyseSimple.C");
AnalyseSimple a;
a.Loop();
gROOT->ProcessLine(".q");
}

and to add root -l run.cxx to the bash script

1 Comment

Accept your own answer if it helped, it's ok - even advised - to do so!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.