1
#!/bin/sh

init=step5_input
rest_prefix=step5_input
mini_prefix=step6.0_minimization
equi_prefix=step6.%d_equilibration
prod_prefix=step7_production
prod_step=step7


# Minimization
gmx grompp -f ${mini_prefix}.mdp -o ${mini_prefix}.tpr -c ${init}.gro -r ${rest_prefix}.gro -p topol.top -n index.ndx
gmx mdrun -v -deffnm ${mini_prefix}

# Equilibration
cnt=1
cntmax=6

while [ ${cnt} <= ${cntmax} ]
    @ pcnt = ${cnt} - 1
    istep=`printf ${equi_prefix} ${cnt}`
    pstep= `printf ${equi_prefix} ${pcnt}`
    if [ ${cnt} == 1 ] then pstep=${mini_prefix}

    gmx grompp -f ${istep}.mdp -o ${istep}.tpr -c ${pstep}.gro -r ${rest_prefix}.gro -p topol.top -n index.ndx
    gmx mdrun -v -deffnm ${istep}
    @ cnt += 1
end

# Production
cnt=1
cntmax=10

while [ ${cnt} <= ${cntmax} ]
    @ pcnt = ${cnt} - 1
    istep=${prod_step}_${cnt}
    pstep=${prod_step}_${pcnt}

    if [ ${cnt} == 1 ] then
        pstep=`printf ${equi_prefix} 6`
        gmx grompp -f ${prod_prefix}.mdp -o ${istep}.tpr -c ${pstep}.gro -p topol.top -n index.ndx
    else
        gmx grompp -f ${prod_prefix}.mdp -o ${istep}.tpr -c ${pstep}.gro -t ${pstep}.cpt -p topol.top -n index.ndx
    endif
    gmx mdrun -v -deffnm ${istep}
    @ cnt += 1
end
9
  • I'm not very familiarized with csh/tcsh but to assign variables in that shell it should be with set varname = value (If I use your syntax I get an error: command not found). Are you sure you are using csh? Commented Nov 5, 2022 at 1:51
  • Thanks for the clarification. Where and why are you using @ ? I'm not sure if in csh it's valid to use @ as you have. The @ is valid to get all the values of arguments you pass to a shell script. Commented Nov 5, 2022 at 2:03
  • I'm not sure if "@" can be used in csh. However, do you want to convert to sh or bash? The title says bash but the shebang says sh (/bin/sh) ? Commented Nov 5, 2022 at 2:19
  • About the if statement, this must have fi to close the condition. With your code I'm not sure where the if statements must be closed. Can you provide more detailed code? Because ${equi_prefix} I don't know what means but it seems that the printf is redundant in your code. Commented Nov 5, 2022 at 2:23
  • I have updated the codes!
    – pkoduro
    Commented Nov 5, 2022 at 2:31

1 Answer 1

0

Your code contains several mistakes. The @ I'm not sure what means in csh or if this one is valid.
There are some differences in the syntax between the different shells so you should take a look at the syntax used in sh, bash, zsh, dash, etc.

This is a valid syntax for your sh script:

#!/bin/sh

init=step5_input
rest_prefix=step5_input
mini_prefix=step6.0_minimization
equi_prefix=step6.%d_equilibration
prod_prefix=step7_production
prod_step=step7


# Minimization
gmx grompp -f ${mini_prefix}.mdp -o ${mini_prefix}.tpr -c ${init}.gro -r ${rest_prefix}.gro -p topol.top -n index.ndx
gmx mdrun -v -deffnm ${mini_prefix}

# Equilibration
cnt=1
cntmax=6

while [ ${cnt} -le ${cntmax} ]; do
    pcnt=$[ cnt - 1 ]
    istep=$(printf ${equi_prefix} ${cnt})
    pstep=$(printf ${equi_prefix} ${pcnt})

    #One liner if statement:
    if [ ${cnt} == 1 ]; then pstep=${mini_prefix}; fi

    gmx grompp -f ${istep}.mdp -o ${istep}.tpr -c ${pstep}.gro -r ${rest_prefix}.gro -p topol.top -n index.ndx
    gmx mdrun -v -deffnm ${istep}
    ((cnt += 1))

done

# Production
cnt=1
cntmax=10

while [ ${cnt} -le ${cntmax} ]; do
    pcnt=$(( cnt - 1))
    istep=${prod_step}_${cnt}
    pstep=${prod_step}_${pcnt}

    if [ ${cnt} -eq 1 ]; then
       pstep=$(printf ${equi_prefix} 6)
       gmx grompp -f ${prod_prefix}.mdp -o ${istep}.tpr -c ${pstep}.gro -p topol.top -n index.ndx
    else
        gmx grompp -f ${prod_prefix}.mdp -o ${istep}.tpr -c ${pstep}.gro -t ${pstep}.cpt -p topol.top -n index.ndx
    fi
    gmx mdrun -v -deffnm ${istep}
    cnt=$[ cnt += 1 ]
 done

While
A valid syntax for a while statement should be:

while [ expr ]; do
 code
done

#Or
while [ expr ]
do
 code
done

So if you want to compare number you should use:

  1. -le means little or equals than
  2. -eq means equals
  3. -lt means little than
  4. -ge means great or equals than
  5. -gt means great than

If
A valid syntax for a if statement should be:

if [ expr ]; then
 code
fi

#or

if [ expr ]
then
 code
fi

The expr to compare numbers is the same as the while expr.

Math operations
You have several ways to use math operations in your code, in this script I've used two ways:

sum=$[ expr ]
#E.g.
pcnt=$[ cnt - 1 ]

And

sum=$(( expr ))
#E.g.
pcnt=$(( cnt - 1))

#or if you are not assign a value:
((expr))
#E.g.
((cnt += 1))

Setting variables
When you are setting some variable you must be careful with the spaces, if you run:

val = 10

That will cause an error: command not found. So the valid syntax is:

val=10
val='something'
val="something"
val=$(command)
val="$(command)"
val=`command`
val="`command`"

And about run commands to assign their output to a variable I see you have:

istep=`printf ${equi_prefix} ${cnt}`

Instead of using val=`command` you should use val=$(command), which is more recommendable:

istep=$(printf ${equi_prefix} ${cnt})

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .