網(wǎng)站首頁 編程語言 正文
1.判斷命令是否存在
優(yōu)雅方法1
首先,檢查命令是否有效的慣用方法直接在if語句中。
if command; then ? ? echo notify user OK >&2 else ? ? echo notify user FAIL >&2 ? ? return -1 fi
(良好做法:使用>&2將消息發(fā)送給stderr。)
優(yōu)雅方法2
將通用邏輯轉(zhuǎn)移到共享函數(shù)中。
check() { ? ? local command=("$@") ? ? if "${command[@]}"; then ? ? ? ? echo notify user OK >&2 ? ? else ? ? ? ? echo notify user FAIL >&2 ? ? ? ? exit 1 ? ? fi } check command1 check command2 check command3
優(yōu)雅方法3
installed () { ? ? ? ? command -v "$1" >/dev/null 2>&1 } if installedthen ? ? ? ? ?xx else ? ? ? ? ?xxx ?fi
2.返回錯誤退出
1.|| exit退出
command1 || exit command2 || exit command3 || exit
2.使用-e
$ ?bash -e xx.sh #!/bin/bash -e xx.sh command1 command2 command3
3.set -e
$ bash xx.sh? #!/bin/bash set -e? command1 command2 command3
3.返回錯誤提示
一般方法:
方法1
if do some command; then ? ? echo notify user OK else ? ? echo notify user fail ? ? exit 255 ?# exit code must be unsigned short fi
方法2
do some command if [ $? -eq 0 ]; then ? ? echo notify user OK else ? ? echo notify user FAIL ? ? return -1 fi
優(yōu)雅方法
方法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
原文鏈接:https://blog.csdn.net/xixihahalelehehe/article/details/104819343
- 上一篇:C#委托用法詳解_C#教程
- 下一篇:C#操作進程的方法介紹_C#教程
相關(guān)推薦
- 2022-08-15 springboot使用配置文件配置bean屬性產(chǎn)生中文亂碼問題
- 2023-07-16 uniapp 小程序訂閱消息報錯( wx.requestSubscribeMessage is no
- 2022-07-14 AVX2指令集優(yōu)化整形數(shù)組求和算法_C 語言
- 2022-10-22 Kotlin匿名函數(shù)使用介紹_Android
- 2022-05-13 larvel8 批量刪除
- 2022-12-11 Go語言實現(xiàn)棧與隊列基本操作學(xué)家_Golang
- 2022-12-12 用C語言如何打印一個等腰三角形_C 語言
- 2022-12-25 python字典中items()函數(shù)用法實例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支