名词设置时如何在外壳中智能追加LD_LIBRARY_PATH [英] how to smart append LD_LIBRARY_PATH in shell when nounset

查看:125
本文介绍了名词设置时如何在外壳中智能追加LD_LIBRARY_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下外壳中,如果未设置LD_LIBRARY_PATH,则错误显示 LD_LIBRARY_PATH:未绑定变量.

In following shell, error shows LD_LIBRARY_PATH: unbound variable if the LD_LIBRARY_PATH not set.

我可以使用类似$ {xxx:-yyy}的用法来简化它吗.

Can I use similar usage like ${xxx:-yyy} to simplified it.

#!/bin/bash
set -o nounset
export LD_LIBRARY_PATH=/mypath:$LD_LIBRARY_PATH

推荐答案

您可以使用以下结构:

export LD_LIBRARY_PATH=/mypath${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

说明:

  • 如果未设置 LD_LIBRARY_PATH ,则 $ {LD_LIBRARY_PATH:+:$ LD_LIBRARY_PATH} 扩展为零,而无需评估 $ LD_LIBRARY_PATH ,因此结果等同于 export LD_LIBRARY_PATH =/mypath ,并且不会引发错误.

  • If LD_LIBRARY_PATH is not set, then ${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} expands to nothing without evaluating $LD_LIBRARY_PATH, thus the result is equivalent to export LD_LIBRARY_PATH=/mypath and no error is raised.

如果已经设置了 LD_LIBRARY_PATH ,则 $ {LD_LIBRARY_PATH:+:$ LD_LIBRARY_PATH} 会扩展为:$ LD_LIBRARY_PATH ,因此结果等于 export LD_LIBRARY_PATH =/mypath:$ LD_LIBRARY_PATH .

If LD_LIBRARY_PATH is already set, then ${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} expands to :$LD_LIBRARY_PATH, thus the result is equivalent to export LD_LIBRARY_PATH=/mypath:$LD_LIBRARY_PATH.

请参见 Bash参考手册/3.5.3 Shell参数扩展以获取有关这些扩展的更多信息.

See the Bash Reference Manual / 3.5.3 Shell Parameter Expansion for more information on these expansions.

这是重要的安全措施,因为两个相邻的冒号或尾随/前导的冒号计数是将当前目录添加到 $ PATH $ LD_LIBRARY_PATH .另请参阅:

This is an important security practice as two adjacent colons or a trailing/leading colon count as adding the current directory to $PATH or $LD_LIBRARY_PATH. See also:

这篇关于名词设置时如何在外壳中智能追加LD_LIBRARY_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆