1. 计算 100 以内所有能被 3 整除的整数之和
#!/bin/bash
sum=0;
for (( i=1;i<=100;i++ ));
do
[[ $[i%3] == 0 ]] && let sum+=$i ;
done
echo $sum
2. 编写脚本,求 100 以内所有正奇数之和
#!/bin/bash
seq -s '+' {1,2,100} | xargs | bc
seq -s '+' {1,2,100} | xargs | bc
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
#!/bin/bash
num=$[RANDOM%10]
count=0
while :
do
read -p "一个1~10的随机数,你猜是多少:" guess
[[ $guess =~ [[:alpha:]] || $guess =~ [[:punct:]] ]] && echo "无效输入
" && exit
let count++
if [ $guess -gt $num ];then
echo "你猜大了"
exit
elif [ $guess -lt $num ];then
echo "你猜小了"
exit
else
echo "恭喜你猜对了"
exit
fi
done
4. 编写函数,实现两个数字做为参数,返回最大值
#!/bin/bash
maxnum(){
if [ $1 -gt $2 ];then
echo "大的数字是$1"
elif [ $1 -lt $2 ];then
echo "大的数字是$2"
else
echo "你输入的数字是相同的"
fi
if [ $1 -gt $2 ];then
echo "大的数字是$1"
elif [ $1 -lt $2 ];then
echo "大的数字是$2"
else
echo "你输入的数字是相同的"
fi
}
maxnum 12 23
maxnum 12 23
5. 编写一个httpd安装脚本
#!/bin/bash
rm -rf /usr/local/src/*
dnf remove -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
systemctl disabled --now firewalld.service
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/'/etc/selinux/config
setenforce 0
dnf install -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
wget https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2
tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src
cd /usr/local/src/httpd-2.4.43/
./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
make -j 4 && make install
echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
sed -i.bak 's/^User.*/User apache/;s/^Group.*/Group apache/;/^Server.*/a ServerName localhost:80' /etc/httpd/httpd.conf
userdel apache
useradd -r -s /sbin/nologin -d /var/www/ -c Apache -u 48 apache
apachectl start
rm -rf /usr/local/src/*
dnf remove -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
systemctl disabled --now firewalld.service
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/'/etc/selinux/config
setenforce 0
dnf install -y gcc make autoconf apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
wget https://mirror.bit.edu.cn/apache//httpd/httpd-2.4.43.tar.bz2
tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src
cd /usr/local/src/httpd-2.4.43/
./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
make -j 4 && make install
echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
sed -i.bak 's/^User.*/User apache/;s/^Group.*/Group apache/;/^Server.*/a ServerName localhost:80' /etc/httpd/httpd.conf
userdel apache
useradd -r -s /sbin/nologin -d /var/www/ -c Apache -u 48 apache
apachectl start
------------恢复内容结束------------