马哥博客作业第四周
1. 计算 100 以内所有能被 3 整除的整数之和
for i in {1..100};do
if [ `expr $i % 3` -eq 0 ];then
echo $i >> test.txt
fi
done
for j in `cat test.txt`;do
let sum+=j
done
echo $sum
rm -f test.txt
2.编写脚本,求 100 以内所有正奇数之和
for i in `seq 1 2 100`;do
let sum+=i
done
echo $sum
3.随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
RandomNum=$(expr $RANDOM % 10 + 1)
while true;do
read -p "Pls input a number within 10: " n1
if [ $n1 -gt $RandomNum ];then
echo "The num is greater,please go on!"
elif [ $n1 -lt $RandomNum ];then
echo "The num is smaller,please go on!"
elif [ $n1 -eq $RandomNum ];then
echo "bingo"
break
fi
done
4.编写函数,实现两个数字做为参数,返回最大值
max () {
if [ -z "$1" -o -z "$2" ];then
echo "Need two numbers."
elif [ $1 -eq $2 ];then
echo "The two numbers are equal."
fi
largest=0
(($1>$2)) && largest=$1 || largest=$2
}
read -p "Pls input two numbers: " n1 n2
echo "n1=$n1 n2=$n2"
max $n1 $n2
echo "The largest number is ${largest}."
5.编写一个httpd安装脚本
URL="https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2"
SRCPKG="httpd-2.4.43"
INSDIR=/apps/httpd
CONFDIR=/etc/httpd
systemctl disable --now firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "正在安装相关依赖包,请稍等..."
yum -y install wget gcc make autoconf ncurses-devel bzip2 apr-devel apr-util-devel pcre-devel openssl-devel &> /dev/null
if [ $? -eq 0 ];then
echo "安装成功!"
else
echo "安装错误,请检查相关依赖包。"
exit 1
fi
echo "下载源码包并解压,请稍后..."
(wget $URL;tar xvf $SRCPKG.tar.bz2 -C /usr/local/src) &>/dev/null
echo "编译并安装..."
cd /usr/local/src/$SRCPKG
./configure --prefix=$INSDIR --sysconfdir=$CONFDIR --enable-ssl
make -j 4 && make install
if [ $? -eq 0 ];then
echo "编译安装正常!"
else
echo "安装错误,请检查报错信息。"
exit 1
fi
sleep 1
echo "配置环境变量"
echo "PATH=$INSDIR/bin:$PATH" > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
echo "指定apache用户运行"
sed -i 's/User daemon/User apache/;s/Group daemon/Group apache/' $CONFDIR/httpd.conf
id apache &>/dev/null
if [ $? -eq 0 ];then
apachectl start
else
useradd -r -s /sbin/nologin -d /var/www -c Apache -u 48 apache && apachectl start
fi
echo "查看进程并测试"
ps -ef |grep httpd && curl 10.0.0.202

浙公网安备 33010602011771号