Issue
I created a bash script that manages multiple related python scripts based on specific conditions (i.e., run this script while file has < x number of lines, run a different script if..., etc.). I am using entry points to execute these python scripts within the bash script.
When validating the bash script I found some edge cases where all the conditions fail, resulting in a sys.exit(load_entry_point('Program', 'console_scripts', 'iter')())
and ValueError: Nothing was generated
(designated within the python program to be raised if nothing happens).
After looking on SO, I tried something like this within some of my conditionals:
if [ $? -ne 0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
I continue to get EXIT 0
, suggesting no errors are being detected. What I am not understanding here?
For clarity, my goal is to detect any errors thrown by the invoked python scripts to have the bash script respond accordingly.
Here is an idea of what the bash script is like:
Iter=1
while [ $nr_lines -lt 17 ]; do
if [ $Iter -eq 5 ]; then
echo ''
echo 'Relax parameter. Iteration:' $Iter
relax -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
else
echo ''
echo 'Iteration:' $Iter
iter -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
fi
done
I have tried putting the conditional for error capturing here:
Iter=1
while [ $nr_lines -lt 17 ]; do
if [ $Iter -eq 5 ]; then
echo ''
echo 'Relax parameter. Iteration:' $Iter
relax -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
else
echo ''
echo 'Iteration:' $Iter
iter -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
fi
if [ $? -ne 0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
done
and here:
Iter=1
while [ $nr_lines -lt 17 ]; do
if [ $Iter -eq 5 ]; then
echo ''
echo 'Relax parameter. Iteration:' $Iter
relax -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
else
echo ''
echo 'Iteration:' $Iter
iter -i $Iter -o ./dir
nr_lines=$(wc -l < ./dir/output$Iter.csv)
let Iter++
fi
done
if [ $? -ne 0 ];
then
echo "exit 1"
fi
echo "EXIT 0"
and both return EXIT 0
, suggesting no errors have occurred.
Solution
Change
iter -i $Iter -o ./dir
to
if ! iter -i $Iter -o ./dir
then
echo "iter failed"
break
fi
This directly tests the exit status of the python script run by iter
. If it exits with a non-zero status, you print the message and break out of the while
loop.
You can do similarly with relax
.
Answered By - Barmar Answer Checked By - Cary Denson (WPSolving Admin)