第四周作业

1. 计算 100 以内所有能被 3 整除的整数之和

sum=0;for(i=1;i<=100;i++);do [$((i%3)) -eq 0] && let sum+=$i;done; echo $sum

2. 编写脚本,求 100 以内所有正奇数之和

#!/bin/bash

sum =0

for((i=1;i<100;i+=2));

do ((sum=sum+i));

done

echo $sum

3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出

#!/bin/bash

while true;do

  n=$((RANDOM%10+1))

  read -p "输入10以内数字:" num

  if[$num -eq $n];then

  echo '相等'

  exit

elif[$num -gt $n];then

 echo "输入数字大于随机数"

 exit

elif[$num -lq $n];then

 echo "输入数小于随机数"

fi

done

4. 编写函数,实现两个数字做为参数,返回最大值

#!/bin/bash

find_max(){

  if [ $1 -lt $2 ]; then

     echo "$2"

  else

    echo "$1"

 fi

}

find_max $1 $2

 

5. 编写一个httpd安装脚本

#!/bin/bash

target_dir=/usr/local/src
install_dir=/usr/local/httpd
rpm -qa | grep wget || yum install -y wget
wget -O $target_dir/httpd-2.4.43.tar.bz2 https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.43.tar.bz2

#安装依赖包
yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config

#添加apache用户
id apache &> /dev/null || useradd -r -u 80 -d /var/www -s /sbin/nologin apache

#解压源码包
tar xf $target_dir/httpd-2.4.43.tar.bz2 -C $target_dir
cd $target_dir/httpd-2.4.43

#编译安装
./configure --prefix=$install_dir --sysconfdir=/etc/httpd --enable-ssl
make -j`lscpu | grep "^CPU(s)" | awk '{print $NF}'` && make install

#设置环境变量
echo 'PATH='$install_dir'/bin:$PATH'  > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh

#修改配置文件
sed -ri 's#(User )daemon#\1apache#' /etc/httpd/httpd.conf
sed -ri 's#(Group )daemon#\1apache#' /etc/httpd/httpd.conf

#启动httpd服务
apachectl start

#检查firewalld状态
firewall_status=`systemctl status firewalld.service | grep "Active" | awk '{print $2}'`
if [ $firewall_status = active ];then
    echo "防火墙已启用,开放端口"
    firewall-cmd --permanent --add-service=http --add-service=https
    firewall-cmd --reload
fi

 

 
 

 

posted @ 2020-07-06 16:24  yi2020  阅读(106)  评论(0编辑  收藏  举报