網站首頁 編程語言 正文
1.判斷命令是否存在
優雅方法1
首先,檢查命令是否有效的慣用方法直接在if語句中。
if command; then ? ? echo notify user OK >&2 else ? ? echo notify user FAIL >&2 ? ? return -1 fi
(良好做法:使用>&2將消息發送給stderr。)
優雅方法2
將通用邏輯轉移到共享函數中。
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
優雅方法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
優雅方法
方法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#教程
相關推薦
- 2022-10-21 React?模式之純組件使用示例詳解_React
- 2022-04-21 詳解Golang?Map中的key為什么是無序的_Golang
- 2022-11-17 python中關于os.path.pardir的一些坑_python
- 2022-07-06 YOLOv5目標檢測之anchor設定_python
- 2023-01-10 Docker調度器Kubernetes使用過程_docker
- 2022-12-01 Rust實現AES加解密詳解_Rust語言
- 2022-08-15 springboot使用配置文件配置bean屬性產生中文亂碼問題
- 2022-05-12 android ViewModel+LiveData簡單使用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支