繁体   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