1. 计算 100 以内所有能被 3 整除的整数之和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: xuanlv #QQ: 360956175 #Date: 2020-06-22 #FileName: sum100.sh #URL: https://www.cnblogs.com/xuanlv-0413/ #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** SUM=0 for i in `seq 1 100`;do if [ $[$i%3] -eq 0 ];then SUM=$[$SUM+$i] fi done echo -e "E[1;32mSUM : $SUME[0m"
2. 编写脚本,求 100 以内所有正奇数之和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: xuanlv #QQ: 360956175 #Date: 2020-06-22 #FileName: sum_of_odd_positive.sh #URL: https://www.cnblogs.com/xuanlv-0413/ #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** I=1 odd_number(){ for i in `seq 1 100`;do if [ $[$i%2] -eq 1 ];then let SUM1+=i fi done } positive_number(){ while [ $I -le 100 ];do let SUM+=$I let I++ done } odd_number positive_number echo -e "E[1;32m正数总数和:$SUME[0m" echo -e "E[1;32m奇数总数和:$SUM1E[0m"
3. 随机生成 10 以内的数字,实现猜字游戏,提示比较大或小,相等则退出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: xuanlv #QQ: 360956175 #Date: 2020-06-21 #FileName: guess.sh #URL: https://www.cnblogs.com/xuanlv-0413/ #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** NUM=$[RANDOM%10] while read -p "输入10以内的数字:" INPUT ;do if [ $INPUT -gt "9" ]||[ $INPUT -le "0" ];then echo "请输入10以内的数字" exit elif [ $INPUT -eq $NUM ];then echo -e "E[1;32m恭喜你猜对了!!!E[0m" break elif [ $INPUT -gt $NUM ];then echo -e "E[1;31m数字太大了,重新猜吧!E[0m" else echo -e "E[1;31m数字太小了,重新猜吧!!E[0m" fi done
4. 编写函数,实现两个数字做为参数,返回最大值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: xuanlv #QQ: 360956175 #Date: 2020-06-22 #FileName: max_num.sh #URL: https://www.cnblogs.com/xuanlv-0413/ #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** max_num(){ if [ $# -ne 2 ];then echo -e "E[1;32m请输入两个数字为脚本参数E[0m" exit fi if [ $1 -gt $2 ];then echo "Max is $1" else echo "Max is $2" fi } max_num $*
5. 编写一个httpd安装脚本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: xuanlv #QQ: 360956175 #Date: 2020-06-18 #FileName: https2.sh #URL: https://www.cnblogs.com/xuanlv-0413/ #Description: The test script #Copyright (C): 2020 All rights reserved #******************************************************************** set -u # 变量 URL=https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/ FILE=httpd-2.4.43.tar.gz INSTALL_DIR=/apps/httpd2 RESD="E[$[RANDOM%7+31];1m" END="E[0m" # 包名 PACKAGE=`echo $FILE |sed -nr 's/(.*[0-9.]+).[[:alpha:]]+.*/1/p'` # 基名 BASEDIR=`basename $INSTALL_DIR` # 后缀名 SUFFIX=`echo httpd-2.4.43.tar.bz2 |sed -nr 's/.*.([^.]+)/1/p'` echo -en "$RESD----------install is httpd.service----------$END" sleep 3 # 安装依赖包 cd /usr/local/src/ yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config # 下载包 ping -W1 -c 2 www.baidu.com &> /dev/null if [ $? -eq 0 ]; then wget $URL$FILE case $SUFFIX in gz|bz2|xz|tar) tar -xvf $FILE ;; zip) unzip $FILE ;; *) echo "后缀识别不了,错误" exit 2 esac else echo -e "E[31m网络不通!!E[0m" cp /root/$FLIE /usr/local/src/ fi # 编译安装 cd $PACKAGE ./configure --prefix=$INSTALL_DIR --sysconfdir=/etc/$BASEDIR make && make install # 创建一个启动Apache用户 useradd -r -s /sbin/nologin -u 50 apache # 修改配置文件用户 sed -ri -e 's/^(User).*/1 apache/' -e 's/^(Group).*/1 apache/' /etc/$BASEDIR/httpd.conf echo "PATH=$INSTALL_DIR/bin:$PATH" > /etc/profile.d/$BASEDIR.sh source /etc/profile.d/$BASEDIR.sh sed -ri 's#(^.*<h1>)(.*)(</h1>.*$)#16.18 Go! 3 #' $INSTALL_DIR/htdocs/index.html apachectl start cd .. rm -rf $FILE $PACKAGE echo -en "$RESD----------httpd.service is finished----------$END"
端口被占用,只能修改端口