Bash忽略$

时间:2013-05-03 09:27:15

标签: linux bash

我有一个bash脚本,它在postfix main.cf文件中回显

IPADD=$(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
cat << EOG > /etc/postfix/main.cf
# LOCAL PATHNAME INFORMATION
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix

# QUEUE AND PROCESS OWNERSHIP
mail_owner = postfix
# SENDING MAIL
myorigin = $mydomain

# RECEIVING MAIL
mydestination = $myhostname localhost.$mydomain localhost $mydomain
# TRUST AND RELAY CONTROL
mynetworks_style = host
smtp_bind_address = $IPADD
inet_interfaces = $IPADD, 127.0.0.1

EOG

问题是,一旦你运行脚本,就会从:

中删除“$ mydomain”
 # SENDING MAIL
    myorigin = $mydomain

并从

中删除“$ myhostname”和“$ mydomain”
# RECEIVING MAIL
    mydestination = $myhostname localhost.$mydomain localhost $mydomain

我希望$ IPADD可以工作和运行,但是希望它忽略$ mydomain和$ myhostname的“$”。

这可能吗?

2 个答案:

答案 0 :(得分:3)

cat << 'EOG' > /etc/postfix/main.cf
# other stuff
myorigin = $mydomain
# other stuff
EOG

Here Document

答案 1 :(得分:0)

使用$(echo '$a')可以解决问题:

a=1
cat  << EOF > /etc/postfix/main.cf
> test=$(echo '$a')
> foo=$a
> EOF

输出:

test=$a
foo=1