1. 计算 100 以内所有能被 3 整除的整数之和
答:
#!/bin/bash
sum=0
for i in {1..100};do
let j=i%3
[ $j -eq 0 ] && let sum+=i
done
echo sum=$sum
2. 编写脚本,求 100 以内所有正奇数之和
答:
#!/bin/bash
sum=0
for i in {1..100..2};do
let sum+=i
done
echo sum=$sum
或
#!/bin/bash
sum=0
for ((i=1;i<=100;i+=2));do
let sum+=$i
done
echo sum=$sum
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
答:
#!/bin/bash
CB=" 33[1;31m"
CE=" 33[0m"
GNUM=$[RANDOM%10]
while read -p "输入一个10以内的数字": INPUTNUM; do
if [ $INPUTNUM -gt $GNUM ];then
echo -e "$CB你输入的数字大了,请继续!$CE"
continue
elif [ $INPUTNUM -lt $GNUM ];then
echo -e "$CB你输入的数字小了,请继续!$CE"
continue
elif [ $INPUTNUM -eq $GNUM ];then
echo -e "$CB你输入的数字刚好!$CE"
break
fi
done
4. 编写函数,实现两个数字做为参数,返回最大值
答:
max (){
if [ $1 -gt $2 ];then
echo "最大的数字是:" $1
else
echo "最大的数字是:" $2
}
max $*
5. 编写一个httpd安装脚本
答:
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i.bak 's@SELINUX@SELINUX=disabled@' /etc/selinux/config
reboot
#! /bin/bash
dnf -y install gcc make autoconf apr-devel apr-util-devel pcre-devel redhat-rpm-config openssl-devel
tar -xvf httpd-2.4.43.tar.bz2 -C /usr/local/src
cd /usr/local/src/httpd-2.4.43
./configure --prefix=/app/httpd --sysconfdir=/etc/httpd --enable-ssl
make -j 4 && make install
echo 'PATH=/app/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
useradd -r apache -c Apache -u 48 -d /var/www -s /sbin/nologin
sed -i.bak 's/User daemon/User apache/' /etc/httpd/httpd.conf
sed -i 's/Group daemon/Group apache/' /etc/httpd/httpd.conf
apachectl start