第四周作业

1. 计算 100 以内所有能被 3 整除的整数之和

#!/bin/bash
sum=0
for num in {1..100};do
    if [ $[$num%3] -eq 0 ];then
        let sum+=$num
    else
        continue
    fi
done
echo $sum

 

2. 编写脚本,求 100 以内所有正奇数之和

#!/bin/bash
sum=0
for num in {1..100};do
    [ $[$num%2] -eq 0 ] || let sum+=$num && continue
done
echo $sum

 

3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出

#!/bin/bash
NUM=$[RANDOM%10+1]

while read -p "输入1-10之间的数字:" INPUT;do
    if [ $INPUT -eq $NUM ];then
        echo "你猜对了!"
        break
    elif [ $INPUT -gt $NUM ];then
        echo "大了!"
    else
        echo "小了!"
    fi
done

 

4. 编写函数,实现两个数字做为参数,返回最大值

#!/bin/bash
num1=$1
num2=$2
if [ $num1 -gt $num2 ];then
    echo "最大值为:"$num1
elif [ $num1 -lt $num2 ];then
    echo "最大值为:"$num2
else
    echo "最大值为:"$num2
fi

 

5. 编写一个httpd安装脚本

#!/bin/bash
#关闭防火墙和selinux
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld.service
systemctl disable firewalld.service
#安装相关依赖包
yum -y install gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
#下载并解压缩包
tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src
#配置安装路径
cd /usr/local/src/httpd-2.4.43/
./configure --prefix=/opt/httpd --sysconfdir=/etc/httpd --enable-ssl
#编译&&安装
make -j 2 && make install
#配置环境变量
echo 'PATH=/opt/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
. !$
#运行
apachectl start

 

你可能感兴趣的:(第四周作业)