linux - 或者 shell 脚本中的两个值?

标签 linux shell

我有一个变量,它可能为空也可能不为空。如果它不为空,我想要那个值。如果它是空的,我想要一些其他命令的值。示例:

my_function ()
{
    name_override="$1"
    user_name=${name_override} || $(git config "user.name")
}

我不确定上面的代码是否有效。但基本上我想运行 git config 并将结果存储在 user_name 中,如果 name_override 变量未设置(因此 $1 不会被提供)。

我怎样才能正确地做到这一点?

最佳答案

完成此操作的 bash 习惯用法是

user_name=${1:-$(git config "user.name")}

其中 :- 表示使用 $1 的值(如果已设置且非空),否则使用以下字符串。

关于linux - 或者 shell 脚本中的两个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961515/

相关文章:

linux - 如何将 Linux 日期从 14-01-2015 转换为 14-1-2015?

linux - puppet 踢和环境变量

python - VTK 与真正的 X 一起工作,与 Xvfb 一起崩溃

linux - 在远程服务器上以图形模式运行 emacs

linux - Bash:使用管道运算符时陷阱 ERR 不起作用

linux - 从不断更新的目录中读取更新

php - 检查命令是否正在多用户 php 进程中运行

linux - linux内核中的系统调用

linux - 在 shell 中从文件中删除模式线的快速方法

php - 无法使用 shell 命令在 php 中转换音频文件格式