这是我的上下文:我有一个名为 profile
# file: profile
MY_PATH = "/home/deng/default"
MY_NAME = "deng"
我的测试文件:
#!/bin/sh
#file: test.sh
DJ_PATH="/deng/jian/doc"
OUTPUT_PATH="\"${DJ_PATH}/output\""
echo "OUTPUT_PATH = ${OUTPUT_PATH}"
DOC_KEY="MY_PATH"
DOC_VALUE=${OUTPUT_PATH}
echo "DOC_VALUE = ${DOC_VALUE}"
echo "sed 's:[ \t]*${DOC_KEY}[ \t]*=.*:${DOC_KEY} = ${DOC_VALUE}:' profile"
sed 's:[ \t]*${DOC_KEY}[ \t]*=.*:${DOC_KEY} = ${DOC_VALUE}:' profile
在终端我跑
$ ./test.sh
输出是
1 OUTPUT_PATH = "/deng/jian/doc/output"
2 DOC_VALUE = "/deng/jian/doc/output"
3 sed 's:[ \t]*MY_PATH[ \t]*=.*:MY_PATH = "/deng/jian/doc/output":' profile
4 MY_PATH = "/home/deng/default"
5 MY_NAME = "deng"
它看起来配置文件中的文本没有被我想要设置的 DOC_VALUE ("/deng/jian/doc/output") 替换。但我在输出行 3 中运行了该命令。它有效:
sed 's:[ \t]*MY_PATH[ \t]*=.*:MY_PATH = "/deng/jian/doc/output":' profile
输出:
MY_PATH = "/deng/jian/doc/output"
MY_NAME = "deng"
所以发生了什么事?我在 test.sh 中写的命令有什么不同
sed 's:[ \t]*${DOC_KEY}[ \t]*=.*:${DOC_KEY} = ${DOC_VALUE}:' profile
和我直接输入的命令?
sed 's:[ \t]*MY_PATH[ \t]*=.*:MY_PATH = "/deng/jian/doc/output":' profile