0

I am completely new to linux, and I am trying to adjust the following script to make sure only PART 2 runs (without running PART 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


Can I make it run Part 2 automatically without needing to choose?

I am running the script through slurm and I am unable to see how it runs or choose an option, so I want part 2 to run automatically.

10
  • Pick option 2 from the menu when running the script? The PART 1 bit only runs if you pick option 1 from the menu. Commented May 30, 2022 at 13:05
  • 1
    Simple way: add a comment character (#) to the start of each line that you don't want to run. Commented May 30, 2022 at 13:05
  • replace [ $option -eq 1 ] under PART 1 by /bin/false (this will run part 2 only if you choose 1 in menu) Commented May 30, 2022 at 13:09
  • Can I make it run Part 2 automatically without needing to choose? (I am running the script through slurm and I am unable to see how it runs or choose an option, so I want part 2 to run automatically) Commented May 30, 2022 at 13:15
  • Can I make it run Part 2 automatically without needing to choose? (I am running the script through slurm and I am unable to see how it runs or choose an option, so I want part 2 to run automatically) Commented May 30, 2022 at 13:15

1 Answer 1

0

What I would do here is I would create a new script (text file) and copy only the parts that I want to run into it.

The export lines at the top sets up the variables for the rest of the script and may be used in multiple places. Copy all of them, or cherry pick the ones you want only. Extra ones will cause no harm.

Then copy part two of the script. You don't need the menu.

I've taken the liberty of changing the coding style to make it clearer what is what. Specifically I replaced ";" with a newline character and added proper indentation. Using ";" to string together commands is fine on the internactive shell prompt but serves little point beyond obfuscating the script in all but the simplest case.

At the end the text file will look like this:

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

Then make the file executable, eg

chmod +x my_part2_script

And finally run it using

./my_part2_script

2
  • Your code is, apart from missing the final fi, also using $option without setting the variable's value. Commented May 30, 2022 at 13:36
  • Thank you, I've fixed it! Commented May 30, 2022 at 13:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.