如何更正 sh 脚本的这些行以避免“语法错误:意外的文件结尾”

如何更正 sh 脚本的这些行以避免“语法错误:意外的文件结尾”
#!/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

答案1

您的代码包含多个错误。我@不确定csh这是什么意思或者是否有效。
不同 shell 之间的语法存在一些差异,因此您应该查看shbashzshdash等中使用的语法。

这是您的脚本的有效语法sh

#!/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应该是:

while [ expr ]; do
 code
done

#Or
while [ expr ]
do
 code
done

因此,如果您想比较数字,您应该使用:

  1. -le表示小于或等于
  2. -eq意味着等于
  3. -lt意思是小于
  4. -ge表示大于或等于
  5. -gt意思是大于

如果
语句的有效语法if应该是:

if [ expr ]; then
 code
fi

#or

if [ expr ]
then
 code
fi

比较expr数字与 expr 相同while

数学运算
您可以通过多种方式在代码中使用数学运算,在此脚本中我使用了两种方式:

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

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

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

设置变量
当您设置某些变量时,如果运行以下命令,则必须小心空格:

val = 10

这会导致错误:找不到命令。所以有效的语法是:

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

关于运行命令将其输出分配给变量,我看到您有:

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

而不是使用值=`command`你应该使用val=$(command),这是更推荐的:

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

相关内容