1. 计算100以内所有能被3整除的整数之和
答:
vim 1.sh
#!/bin/bash
#
#********************************************************************
#Author: jiquanquan
#QQ: 298007250
#Date: 2020-06-21
#FileName: 1.sh
#URL: http://www.example.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=0
for i in {1..100}; do
if !(let i%=3 ); then
let sum+=i
fi
done
echo sum=$sum
2. 编写脚本,求100以内所有正奇数之和
答:
vim sum-odd.sh
#!/bin/bash
#
#********************************************************************
#Author: jiquanquan
#QQ: 298007250
#Date: 2020-06-21
#FileName: sum-odd.sh
#URL: http://www.example.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=0
for i in `seq -s ' ' 1 2 100`; do
let sum+=i
done
echo sum=$sum
3. 随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
答:
#!/bin/bash
#
#********************************************************************
#Author: jiquanquan
#QQ: 298007250
#Date: 2020-06-21
#FileName: 3.sh
#URL: http://www.example.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
num=$[$RANDOM%10]
while read -p "请输入一个 0-9 之间的数字:" i ;do
if [[ $i =~ ^[0-9]+$ ]];then
if [ $i = $num ];then
echo "选择对了!"
break
elif [ $i -lt $num ];then
echo "数字选择小了,重新来!"
else
echo "数字选择大了,重新来!"
fi
else
echo "输入非数字:"
break
fi
done
4. 编写函数,实现两个数字做为参数,返回最大值
答:
定义函数
vim big
big () {
while read -p "输入两个数字,比大小:" i j;do
if [ $i -gt $j ];then
echo $i
break
else
echo $j
break
fi
done
}
脚本调用函数:
vim 4.sh
#!/bin/bash
#
#********************************************************************
#Author: jiquanquan
#QQ: 298007250
#Date: 2020-06-22
#FileName: 4.sh
#URL: http://www.example.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
. big
big
执行脚本:
bash 4.sh
5. 编写一个httpd安装脚本
答:
vim httpd.sh
#!/bin/bash
#
#********************************************************************
#Author: jiquanquan
#QQ: 298007250
#Date: 2020-06-22
#FileName: httpd.sh
#URL: http://www.example.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
dnf install -y wget
dnf install -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
wget https://downloads.apache.org/httpd/httpd-2.4.43.tar.bz2
dnf install -y bzip2
tar -xvf httpd-2.4.43.tar.bz2 -C /usr/local/src/
cd /usr/local/src/httpd-2.4.43/
./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl
make -j 4 && make install
echo 'PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
useradd -r -s /sbin/nologin -M -c apache -u 48 apache
sed -i s#"User daemon"#"User apache"#g /etc/httpd/httpd.conf
sed -i s#"Group daemon"#"Group apache"#g /etc/httpd/httpd.conf
apachectl