Detecting sys.exit() and ValueError from a python script in bash script

huangapple go评论89阅读模式
英文:

Detecting sys.exit() and ValueError from a python script in bash script

问题

我创建了一个bash脚本,根据特定条件(即,如果文件有小于x行,则运行此脚本,如果...,等等)管理多个相关的Python脚本。我在bash脚本中使用入口点来执行这些Python脚本。

在验证bash脚本时,我发现一些边缘情况,其中所有条件都失败,导致sys.exit(load_entry_point('Program', 'console_scripts', 'iter')())ValueError: Nothing was generated(在Python程序中指定,如果什么都不发生,就会引发此错误)。

在Stack Overflow上寻找解决方案后,我尝试了在某些条件中使用以下内容:

if [ $? -ne 0 ];
then
    echo "exit 1"
fi
echo "EXIT 0"

我继续得到EXIT 0,表明没有检测到错误。我在这里有什么不明白的地方?

为了澄清,我的目标是检测由调用的Python脚本抛出的任何错误,并相应地让bash脚本作出响应。

这是bash脚本的大致示意:

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

我尝试将错误捕获条件放在这里:

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

以及在这里:

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"

两者都返回EXIT 0,表明没有发生错误。

英文:

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(&#39;Program&#39;, &#39;console_scripts&#39;, &#39;iter&#39;)()) 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 &quot;exit 1&quot;
    fi
    echo &quot;EXIT 0&quot;

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 &#39;&#39;
        echo &#39;Relax parameter. Iteration:&#39; $Iter

        relax -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./dir/output$Iter.csv)
        
        let Iter++

    else

        echo &#39;&#39;
        echo &#39;Iteration:&#39; $Iter

        iter -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./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 &#39;&#39;
        echo &#39;Relax parameter. Iteration:&#39; $Iter

        relax -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./dir/output$Iter.csv)
        
        let Iter++

    else

        echo &#39;&#39;
        echo &#39;Iteration:&#39; $Iter

        iter -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./dir/output$Iter.csv)
        
        let Iter++
    fi

    if [ $? -ne 0 ];
    then
        echo &quot;exit 1&quot;
    fi
    echo &quot;EXIT 0&quot;

done

and here:

Iter=1
while [ $nr_lines -lt 17 ]; do

    if [ $Iter -eq 5 ]; then

        echo &#39;&#39;
        echo &#39;Relax parameter. Iteration:&#39; $Iter

        relax -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./dir/output$Iter.csv)
        
        let Iter++

    else

        echo &#39;&#39;
        echo &#39;Iteration:&#39; $Iter

        iter -i $Iter -o ./dir

        nr_lines=$(wc -l &lt; ./dir/output$Iter.csv)
        
        let Iter++
    fi

done

if [ $? -ne 0 ];
then
    echo &quot;exit 1&quot;
fi
echo &quot;EXIT 0&quot;

and both return EXIT 0, suggesting no errors have occurred.

答案1

得分: 1

将以下部分进行翻译:

        iter -i $Iter -o ./dir

改为

        if ! iter -i $Iter -o ./dir
        then
            echo "iter 失败"
            break
        fi

这直接测试了由 iter 运行的 Python 脚本的退出状态。如果它以非零状态退出,您将打印该消息并跳出 while 循环。

您可以类似地处理 relax

英文:

Change

        iter -i $Iter -o ./dir

to

        if ! iter -i $Iter -o ./dir
        then
            echo &quot;iter failed&quot;
            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.

huangapple
  • 本文由 发表于 2023年7月7日 02:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定