用shell脚本安装apache

我们首先创建一个文件为test.sh,执行此文件的方法有以下四种方式:

1、./test.sh(必须chmod赋予执行权限)

2、. test.sh

3、sourse test.sh

4、[shell] test.sh  (shell类型可以为bash、dash、tcsh、csh、sh等)

代码段如下:

# !/bin/bash
cd /root/soft
tar zxvf httpd-2.2.17.tar.gz -C /usr/src  > /dev/null       (这里的/dev/null为黑洞设备,意思是把语句输出结果放进黑洞,不显示出来,也可以自定义文件路径)
cd /usr/src
cd httpd-2.2.17
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi > /dev/null
make >> /dev/null
make install > /dev/null
cd
ln -s /usr/local/httpd/bin/* /usr/local/bin
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
echo "# chkconfig: 35 84 21
# description:hehe" >> /etc/init.d/httpd
echo "ServerName www.benet.com" >> /usr/local/httpd/conf/httpd.conf
apachectl -t &> /root/桌面/error.log
echo "192.168.1.1  www.benet.com" >> /etc/hosts
chkconfig --add httpd
service httpd stop &> /dev/null
service httpd start &> /dev/null
firefox http://www.benet.com

以上代码中的重定向符号是不一样的,那么总结为:

1、重定向输入

  使用"<"符号,通过重定向输入可以使一些交互式操作过程能够通过读取文件来执行(默认设备是键盘,文件编号为0,所以也可以写成 0<)

2、重定向输出

  1)正常输出:使用“>”和“>>”符号,分别用于覆盖或追加文件(默认设备是显示器,文件编号为1,所以也可以写成1>或1>>)

  2)错误输出:错误重定向指的是将执行命令过程中出现的错误信息(如选项参数错误等)保存在制定的文件,而不是直接显示在屏幕上。错误重定向使用“2>”或“2>>”操作符

  3)混合输出:“&>”综合了正常输出和错误输出两种方法,这种方法比较常用。

原文地址:https://www.cnblogs.com/mangood/p/5950907.html