# ROOT commands from bash script

**URL:** <https://root-forum.cern.ch/t/root-commands-from-bash-script/27572>\
**Category:** ROOT\
**Created:** [January 16, 2018, 1:50pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572 "2018-01-16T13:50:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [January 16, 2018, 1:50pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/1 "2018-01-16T13:50:24Z")

</div>

Hi, I want to write a bash script that runs some root commands. The script is

```auto
#!/bin/bash

for i in `seq 1 235`;
        do
        root -b -q 
        .x masses_wrong_ratio_opt.C("/home/george/Desktop/wrong_weights/output/benchmark_ttH_1.dat.out") # runs the macro script that creates the root files
        .q # exits root    
        mv ratio_wrng.root ratio_wrng_$i.root # changes the name of the root files
done 

```

but it doesn’t seem to work. the output is

```auto
./wrong_ratio.sh: line 6: syntax error near unexpected token `('
./wrong_ratio.sh: line 6: `	.x masses_wrong_ratio_opt_C("/home/george/Desktop/wrong_weights/output/benchmark_ttH_'$i'.dat.out") # runs the macro script that creates the root files'

```

Anyone knows how to fix it?

Chees  
George

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [January 16, 2018, 1:59pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/2 "2018-01-16T13:59:52Z")

</div>

The script below should print ROOT’s help. You can modify it to do what you want.

```auto
#!/bin/bash

root -l <<-EOF
.help
.q
EOF

```

Basically, you need to use `<<-EOF` for the next lines to be used as input to ROOT, and then `EOF` when you are finished with ROOT commands.

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [January 16, 2018, 2:07pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/3 "2018-01-16T14:07:22Z")

</div>

You don’t need .x, just use:

```auto
for i in {1..235}; do # in bash, you dont need to call seq
  root -b -q masses_wrong_ratio_opt.C'("/home/george/Desktop/wrong_weights/output/benchmark_ttH_1.dat.out")'
done

```

The trick is to quote the parentheses and the double quotes as well. Easiest done using single quotes 🙂

It gets a bit messy if you want to use i instead of 1 in the filename:  
`root -b -q masses_wrong_ratio_opt.C'("/home/george/Desktop/wrong_weights/output/benchmark_ttH_'${i}'.dat.out")'`

Complely untested 🙂

Also, consider using `root -b -q masses_wrong_ratio_opt.C+'("/home/geo ...` to compile the code.

---

<div class="post-metadata">

**Author:** ![Geo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/geo/32/15552_2.png) [@Geo](https://root-forum.cern.ch/u/Geo)\
**Post date:** [January 16, 2018, 2:12pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/4 "2018-01-16T14:12:27Z")

</div>

But how will the argument of the macro change if I don’t use i? I want each time to be "/home/george/Desktop/wrong\_weights/output/benchmark\_ttH\_ΝUMBER.dat.out"  
where NUMBER to span from 1 to 235… and then to exit root…

Thank you

---

<div class="post-metadata">

**Author:** ![adrian\_sev](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/adrian_sev/32/13169_2.png) [@adrian\_sev](https://root-forum.cern.ch/u/adrian_sev)\
**Post date:** [January 16, 2018, 2:25pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/5 "2018-01-16T14:25:38Z")

</div>

you can use with the escaping of " and ( :  
`root.exe -l -b -q EmcalJetCDF.C\(\"local\",\"test\",\"data.txt\"\)`

if you want to start more similar processes is better to have separate subdirs in which you will have the input,  
a generic named symlink to input data and the output file  
if the output file is root you can do hadd of the results at the end

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [January 30, 2018, 2:25pm UTC](https://root-forum.cern.ch/t/root-commands-from-bash-script/27572/6 "2018-01-30T14:25:50Z")

</div>

Closed on January 30, 2018, 2:25pm UTC

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