0

I am brand new at bash script. I want to write a script using forloop which will give me the following line and execute also these lines.

gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 1000 -e 10000 -skip 10 -o 1.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 2000 -e 20000 -skip 10 -o 2.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 3000 -e 30000 -skip 10 -o 3.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 4000 -e 40000 -skip 10 -o 4.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 5000 -e 50000 -skip 10 -o 5.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 6000 -e 60000 -skip 10 -o 6.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 7000 -e 70000 -skip 10 -o 7.out

....

2
  • Your question ins't really clear. Do you want a different script to execute each command and call that from within another script? Please clarify. Commented Jul 3, 2017 at 19:59
  • I want the script that will create the that command and execute that command Commented Jul 3, 2017 at 21:33

3 Answers 3

4

You can also save a couple of lines of typing and get them all done in parallel with GNU Parallel:

parallel gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b {}000 -e {}0000 -skip 10 -o {}.out ::: {1..7}

Use parallel --dry-run if you want to see what it would do but without actually doing anything.

Sign up to request clarification or add additional context in comments.

4 Comments

@DavidC.Rankin You know it makes sense - especially as CPUs continue to get wider (more cores) rather than taller (more GHz).... :-)
Very nice command! parallel isn't shipped with Ubuntu though
@DavidC.Rankin May I suggest spending an hour on man parallel_tutorial?
Yes, will do. I can see it being beneficial in large builds for those packages that can be built in parallel (peripheral packages, etc.)
1

You can use a C-style loop as well, e.g.

for ((i = 1; i < 8; i++)); do
    echo "gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b $((i * 1000)) -e $((i * 10000)) -skip 10 -o $i.out"
done

Example Use/Output

$ for ((i = 1; i < 8; i++)); do
>     echo "gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b $((i * 1000)) -e $((i * 10000)) -skip 10 -o $i.out"
> done
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 1000 -e 10000 -skip 10 -o 1.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 2000 -e 20000 -skip 10 -o 2.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 3000 -e 30000 -skip 10 -o 3.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 4000 -e 40000 -skip 10 -o 4.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 5000 -e 50000 -skip 10 -o 5.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 6000 -e 60000 -skip 10 -o 6.out
gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b 7000 -e 70000 -skip 10 -o 7.out

2 Comments

Dear Sir, it gives me the Syntax error: Bad for loop variable
Hmm, are you using i somewhere else as a variable? That's the only thing I can think of. Obviously i is not a Bad for loop variable... Make sure you have no typo. (you can just copy/paste the code right into an xterm to run it -- it works -- no error... (make sure you copy the top code without the > (those are just terminal line continuation marks (actually your PS3 prompt) that the terminal outputs when entering multi-line input)
1

Using a basic bash for loop:

for i in {1..7}; do
  gmx trjconv -f md_0_1.xtc -s md_0_1.tpr -b ${i}000 -e ${i}0000 -skip 10 -o $i.out
done

3 Comments

You need to multiply your -b and -e values by $i. (e.g. -b $((i * 1000)))
I didn't see that, thanks @DavidC.Rankin. You don't have to multiply though, ${i}000 does the trick :)
Sure, that's another creative way to solve the problem. I'll give you a vote on that one :)

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.