如何回应而不被执行

时间:2015-08-14 19:29:06

标签: bash shell export echo

我正在尝试在/ etc / profile中添加echo语句,但它们会执行。

示例:这就是我写的

echo CURRENTYEAR="`/bin/date +%y`" >> /etc/profile 
    echo  CURRENTWEEK="`/bin/date +%V` " >> /etc/profile
    echo VERSION="$CURRENTYEAR"."$CURRENTWEEK" >> /etc/profile
    echo export VERSION >> /etc/profile



      echo export SVN_HOME="https://example.com/svn/road" >> /etc/profile
      echo SVN_BRANCH="$SVN_HOME/branches/qa_weekly/$VERSION" >> /etc/profile
      echo export SVN_TAG="https://example.cpm/svn/road/tags" >> /etc/profile
      echo export SVN_TRUNK="https://example.com/svn/empire/trunk" >> /etc/profile
     export PATH=$PATH:/road >> /etc/profile
     export SVN_BRANCH

结果:我得到了什么

CURRENTYEAR=15
CURRENTWEEK=33
VERSION=.
export VERSION
export SVN_HOME=https://example.com/svn/road
SVN_BRANCH=/branches/qa_weekly/
export SVN_TAG=https://example.com/svn/road/tags
export SVN_TRUNK=https://example.com/svn/road/trunk

我希望它在/ etc / profile

中如下所示
CURRENTYEAR=`/bin/date +%y`
CURRENTWEEK=`/bin/date +%V`
VERSION=$CURRENTYEAR"."$CURRENTWEEK
export VERSION

SVN_BRANCH=$SVN_HOME/branches/qa_weekly/$VERSION
SVN_TAG=$SVN_HOME/tags
SVN_TRUNK=$SVN_HOME/trunk

先谢谢。

2 个答案:

答案 0 :(得分:5)

将字符串放在单引号中:

echo 'CURRENTYEAR="`/bin/date +%y`"' >> /etc/profile
#....^.............................^

http://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

  

用单引号括起字符会保留引号中每个字符的字面值。单引号之间可能不会出现单引号,即使前面有反斜杠也是如此。

答案 1 :(得分:1)

使用带有引用文档的cat而不是多个echo语句:

cat >> /etc/profile <<'EOF'
CURRENTYEAR=`/bin/date +%y`
CURRENTWEEK=`/bin/date +%V`
VERSION="$CURRENTYEAR.$CURRENTWEEK"
export VERSION
export SVN_HOME="https://example.com/svn/road"
SVN_BRANCH="$SVN_HOME/branches/qa_weekly/$VERSION"
export SVN_TAG="https://example.cpm/svn/road/tags" 
export SVN_TRUNK="https://example.com/svn/empire/trunk"
export PATH=$PATH:/road
export SVN_BRANCH
EOF

在第一行中引用EOF可防止在此文档的正文中发生任何替换。