在 Mac OSX 10.6 中向 etc/hosts 和 Apache httpd-vhosts.conf 添加新行的 Shell 脚本
我正在使用 Mac OSX 10.6 并在其上进行 Web 开发。我对编写 shell 脚本有一点了解,但目前还不太精通。
我想要做的是编写一个 shell 脚本,该脚本将简单地请求本地站点别名和文档目录,然后将新别名附加到主机上,并在新行上添加“127.0.0.1 mysite.local”之类的内容在 etc/hosts 的底部。
然后该脚本将在 Apache 的 httpd-vhosts.conf 文件中添加如下内容:
<VirtualHost *:80>
DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
ServerName mysite.local
ServerAlias mysite.localhost
</VirtualHost>
然后它最终会运行命令来重新启动我的 Apache 服务器。现在我知道了重启 Apache 的终端命令,这很简单。我还知道如何从运行脚本的用户那里读取站点名称和路径。如下所示:
#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath
我不知道该怎么做是将文本从终端附加到文件中。
有什么想法或有用的想法吗?
谢谢, 帕特里克
I am using Mac OSX 10.6 and doing web development on it. I know a small amount about writing shell scripts, but I am not really versed in them as of yet.
What I would like to do is to write a shell script that will simply ask for a local site alias and the document directory and it will then append the new alias onto hosts with something like "127.0.0.1 mysite.local" on a new line at the bottom of etc/hosts.
Then the script would append Apache's httpd-vhosts.conf file with something like this:
<VirtualHost *:80>
DocumentRoot "/Repositories/myproject/mysite.com/trunk/htdocs"
ServerName mysite.local
ServerAlias mysite.localhost
</VirtualHost>
Then it would finally run the command to restart my Apache server. Now I know the terminal command to restart Apache, that is simple enough. I also know how to read in the site name and path from the user running the script. Such as below:
#!/bin/bash
read -p "New local site name: " site
read -p "Site path (ex:/Repositories/myproject/mysite.com/trunk/htdocs): " sitepath
What I don't know how to do is to append text to a file from terminal.
Any thoughts or helpful ideas?
Thanks,
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未经测试,但它应该可以工作:
>>
将输出重定向到给定文件,并将内容附加到文件中。我还使用-e
来允许\t
扩展为制表符。请注意,您需要使用
sudo
运行此脚本。我还添加了在修改原始文件之前备份原始文件的命令,以防万一。Untested, but it should work:
>>
redirects the output to the given file, appending the contents to the file. I’m also using-e
to allow\t
to be expanded to a tab character.Note that you need to run this script with
sudo
. I've also included commands to backup the original files before modifying them, just in case.我在上面的答案中做了一些调整并做了一些额外的事情,因为这对我不起作用,但它帮助我提出了另一个解决方案。此答案仅适用于
Mac
用户。首先进入您的
/etc/apache2/httpd.conf
并取消注释虚拟主机引用,即这一行Include /private/etc/apache2/extra/httpd-vhosts.conf
现在创建一个 bash 文件,我在用户的家中创建了一个名为
imran
的文件,您需要将其替换为您的用户名
。我将其放置在
/Users/imran
中,命名为create_new_site.sh
。我给了它可执行权限,因此可以使用 chmod +x create_new_site.sh 轻松执行它脚本代码如下:
完成后,您可以开始使用 sudo 创建新站点。 /create_new_site.sh。请记住,您需要位于主目录中,可以通过 cd ~ 命令进入该目录。现在假设您创建了一个名为
test
的站点。您应该能够访问 http://test.local/phpinfo.php 查看您的虚拟主机
工作。I made some tweaks and did some extra stuff in the above answer, because this didn't work for me but it helped me to come up with another solution. This answer is only for
Mac
users.First go in your
/etc/apache2/httpd.conf
and uncomment virtual host reference, which is this lineInclude /private/etc/apache2/extra/httpd-vhosts.conf
Now create a bash file I did that in my user’s home named as
imran
, you need to replace it with yourusername
.I placed it inside
/Users/imran
named ascreate_new_site.sh
. I gave it executeable permissions, so it can be easily executed usingchmod +x create_new_site.sh
Code for the script is as below:
Once that is done you can start creating new sites using
sudo ./create_new_site.sh
. Remember that you need to be in your home directory, which you can go viacd ~
command. Now let's suppose you created a site with nametest
. You should be able to visit http://test.local/phpinfo.php to see yourvhost
working.