马哥博客作业第四周
1、计算100以内所有能被3整除的整数之和
sum=0
for i in {1..100}; do
[ $((i%3)) -eq 0 ] && let sum+=$i
done
echo "100以内所有能被3整除的整数之和:$sum"
2、编写脚本,求100以内所有正奇数之和
sum=0
for i in {1..100}; do
[ ! $((i%2)) -eq 0 ] && let sum+=$i
done
echo "100以内所有正奇数之和:$sum"
3、随机生成10以内的数字,实现猜字游戏提示比较大或小,相等则退出
num=$[RANDOM%10]
while read -p "请输入0-9之间的数字:" input; do
if [ $num -eq $input ]; then
echo "恭喜你猜对了"
break
elif [ $num -gt $input ]; then
echo "数字太小了,重新猜"
else
echo "数字太大了,重新猜"
fi
done
4、编写函数,实现两个数字做为参数,返回最大值
nmax() {
if [ $1 -ge $2 ]; then
echo "最大值:$1"
else
echo "最大值:$2"
fi
}
nmax $1 $2
5、编写一个httpd安装脚本
#!/bin/bash
echo "start install httpd"
sleep 3
yum -y install gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
cd /usr/local/src
wget https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2
tar xvf httpd-2.4.43.tar.bz2
cd httpd-2.4.43/
./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
make && make install
echo "PATH=/apps/httpd/bin:$PATH" > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
useradd -r -s /sbin/nologin apache
sed -i -e 's/^User.*/User apache/' -e 's/^Group.*/Group apache/' /etc/httpd/httpd.conf
apachectl start
echo "httpd is installed"