这篇文章主要介绍了shell命令返回值判断的方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇shell命令返回值判断的方法有哪些文章都会有所收获,下面我们一起来看看吧。1.判断命令是否存在优雅方法1首先
这篇文章主要介绍了shell命令返回值判断的方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇shell命令返回值判断的方法有哪些文章都会有所收获,下面我们一起来看看吧。
首先,检查命令是否有效的惯用方法直接在if语句中。
if command; then echo notify user OK >&2else echo notify user FaiL >&2 return -1fi
(良好做法:使用>&2将消息发送给stderr。)
将通用逻辑转移到共享函数中。
check() { local command=("$@") if "${command[@]}"; then echo notify user OK >&2 else echo notify user FAIL >&2 exit 1 fi}check command1check command2check command3
installed () { command -v "$1" >/dev/null 2>&1}if installed <command1>then <command1> xxelse <command1> xxx fi
command1 || exitcommand2 || exitcommand3 || exit
$ bash -e xx.sh#!/bin/bash -e xx.shcommand1command2command3
$ bash xx.sh #!/bin/bashset -e command1command2command3
方法1
if do some command; then echo notify user OKelse echo notify user fail exit 255 # exit code must be unsigned shortfi
方法2
do some commandif [ $? -eq 0 ]; then echo notify user OKelse echo notify user FAIL return -1fi
方法1
die() { local message=$1 echo "$message" >&2 exit 1}command1 || die 'command1 failed'command2 || die 'command2 failed'command3 || die 'command3 failed'
方法2(推荐)
warn () { echo "$@" >&2}die () { status="$1" shift warn "$@" exit "$status"}do some command && echo notify user OK || die 255 Notify user fail
关于“shell命令返回值判断的方法有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“shell命令返回值判断的方法有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。
--结束END--
本文标题: shell命令返回值判断的方法有哪些
本文链接: https://www.lsjlt.com/news/323328.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0