0

I am trying to execute three tasks in one bash script.

The coding I did is :

#!/bin/bash

(cd TRAJ_OctylGlcTryp_C1/
&&
cpptraj zOctylgluTryC1.prmtop << EOF
trajin reImaged-OctylgluTryC1.nc 1 70000 500
trajout reImaged-OctylgluTryC1-500.nc netcdf
EOF 
&& 
cd ../)

Which means, first go into directory TRAJ_OctylGlcTryp_C1 and select few frames of simulation data and finally come out of the folder.

But I get error like this

./run_Select500Frames.sh: line 4: syntax error near unexpected token `&&'
./run_Select500Frames.sh: line 4: `&& '

Is there any way to get rid of this error? Thanks.

Vijay
  • 965
  • 5
  • 13
  • 27

1 Answers1

2

You can write it more simply this way:

#!/bin/bash

set -eu

pushd TRAJ_OctylGlcTryp_C1/ > /dev/null

cpptraj zOctylgluTryC1.prmtop << EOF
trajin reImaged-OctylgluTryC1.nc 1 70000 500
trajout reImaged-OctylgluTryC1-500.nc netcdf
EOF

popd > /dev/null

I recommend always using set -eu at the top of new Bash scripts. This way the script will stop by itself if a command fails. From there, I choose to use pushd and popd as a slightly more reliable way of restoring the old working directory at the end, and the rest is easy. No more && at all.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I tried the above script but I get error message: 'EOF': Command not found. – Vijay Jan 09 '15 at 07:49
  • 1
    Make sure you don't have extra whitespace around the EOFs. I did have one extra space in my answer which I've just removed. The code works for me. – John Zwinck Jan 09 '15 at 07:54