默认退出状态码.sh Shell 脚本

Shell 片段 · codes/shell/脚本函数/默认退出状态码.sh · 53 行

源码

#!/usr/bin/env bash

# testing the exit status of a function

func1() {
	echo "Trying to display a non-existent file"
	ls -l badfile
}

#由于最后一条命令未执行成功,返回的状态码非0
echo "testing the function"
func1
echo "The exit status is : $?"

func2() {
	ls -l badfile
	echo "Another test to display a non-existent file"
}

#由于最后一条命令echo执行成功,返回的状态码为0
echo "Another test"
func2
echo "The exit status is : $?"





























查看上游源码 ↗

内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。