Recover output from root macro in bash script

I have a root macro that returns an integer. File mymacro.C:
int mymacro() {return 3; }

How can I recover this output when I call it from a bash script? I tried this, but the variable remains empty:

#!/bin/bash
var=$(root -b -q "mymacro.C")
echo $var

Thanks in advance,

Maybe something like this (tested only in zsh):

root -b -q mymacro.C       # you don't need quotes here
var=$?
echo $var

yus’s code also worked for me in bash.
Here is another way: since the output line looks like this
(int) 3
you can look for this line and then print the 3 (second field in the line):

#!/bin/bash
var=$(root -l -b -q "mymacro.C" | grep "(int)" | gawk '{print $2}')
echo $var

Note that we are immediately filtering the output through grep, so in more complex examples you may see stuff you don’t want (if there are other (int) lines) or may not see other stuff you want (if there are other outputs), so you will have to modify accordingly.
Also note that, in either case, you can (but don’t need to) pass to root the “-l” option to avoid the ‘splash screen’.

Thank you, both worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.