bash 使用 shell 脚本中的内容创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13633638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create file with contents from shell script
提问by UKB
How do I, in a shell script, create a file called foo.conf and make it contain:
我如何在 shell 脚本中创建一个名为 foo.conf 的文件并使其包含:
NameVirtualHost 127.0.0.1
# Default
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>
回答by ams
Use a "here document":
使用“此处文档”:
cat > foo.conf << EOF
NameVirtualHost 127.0.0.1
# Default
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>
EOF
回答by sampson-chen
You can do that with echo
:
你可以这样做echo
:
echo 'NameVirtualHost 127.0.0.1
# Default
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>' > foo.conf
Everything enclosed by single quotes are interpreted as literals, so you just write that block into a file called foo.conf
. If it doesn't exist, it will be created. If it does exist, it will be overwritten.
用单引号括起来的所有内容都被解释为文字,因此您只需将该块写入名为foo.conf
. 如果它不存在,它将被创建。如果它确实存在,它将被覆盖。
回答by nullrevolution
a heredoc might be the simplest way:
继承人可能是最简单的方法:
cat <<END >foo.conf
NameVirtualHost 127.0.0.1
# Default
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www"
</VirtualHost>
END
回答by Sergej
This code fitted best for me:
这段代码最适合我:
sudo dd of=foo.conf << EOF
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/localhost
</VirtualHost>
EOF
It was the only one I could use with sudo
out of the box!
这是我sudo
开箱即用的唯一一款!