我对 Linux 完全陌生,我正在尝试调整以下脚本以确保仅运行第 2 部分(而不运行第 1 部分):
fail () {
echo Execution aborted.
read -n1 -r -p "Press any key to continue..." key
exit 1
}
# "name" and "dirout" are named according to the testcase
export name=case
export dirout=${name}_out
export diroutdata=${dirout}/data
# "executables" are renamed and called from their directory
export dirbin=$HOME/DualSPHysics/bin/linux
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${dirbin}
export gencase="${dirbin}/GenCase_linux64"
export dualsphysicscpu="${dirbin}/DualSPHysics5.0CPU_linux64"
export dualsphysicsgpu="${dirbin}/DualSPHysics5.0_linux64"
export boundaryvtk="${dirbin}/BoundaryVTK_linux64"
export partvtk="${dirbin}/PartVTK_linux64"
export partvtkout="${dirbin}/PartVTKOut_linux64"
export measuretool="${dirbin}/MeasureTool_linux64"
export computeforces="${dirbin}/ComputeForces_linux64"
export isosurface="${dirbin}/IsoSurface_linux64"
export flowtool="${dirbin}/FlowTool_linux64"
export floatinginfo="${dirbin}/FloatingInfo_linux64"
option=-1
if [ -e $dirout ]; then
while [ "$option" != 1 -a "$option" != 2 -a "$option" != 3 ]
do
echo -e "The folder "${dirout}" already exists. Choose an option.
[1]- Delete it and continue.
[2]- Execute post-processing.
[3]- Abort and exit.
"
read -n 1 option
done
else
option=1
fi
# PART 1
if [ $option -eq 1 ]; then
# "dirout" to store results is removed if it already exists
if [ -e ${dirout} ]; then rm -r ${dirout}; fi
# CODES are executed according the selected parameters of execution in this testcase
${gencase} ${name}_Def ${dirout}/${name} -save:all
if [ $? -ne 0 ] ; then fail; fi
${dualsphysicsgpu} -gpu ${dirout}/${name} ${dirout} -dirdataout data -svres
if [ $? -ne 0 ] ; then fail; fi
fi
# PART 2
if [ $option -eq 2 -o $option -eq 1 ]; then
export dirout2=${dirout}/particles
${partvtk} -dirin ${diroutdata} -savevtk ${dirout2}/PartFluid -onlytype:-all,fluid -vars:+idp,+vel,+rhop,+press,+vor
if [ $? -ne 0 ] ; then fail; fi
${partvtk} -dirin ${diroutdata} -savevtk ${dirout2}/PartSquare -onlytype:-all,moving
if [ $? -ne 0 ] ; then fail; fi
${partvtkout} -dirin ${diroutdata} -savevtk ${dirout2}/PartFluidOut -SaveResume ${dirout2}/_ResumeFluidOut
if [ $? -ne 0 ] ; then fail; fi
export dirout2=${dirout}/MeasureElevation
${measuretool} -dirin ${diroutdata} -filexml ${dirout}/case.xml -savecsv ${dirout2}/MeasureSWL -points /scratch/gpfs/hse/DualSPHysics/IASS/points.txt -vars:-all -height
if [ $? -ne 0 ] ; then fail; fi
fi
if [ $option != 3 ];then
echo All done
else
echo Execution aborted
fi
read -n1 -r -p "Press any key to continue..." key
我可以让它自动运行第 2 部分而不需要选择吗?
我通过 slurm 运行脚本,但无法看到它如何运行或选择选项,因此我希望第 2 部分自动运行。
答案1
我在这里要做的是创建一个新脚本(文本文件)并仅复制我想要运行的部分。
顶部的导出行为脚本的其余部分设置变量,并且可以在多个地方使用。复制所有这些,或者只挑选您想要的。额外的不会造成任何伤害。
然后复制脚本的第二部分。你不需要菜单。
我冒昧地改变了编码风格,以使其更清楚什么是什么。具体来说,我替换了“;”带有换行符并添加了适当的缩进。使用 ”;”在交互式 shell 提示符下将命令串在一起很好,但除了在最简单的情况下混淆脚本之外,没有什么意义。
最后,文本文件将如下所示:
fail () {
echo Execution aborted.
read -n1 -r -p "Press any key to continue..." key
exit 1
}
# "name" and "dirout" are named according to the testcase
export name=case
export dirout=${name}_out
export diroutdata=${dirout}/data
# "executables" are renamed and called from their directory
export dirbin=$HOME/DualSPHysics/bin/linux
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${dirbin}
export gencase="${dirbin}/GenCase_linux64"
export dualsphysicscpu="${dirbin}/DualSPHysics5.0CPU_linux64"
export dualsphysicsgpu="${dirbin}/DualSPHysics5.0_linux64"
export boundaryvtk="${dirbin}/BoundaryVTK_linux64"
export partvtk="${dirbin}/PartVTK_linux64"
export partvtkout="${dirbin}/PartVTKOut_linux64"
export measuretool="${dirbin}/MeasureTool_linux64"
export computeforces="${dirbin}/ComputeForces_linux64"
export isosurface="${dirbin}/IsoSurface_linux64"
export flowtool="${dirbin}/FlowTool_linux64"
export floatinginfo="${dirbin}/FloatingInfo_linux64"
# PART 2
export dirout2=${dirout}/particles
${partvtk} -dirin ${diroutdata} -savevtk ${dirout2}/PartFluid -onlytype:-all,fluid -vars:+idp,+vel,+rhop,+press,+vor
if [ $? -ne 0 ]
then
fail
fi
${partvtk} -dirin ${diroutdata} -savevtk ${dirout2}/PartSquare -onlytype:-all,moving
if [ $? -ne 0 ]
then
fail
fi
${partvtkout} -dirin ${diroutdata} -savevtk ${dirout2}/PartFluidOut -SaveResume ${dirout2}/_ResumeFluidOut
if [ $? -ne 0 ]
then
fail
fi
export dirout2=${dirout}/MeasureElevation
${measuretool} -dirin ${diroutdata} -filexml ${dirout}/case.xml -savecsv ${dirout2}/MeasureSWL -points /scratch/gpfs/hse/DualSPHysics/IASS/points.txt -vars:-all -height
if [ $? -ne 0 ]
then
fail
fi
然后使文件可执行,例如
chmod +x my_part2_script
最后使用运行它
./my_part2_script