加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

理解这个.bashrc脚本(花括号,eval,……)

发布时间:2020-12-15 22:42:12 所属栏目:安全 来源:网络整理
导读:我很难理解我的ubuntu .bashrc中写的内容,如下所示. 这是我不明白的: 花括号的目的是什么,以及后面使用的符号是什么? (例如:${debian_chroot: – }和${debian_chroot:($debian_chroot)}) eval命令. 以下代码片段如何工作. [ -x /usr/bin/lesspipe ] eva
我很难理解我的ubuntu .bashrc中写的内容,如下所示.
这是我不明白的:

>花括号的目的是什么,以及后面使用的符号是什么? (例如:${debian_chroot: – }和${debian_chroot:($debian_chroot)})
> eval命令.
>以下代码片段如何工作.

[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$'
else
    PS1='${debian_chroot:+($debian_chroot)}u@h:w$'
fi

解决方法

${var:-default}表示$var,如果定义$var,否则为“default”

${var:value}表示如果定义了$var,则使用“value”;否则没什么

第二个可能看起来有点奇怪,但你的代码片段显示了一个典型的用法:

${debian_chroot:+($debian_chroot)}

这意味着“如果定义$debian_chroot,则将其插入括号中.”

上面,“已定义”表示“设置为某些非空值”. Unix shell通常不区分未设置的变量和设置为空字符串的变量,但是如果使用未设置的变量,则可以告诉bash引发错误条件. (你用set -u来做.)在这种情况下,如果从未设置debian_chroot,$debian_chroot将导致错误,而${debian_chroot: – }将使用$debian_chroot(如果已设置),否则为空字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读