N46期第四周作业
1. 计算 100 以内所有能被 3 整除的整数之和
sum=0;for((i=0;i<=100;i++));do [ $[$i%3] -eq 0 ] && ((sum=$sum+$i));done;echo $sum
2. 编写脚本,求 100 以内所有正奇数的之和
1.
#!/bin/bash
sum=0
for i in `seq 100`
do
if [ $[$i%2] -ne 0 ];then
sum=$[$sum+$i]
fi
done
echo $sum
2.
#!/bin/bash
sum=0
for i in `seq 1 2 100`
do
sum=$[$sum+$i]
done
echo $sum
3.
#!/bin/bash
echo `seq 1 2 100`|tr -t " " "+"|bc
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
num=`echo $[$RANDOM%10]`
while :;
do
read -p "请输入10以内的数字:" input
if [ $num -eq $input ];then
break
elif [ $num -gt $input ];then
echo "猜的数字小了"
else
echo "猜的数字大了"
fi
done
4. 编写函数,实现两个数字做为参数,返回最大值
GET_max(){
if [ $1 -gt $2 ];then
echo "最大值是$1"
else
echo "最大值是$2"
fi
}
5. 编写一个httpd安装脚本
#!/bin/bash colour="echo -e \033[1;32m" end="\033[0m" cpu=`lscpu|sed -n '/^CPU(s)/p'|tr -s " "|cut -d " " -f2` $colour 安装httpd $end yum install apr-devel apr-util-devel pcre-devel openssl-devel -y &> fail-1.log if [[ $? =~ 0 ]];then $colour 相关组件安装完成 $end cd /usr/local/src/ $colour 开始下载安装文件 $end wget https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2 &> /dev/null tar -xf httpd-2.4.43.tar.bz2 cd httpd-2.4.43/ ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl &> fail-2.log if [[ $? =~ 0 ]];then $colour ./configure 完成 $end make -j $cpu &>fail-3.log if [[ $? =~ 0 ]];then $colour make 完成 $end make install &> fail-4.log if [[ $? =~ 0 ]];then $colour make install 完成 $end echo "PATH=$PATH:/usr/local/httpd/bin" > /etc/profile.d/httpd.sh source /etc/profile.d/httpd.sh useradd -r -s /sbin/nologin -d /usr/local/httpd apache cd /etc/httpd/ cp httpd.conf httpd.conf.bak sed -i "s#daemon#apache#g" /etc/httpd/httpd.conf echo "安装完成" else $colour make install 完成 $end fi else $colour make 失败,请检查 $end fi else $colour ./configure 失败 $end fi else $colour 组件安装失败,请检查 $end fi