簡體   English   中英

是否有可能使用bash函數在參數末尾檢測空格?

[英]Is it possible to detect a space at the end of an argument with a bash function?

我有一個bash函數,看起來像這樣:

test5() { echo $#; }

我希望能夠檢測到最后一個參數后面是否有空格。

例如,運行此命令(引號顯示以說明空白): "test5 test"產生: 1"test5 test "也產生1 我想檢測一下測試后何時有空間。

謝謝!

從命令行無法使用。 可以在腳本中工作:

#/bin/bash
test5(){
    [[ $(sed ${BASH_LINENO[0]}'!d;/ $/!d' <"${BASH_SOURCE[1]}") ]] &&
    echo called with trailing whitespace

    echo $#;
}

test5 5 args no trailing spaces
test5 5 args three trailing spaces   
test5 "1 arg no trailing spaces     "

暫無
暫無

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

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