第4周作业
1. 计算 100 以内所有能被 3 整除的整数之和
#!/bin/bash
sum=0
for i in {1..100}; do
if [ $[$i%3] -eq 0 ]; then sum=$[$sum+$i] fi done
echo "sum=$sum"
2. 编写脚本,求 100 以内所有正奇数之和
#!/bin/bash
#求100以内的奇数和
echo 计算出1-100的奇数之和
sum=0
for i in $(seq 1 2 100)
do
let sum+=i
done
echo $sum
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
#随机生成10以内的数字,实现猜字游戏
read -p "请输入一个10以内数字: " NU
if [ $NU -gt $[$RANDOM%10] ];then
echo "数字比较大"
elif [ $NU -eq $[$RANDOM%10] ];then
exit
elif [ $NU -lt $[$RANDOM%10] ];then
echo "数字太小了"
fi
4. 编写函数,实现两个数字做为参数,返回最大值
#!/bin/bash
# 比较两个数大小,返回最大值
max_num () {
if [ $1 -gt $2 ];then
echo "Max is $1"
else
echo "Max is $2"
fi
}
max_num $*
5. 编写一个httpd安装脚本
#!/bin/bash
#一键安装httpd
#配置yum源和epel源
rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean al
yum makecache
yum repolist
#下载httpd源码包
curl -o /opt/httpd-2.4.43.tar.bz2 https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.43.tar.bz2
ls /opt
#安装解压赖包
yum -y install bzip2
#安装httpd依赖包
yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
#创建apache用户
useradd -r -u 48 -s /sbin/nologin -c "Apache" -d /var/www/ apache
cd /opt/ && tar -xjvf httpd-2.4.43.tar.bz2 -C /usr/local
cd /usr/local
./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl
make && make install
if [ $? -eq 0 ] ;then echo "httpd installed successful!"
fi