简体   繁体   English

是否有可能使用bash函数在参数末尾检测空格?

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

I have a bash function that looks like this: 我有一个bash函数,看起来像这样:

test5() { echo $#; }

I want to be able to detect when there is a space after the last argument. 我希望能够检测到最后一个参数后面是否有空格。

For example, running this (quotes show to illustrate whitespace): "test5 test" produces: 1 while "test5 test " also produces 1 . 例如,运行此命令(引号显示以说明空白): "test5 test"产生: 1"test5 test "也产生1 I would like to detect when there is a space after test. 我想检测一下测试后何时有空间。

Thank you! 谢谢!

Won't work from commandline. 从命令行无法使用。 May work in script: 可以在脚本中工作:

#/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