gpt4 book ai didi

bash 脚本 - 仅当不为空时回显

转载 作者:行者123 更新时间:2023-11-29 09:31:49 25 4
gpt4 key购买 nike

有没有我可以用来封装 echo 语句的选项或其他东西,以便它们在该行为空时不打印?

我在想:

myecho(str){
if [[ -z str ]]; then
echo str
fi
}

但我找不到如何传递参数。

最佳答案

传递给函数的参数就像传递给脚本的参数一样。从 $1 开始直到 $n

myecho(){
if [ -n "$1" ] ; then
echo "$@"
fi
}

myecho ""
myecho "foo"

在测试中使用 -n 可以验证字符串是否为空。详细了解 bash 中的函数:http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

关于bash 脚本 - 仅当不为空时回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29064823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com