bash:如何在'cat' create-file命令的内容中得到<current path>
bash: How to get <current path> in the content of the 'cat' create-file command
下面的 shell 脚本创建了一个新文件 "foo.conf",其内容在 EOF 标记之间指定。
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "./search.bin"
EOF
创建的文件包含以下内容:
DocumentRoot "./search.bin"
但我需要有当前目录的完整路径(我的 shell 脚本所在的位置),例如:
DocumentRoot "/home/me/search.bin"
这可能吗?
提前致谢。
使用这个:
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "$(readlink -f ./search.bin)"
EOF
你可以替换
readlink -f
来自
realpath
带有未加引号的第一个单词的 Heredocs 允许几乎完整的变量和其他扩展。所以你可以做
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "$(pwd)/search.bin"
EOF
下面的 shell 脚本创建了一个新文件 "foo.conf",其内容在 EOF 标记之间指定。
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "./search.bin"
EOF
创建的文件包含以下内容:
DocumentRoot "./search.bin"
但我需要有当前目录的完整路径(我的 shell 脚本所在的位置),例如:
DocumentRoot "/home/me/search.bin"
这可能吗? 提前致谢。
使用这个:
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "$(readlink -f ./search.bin)"
EOF
你可以替换
readlink -f
来自
realpath
带有未加引号的第一个单词的 Heredocs 允许几乎完整的变量和其他扩展。所以你可以做
#!/bin/sh
cat > foo.conf << EOF
DocumentRoot "$(pwd)/search.bin"
EOF