博客第9周
1. 计算 100 以内所有能被 3 整除的整数之和
#/bin/bash
sum=0
for i in {1..100};do
if [ 0 -eq $[$i%3] ];then
echo $i
let sum+=i;
fi
echo sum=$sum
done
或
seq -s+ 3 3 100|bc
2. 编写脚本,求 100 以内所有正奇数之和
#/bin/bash
sum=0
for i in {1..100};do
if [ 1 -eq $[$i%2] ];then
echo $i
let sum+=i;
fi
echo sum=$sum
done
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#/bin/bash
NUM=$[$RANDOM%10]
while read -p "请输入0-9之间任意1个数字:" INPUT;do
if [[ "$INPUT" != [0-9] ]];then
echo '请输入0-9之间任意1个数字'
else
if [ $NUM -gt $INPUT ];then
echo '你输入的数字小了'
elif [ $NUM -lt $INPUT ];then
echo '你输入的数字大了'
else
echo '恭喜你,猜对了!'
break;
fi
fi
done
4. 编写函数,实现两个数字做为参数,返回最大值
while read -p '请输入任意一个数字:' parm1;do
if [[ "$parm1" =~ [0-9] ]];then
echo $parm1
break
else
echo "请输入有效的任意一个数字"
fi
done
while read -p '请输入任意一个数字:' parm2;do
if [[ "$parm2" =~ [^0-9] ]];then
echo '请输入任意有效的数字'
else
echo $parm2
break
fi
done
function CompareNumbers () {
local num1=$parm1
local num2=$parm2
if [ $num1 -gt $num2 ];then
echo "$num1 > $num2"
exit
elif [ $num1 -lt $num2 ];then
echo $num2 > $num1
exit
else
exit
fi
}
5. 编写一个httpd安装脚本
#!/bin/bash
######
#Autho: tyzh
#Date: 2020-06-17
######
####安装前检查####
CurrentVersion=`httpd -v|head -1`
colorGreen="\033[32m"
colorRed="\033[31m"
colorEnd="\033[0m"
###判断是否有运行的httpd服务#####
netstat -tnlp|grep httpd
if [ $? = 1 ];then
echo "当前版本为:$CurrentVersion"
read -p "请再次确认,是否要安装httpd2.4.43(yes/no)? $colorend" INPUT
INPUT=`echo $INPUT|tr 'A-Z' 'a-z'`
case $INPUT in
n|no)
exit 1
;;
y|yes)
echo "安装httpd2.4.43准备安装......"
;;
*)
echo 'Input fales,plese input yes or no'
;;
esac
else
echo "警告需要先停掉目前服务,再继续安装,apachectl -k stop"
read -p "请再次确认,是否要安装httpd2.4.43(yes/no)?" INPUT
INPUT=`echo $INPUT|tr 'A-Z' 'a-z'`
case $INPUT in
n|no)
exit 1
;;
y|yes)
echo "安装httpd2.4.43准备安装......"
apachectl -k stop
;;
*)
echo 'Input fales,plese input yes or no'
;;
esac
fi
sleep 5s
####安装依赖####
echo -e "$colorGreen yilai install starting.....$colorEnd"
yum -y install gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
if [ $? = 0 ];then
echo -e "$colorGreen yilai install finished.....$colorEnd"
else
echo -e "$colorRed yilai install false,please check your repo source!$colorEnd"
sleep 10s
exit 1
fi
sleep 5s
######添加apache用户,组#####
group apache
useradd -r -s /sbin/nologin -d /var/www/html apache
####安装httpd2.4.43包####
echo -e "$colorGreen httpd-2.4.43 starting......$colorEnd"
wget https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2
tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src
cd /usr/local/src/httpd-2.4.43/
###配置httpd###
./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl
###编译并安装####
make -j 2 && make install
echo -e "$colorGreen httpd-2.4.43 finished......$colorEnd"
####httpd启动环境配置,启动###
echo PATH=/usr/local/httpd/bin:$PATH > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
sed -i 's/User daemon/User apache/' /etc/httpd/httpd.conf
sed -i 's/Group daemon/Group apache/' /etc/httpd/httpd.conf
###检查安装文件####
apachectl -t
####启动服务####
apachectl -k restart
echo 'httpd 服务已经启动.......'
echo $CurrentVersion