第四周作业

1. 计算 100 以内所有能被 3 整除的整数之和
sum=0;for ((i=1;i<=100;i++));do [ $((i%3)) -eq 0 ] && let sum+=$i;done;echo $sum

2. 编写脚本,求 100 以内所有正奇数之和
vim odd_sum.sh
#!/bin/bash
#求100以内的奇数和

sum=0
for i in `seq 1 2 100`;do
    let sum+=$i
    let i++
done
echo $sum

3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
vim random.sh
#!/bin/bash
#随机生成10以内的数字,实现猜字游戏

while true;do
    n=$((RANDOM%10+1))
    read -p "请输入一个10以内的数字:" num
    if [ $num -eq $n ];then
        echo "恭喜你中奖了"
        exit
    elif [ $num -gt $n ];then
        echo "数字比较大"
    elif [ $num -lt $n ];then
        echo "数字比较小"
    fi
done

4. 编写函数,实现两个数字做为参数,返回最大值
vim max_num.sh
#!/bin/bash
#比较两个数的最大值

max_num () {
    if [ $# -ne 2 ];then
        echo "请输入两个数字为脚本参数"
        exit
    fi

    if [ $1 -gt $2 ];then
        echo "Max is $1"
    else
        echo "Max is $2"
    fi
}
max_num $*

5. 编写一个httpd安装脚本
vim httpd_install.sh
#!/bin/bash
#httpd source code install

#下载源码包
target_dir=/usr/local/src
install_dir=/usr/local/httpd
rpm -qa | grep wget || yum install -y wget
wget -O $target_dir/httpd-2.4.43.tar.bz2 https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.43.tar.bz2

#安装依赖包
yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config

#添加apache用户
id apache &> /dev/null || useradd -r -u 80 -d /var/www -s /sbin/nologin apache

#解压源码包
tar xf $target_dir/httpd-2.4.43.tar.bz2 -C $target_dir
cd $target_dir/httpd-2.4.43

#编译安装
./configure --prefix=$install_dir --sysconfdir=/etc/httpd --enable-ssl
make -j`lscpu | grep "^CPU(s)" | awk '{print $NF}'` && make install

#设置环境变量
echo 'PATH='$install_dir'/bin:$PATH'  > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh

#修改配置文件
sed -ri 's#(User )daemon#\1apache#' /etc/httpd/httpd.conf
sed -ri 's#(Group )daemon#\1apache#' /etc/httpd/httpd.conf

#启动httpd服务
apachectl start

#检查firewalld状态
firewall_status=`systemctl status firewalld.service | grep "Active" | awk '{print $2}'`
if [ $firewall_status = active ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-service=http --add-service=https
    firewall-cmd --reload
fi

posted @ 2020-06-21 20:58  小余518  阅读(156)  评论(0)    收藏  举报