簡體   English   中英

更好的一線式退出帶有錯誤消息的bash腳本

[英]Nicer one-liner for exiting bash script with error message

我想用更清潔,更丑陋的單缸紙完成測試:

#!/bin/bash
test -d "$1" || (echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1) || exit 1

# ... script continues if $1 is directory...

基本上,我追求的是不會重復exit東西,並且最好不要產生子殼(因此應該看起來也不太丑),但仍然適合一行。

沒有子shell,也沒有重復exit

test -d "$1" || { echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1; }

您可能還希望參考分組命令

{}

 { list; } 

在大括號之間放置命令列表會導致該列表在當前Shell上下文中執行。 沒有創建子外殼。 以下列表是分號(或換行符)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM