【问题标题】:sed not able to process ~user/.bash_profilesed 无法处理 ~user/.bash_profile
【发布时间】:2012-09-10 07:55:18
【问题描述】:

sed 有一个非常奇怪的问题,如果我这样做的话:

[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' ~user/.bash_profile

没关系。但如果我尝试以下操作:

[root@Camel ~]-> CONF_FILE="~user/.bash_profile"
[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' ${CONF_FILE}
sed: can't read ~user/.bash_profile: No such file or directory

还尝试引用变量:

[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' "${CONF_FILE}"
sed: can't read ~user/.bash_profile: No such file or directory

不知道哪里出了问题,请指教。

Shell 的版本是3.2.25(1)-release。

【问题讨论】:

    标签: bash sed home-directory


    【解决方案1】:

    来自man bash/EXPANSION

    EXPANSION
     Expansion is performed on the command line after it has been
     split  into  words.  There are seven kinds of expansion per-
     formed:  brace expansion,  tilde  expansion,  parameter  and
     variable  expansion, command substitution, arithmetic expan-
     sion, word splitting, and pathname expansion.
    
     The order of expansions is: brace  expansion,  tilde  expan-
     sion,  parameter, variable and arithmetic expansion and com-
     mand substitution (done in a  left-to-right  fashion),  word
     splitting, and pathname expansion.
    

    参数和变量扩展在波浪号扩展之后

    要进行波浪号扩展,这可以在变量定义中完成

    CONF_FILE=~user/.bash_profile
    

    而不是

    CONF_FILE="~user/.bash_profile"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2023-02-14
      • 2012-04-20
      • 2016-08-01
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多