我试图编写一个bash脚本,它将利用Augeas在/etc/httpd/conf.d/中创建一个conf文件,并为vhost设置必要的节点。由于某种原因,我无法设置DocumentRoot节点。当我尝试通过交互式shell时,我可以创建它:
[root@panel conf.d]# augtool
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
augtool> set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive='DocumentRoot']/arg "/var/www/vhosts/hey.com"
augtool> print /files/etc/httpd/conf.d/hey.com.conf/
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive/arg = "/var/www/vhosts/hey.com"
augtool> save
Saved 1 file(s)
augtool> quit
[root@panel conf.d]# cat hey.com.conf
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>
但是,我的脚本无法创建它:
(我把脚本放在我的路径中以便于执行) 用法示例:#aug hey.com
#!/bin/bash
# Set up conf file
name=$1
path="/files/etc/httpd/conf.d/"
conf="$name.conf"
echo ${path}${conf}
docroot="/var/www/vhosts/$name"
# Create Vhost
echo "DocRoot = $docroot"
mkdir $docroot
echo $path$conf
augtool -s set ${path}${conf}/VirtualHost/arg "*:80"
augtool -s set ${path}${conf}/VirtualHost/directive "DocumentRoot"
echo $docroot
echo "Nodes Present"
augtool print ${path}${conf}
#augtool -s set ${path}${conf}/VirtualHost/*[self::directive='DocumentRoot']/arg "$docroot"
augtool -s set /files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive=DocumentRoot]/arg /var/www/vhosts/hey.com
augtool print ${path}${conf}
augtool match /augeas//error
据我所知,它试图执行相同的命令,但无法将参数写入DocumentRoot。我非常感谢任何帮助指明我正确的方向或重新思考我的想法。
这是在CentOS 6.6盒子上。
更新
脚本动态创建文件,因此事先并不存在。我在脚本运行之间删除它。样本输出如下:
[root@panel ~]# aug hey.com
/files/etc/httpd/conf.d/hey.com.conf
DocRoot = /var/www/vhosts/hey.com
/files/etc/httpd/conf.d/hey.com.conf
Saved 1 file(s)
Saved 1 file(s)
/var/www/vhosts/hey.com
Nodes Present
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/files/etc/httpd/conf.d/hey.com.conf
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/arg = "*:80"
/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/directive = "DocumentRoot"
/augeas/files/etc/sysconfig/iptables.save/error = parse_failed
答案 0 :(得分:1)
弄清楚了,我没有正确引用。在augtool提示符下工作的相同命令在我的bash脚本中不起作用。在脚本中我能够设置指令本身,唯一不同的是参数是使用*和[],而事实证明bash正在处理那些而不是augtool。我引用了它:
augtool -s set '/files/etc/httpd/conf.d/hey.com.conf/VirtualHost/*[self::directive="DocumentRoot"]/arg "/var/www/vhosts/hey.com"'
现在我能够很好地构建节点:
[root@panel ~]# cat /etc/httpd/conf.d/hey.com.conf
<VirtualHost *:80>
DocumentRoot /var/www/vhosts/hey.com
</VirtualHost>