Vijay
Vijay

Reputation: 985

multiple task in one bash script

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.

Upvotes: 0

Views: 340

Answers (1)

John Zwinck
John Zwinck

Reputation: 249394

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.

Upvotes: 2

Related Questions