1,可以为用户做正整数的加、减、乘、除、取余的运算,且在用户输入非正整数时给与提醒
2,有个httpd的虚拟主机配置文件叫/etc/httpd/conf.d/test-88.conf,内容如下,要求编写脚本将虚拟主机www.b.com加入到www.c.com配置的上面,b网站的其他参数可以在运行脚本时自定义,另外c网站的根目录要修改成/var/www/html_c,还要判断如果test-88.conf文件不存在时给与使用者提示"配置文件不存在"
<virtualhost *:80>
servername www.c.com
documentroot /var/www/html
</virtualhost>
第一题答案:
#!/bin/bash
read -p "请输入第一个数字:" a
while :
do
echo "$a"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] && echo string a is number &> dev null
if [ $? -eq 0 ] ;then
break
else
read -p "请输入整数:" a
fi
done
read -p "请输入运算符号:" m
read -p "请输入第二个数字:" b
while :
do
echo "$b"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] && echo string a is number &> dev null
if [ $? -eq 0 ] ;then
break
else
read -p "请输入整数:" b
fi
done
case $m in
+)
echo "$a+$b=$[a+b]";;
-)
echo "$a-$b=$[a-b]";;
\*)
echo " $a*$b=$[a*b] ";;
/)
echo "$a/$b=$[a/b]";;
%)
echo "$a%$b=$[a%b]";;
esac
第二题目答案:
if [ -e /etc/httpd/conf.d/test-88.conf ]; then
echo "配置文件存在,请继续"
else
echo "配置文件不存在"
fi
sed -r -i '/^$|^#/d' /etc/httpd/conf.d/test-88.conf
l=$(sed -n '/^servername www.c.com/=' /etc/httpd/conf.d/test-88.conf)
echo $l
read -p "请输入虚拟主机ip地址:" x
read -p "请输入虚拟主机端口号:" x1
sed -i "$((l-1))i <virtualHost $x:$x1>" /etc/httpd/conf.d/test-88.conf
read -p "请输入虚拟主机域名:" y
sed -i "$((l))i ServerName $y " /etc/httpd/conf.d/test-88.conf
read -p "请输入虚拟主机网页根目录:" z
sed -i "$((l+1))i DocumentRoot $z" /etc/httpd/conf.d/test-88.conf
sed -i "$((l+2))i </virtualhost>" /etc/httpd/conf.d/test-88.conf