如果将错误数量的参数传递给我的脚本,如何退出并显示失败代码?

Dav*_*ave 1 bash shell-script exit-status

我正在使用 bash。如果我的脚本参数数量错误,如何以不成功代码退出?

我有这个

#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Should be three parameters to this script -- [CWD driver-directory side-file]"
fi
Run Code Online (Sandbox Code Playgroud)

但随后脚本继续。

Jef*_*ler 6

您只需要致电exit:

#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo "Should be three parameters to this script -- [CWD driver-directory side-file]"
    exit 9
fi
Run Code Online (Sandbox Code Playgroud)

使用您喜欢或想要的 1 到 255 之间的任何非负整数。