Bash 替换 : log when variable is not set

标签 bash

我使用 bash substitutions为输入提供简洁的单行验证,例如:

#!/bin/bash
export PARAM1=${1?Error, please pass a value as the first argument"}
# do something...

但在某些情况下,我只想在某些内容未设置时记录一条消息,然后照常继续。这可能吗?

最佳答案

也许是

test -n "$1" && export PARAM1="$1" || log "\$1 is empty!"

应该做;当且仅当 $1 不为空时,test 子句才返回 true。

关于Bash 替换 : log when variable is not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092628/

相关文章:

java - 从 in_exec Fluentd 插件启动 java 命令

mysql - Vagrant + SSH + Bash + MySQL - 运行脚本

bash - 使用ssh启动后台进程,运行实验脚本,然后将其停止

linux - 如何将带空格的参数传递给 sed 命令?

linux - (bash) 使用 cat 将多行文本缩进到 $VAR?

linux - 此 bash/shell 练习需要帮助

python - 在 .profile 中运行程序会减慢 GUI startx

bash - 需要对 kubectl stdin 和 pipeline 进行一些解释

bash - 将文件的内容通过管道传输到 ls

linux - 在 bash tee 中,函数变量是局部的,我该如何逃避呢?