马哥博客作业第四周

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

 sum=0;for i in {0..100..3};do ((sum+=i)); done;echo $sum

 

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

#!/bin/bash
sum=0
for i in {1..100..2};do
        ((sum+=i));
done
echo $sum

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

#! /bin/bash
i=$(($RANDOM%10+1))
echo i是一个小于等于10的正整数...
echo 你能猜出i是几吗?
echo 请输入一个整数...
while true;do
        read guess
        if [[ ! $guess =~ ^[0-9]+$ ]]; then
                echo 请输入一个正整数
                continue
        fi
        if [ $guess -eq $i ]; then
                echo 你猜对了
                exit
        elif [ $guess -lt $i ]; then
                echo i比你猜的要大
        elif [ $guess -gt $i ]; then
                echo i比你猜的要小
        fi
done

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

max() { return $(($1>=$2?$1:$2)); }

 

5. 编写一个httpd安装脚本

 

#! /bin/bash

#download the source code
download_dir=/usr/local/src
wget $download_dir/httpd-2.4.43.tar.bz2 https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2

#install dependencies
dnf install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config

#add user
id apache &> /dev/null || useradd -r -u 80 -d /var/www -s /sbin/nologin apache

#unzip and make
tar xf $download_dir/httpd-2.4.43.tar.bz2 -C $download_dir
cd $download_dir/httpd-2.4.43
./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl
make && make install

#set PATH and configure
echo 'PATH=$download_dir/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
sed -ir 's/^(User).*/1 apache/' /etc/httpd/httpd.conf
sed -ir 's/^(Group).*/1 apache/' /etc/httpd/httpd.conf

apachectl start

 

 

原文地址:https://www.cnblogs.com/gehaibao/p/13171274.html