2、编写脚本 ​​createuser.sh​​,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-20
#FileName: creatuser.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
if [ -z $1 ];then
echo -e "${RED}请输入用户名${END}"
elif id $1 &> /dev/null ;then
echo -e "${RED}用户已存在${END}"
exit 1
else
useradd $1 &> /dev/null && echo -e "${GREEN}用户已创建${END}"
echo "123456" |passwd --stdin $1 &> /dev/null && echo -e "${GREEN}密码已创建${END}"
passwd -e $1 &> /dev/null && echo -e "${RAN}${1}下次登陆需修改密码${END}"
id $1
fi

2、编写脚本 ​​yesorno.sh​​,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-20
#FileName: yesorno.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
read -t 6 -p "请输入yes或no:" YN
echo $YN | tr A-Z a-z
case $YN in
y|yes)
echo -e "${RAN}您输入的是yes${END}"
;;
n|no)
echo -e "${RAN}您输入的是no${END}"
;;
*)
echo -e "${RAN}输入错误${END}"
esac

3、​​编写脚本filetype.sh​​,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-20
#FileName: filetype.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
read -p "请输入文件:" File
Dirpath=`dirname $File`
Basepath=`basename $File`

if [ ${Dirpath} = \. ];then
Allpath=`pwd`/$Basepath
else
Allpath=$Dirpath/$Basepath
fi
if [ -f $Allpath ];then
if [[ $Allpath =~ .*\.sh ]];then
echo -e "${RAN}$Allpath 是个sh脚本文件${END}"
else
echo -e "${RAN}${Allpath} 是个普通文件${END}"
fi
elif [ -d $Allpath ];then
echo -e "${RAN}${Allpath} 是个目录文件${END}"
elif [ -L $Allpath ];then
echo -e "${RAN}${Allpath} 是个链接文件${END}"
else
echo -e "${RAN}${Allpath} 是其他类型文件${END}"
fi

[root@rocky8 scripts]# ./filetype.sh
请输入文件:/etc/passwd
/etc/passwd 是个普通文件
[root@rocky8 scripts]# ./filetype.sh
请输入文件:/etc/init.d
/etc/init.d 是个目录文件
[root@rocky8 scripts]# ./filetype.sh
请输入文件:y^[[C
/root/scripts/yes 是其他类型文件
[root@rocky8 scripts]# ./filetype.sh
请输入文件:yesorno.sh
/root/scripts/yesorno.sh 是个sh脚本文件

4、编写脚本 ​​checkint.sh​​,判断用户输入的参数是否为正整数

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-20
#FileName: checkint.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
read -p "请输入数字:" Digit
if [[ $Digit =~ ^[^0-9]+$ ]];then
echo -e "${RAN}请输入正确的数字${END}"
exit
elif [[ $Digit =~ ^[0]+$ ]];then
echo -e "${RAN}您输入的是0,非正整数${END}"
elif [[ $Digit =~ ^[0-9]+$ ]];then
echo -e "${RAN}您输入的是正整数${END}"
exit 1
elif [[ $Digit =~ ^-[0-9]+$ ]];then
echo -e "${RAN}您输入的是负整数${END}"
exit 2
elif [[ $Digit =~ ^-?[0-9]\.[0-9]*$ ]];then
echo -e "${RAN}您输入的是浮点数数字${END}"
exit 3
fi
[root@rocky8 scripts]# ./checkint.sh
请输入数字:5
您输入的是正整数
[root@rocky8 scripts]# ./checkint.sh
请输入数字:-6
您输入的是负整数
[root@rocky8 scripts]# ./checkint.sh
请输入数字:0.35
您输入的是浮点数数字
[root@rocky8 scripts]# ./checkint.sh
请输入数字:dsaf
请输入正确的数字
[root@rocky8 scripts]# ./checkint.sh
请输入数字:0
您输入的是0,非正整数

5、编写脚本 ​​reset.sh​​,实现系统安装后的初始化环境,包括:1、别名 2、环境变量,如PS1等 3、安装常用软件包,如:tree 5、实现固定的IP的设置,6、vim的设置等

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: reset.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
setPS1() {
echo 'PS1="\[\e[1;32m\][\[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ "' > /etc/profile.d/env.sh && echo -e "${RAN}PS1提示符已设置${END}"
}
setHostname() {
select host in 临时更改主机名 永久更改主机名 退出;do
case $host in
临时更改主机名)
read -p "请输入主机名(别整那些无效的字符):" hostname
hostname $hostname && echo -e "${RAN}主机名已更改为${hostname},临时生效${END}"
;;
永久更改主机名)
read -p "请输入主机名(别整那些无效的字符):" hostname
hostnamectl set-hostname $hostname && echo -e "${RAN}主机名已更改${hostname},重进shell生效${END}"
;;
退出)
break
;;
esac
done
}
setFirewalld() {
systemctl disable --now firewalld && echo -e "${RAN}防火墙已关闭${END}"
}
setSelinux() {
sed -i.bak 's/^(SELINUX=).*/\1disabled/'/etc/selinux/config && echo -e "${RAN}selinux已关闭${END}"
}
PS3="请选择:"
select menu in 别名 PS1 设置主机名 关闭防火墙 关闭selinux 执行全部 退出 ;do
case $menu in
别名)
export alias rm=$(DIR=/data/`date +%F_%T`;mkdir -p $DIR;mv -t $DIR) && echo -e "${RAN}rm别名已设置${END}"
;;
PS1)
setPS1
;;
设置主机名)
setHostname
;;
关闭防火墙)
setFirewalld
;;
关闭selinux)
setSelinux
;;
执行全部)
setAlias
setPS1
setHostname
setFirewalld
setSelinux
;;
退出)
exit
;;
esac
done

6、判断/var/目录下所有文件的类型

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: var.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
for wenjian in $(find /var) ;do
file $wenjian >> /root/scripts/varfile.txt
done
cat /root/scripts/varfile.txt

7、添加10个用户user1-user10,密码为8位随机字符

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: useradd.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/profile.d/color.sh
for i in user{1..10};do
id $i &> /dev/null && echo -e "${RAN}User is exists${END}" || { useradd $i && echo -e "${RAN}${i} create ok${END}"; }
{ echo "$(cat /dev/urandom |tr -dc '[:alnum:][:punct:]'|head -c8):${i}" | tee -a /root/scripts/useradd.txt |chpasswd $i &> /dev/null ; } && echo -e "${RAN}密码已创建,请查看useradd.txt文件${END}"
done
cat useradd.txt
[root@rocky8 scripts]# ./useradd.sh
user1 create ok
user2 create ok
user3 create ok
user4 create ok
user5 create ok
user6 create ok
user7 create ok
user8 create ok
user9 create ok
user10 create ok
1Rf>Hs;]:user1
3<~Fy<Ou:user2
Dt6}GVLM:user3
N18aM?=0:user4
P_e"4.l::user5
=g2L9B&R:user6
I$S<;U)5:user7
n=3H~t'W:user8
z$3xj#08:user9
{41m-zO_:user10

8、(在ubuntu下完成,先完成以下操作)

[root@ubuntu2004-1 rc5.d]# systemctl disable nginx

/etc/rc.d/rc5.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K01nginx stop S01atd start

#!/bin/bash
Dir="/etc/rc5.d"
cd $Dir
for i in * ;do
if [[ $i =~ ^K.* ]];then
echo -e "$i stop"
elif [[ $i =~ ^S.* ]];then
echo -e "$i start"
else
echo -e "啥也不是"
fi
done
[root@ubuntu2004 ~]# ./rc5.sh
K01apache-htcacheclean stop
S01apache2 start
S01apport start
S01atd start
S01autofs start
...

9、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: sum.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入整数:" num
if [[ $num =~ ^[0]+$ ]];then
echo "请输入正整数,您输入的是$num"
elif [[ ! $num =~ ^[0-9]+$ ]];then
echo "请输入正整数,您输入的是$num"
else
j=0
for (( i=1;i<=num;i++ )) ;do
let j+=i
done
echo $j
fi
[root@rocky8 scripts]# ./sum.sh
请输入整数:3
6
[root@rocky8 scripts]# ./sum.sh
请输入整数:0.5
请输入正整数,您输入的是0.5
[root@rocky8 scripts]# ./sum.sh
请输入整数:dsfa
请输入正整数,您输入的是dsfa
[root@rocky8 scripts]# ./sum.sh
请输入整数:145689
10612715205

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

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: 100_3.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
for i in {1..100};do
if [ $(($i%3)) -eq 0 ];then
let sum+=i
fi
done
echo "100以内所有能被3整除的整数之和:$sum"
[root@rocky8 scripts]# ./100_3.sh
100以内所有能被3整除的整数之和:1683

11、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: scan_net.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入网络地址,如:192.168.0.0 ==>>" IP
[[ $IP =~ ^(([0]|[1-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-4])\.){3}([0]|[1-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-4])$ ]] || { echo -e "请输入合法的IP地址段" ;exit ; }
ipnet=`echo $IP |egrep -o ".*\."`
for ((i=1;i<=254;i++));do
if ping -c1 ${ipnet}${i} &> /dev/null ;then
echo -e "${ipnet}${i} is UP"
else
echo -e "${ipnet}${i} is DOWN"
fi
done

12、打印九九乘法表

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: 9x9.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
i=1
while ((i<=9 ));do
for ((j=1;j<=i;j++));do
printf "%sx%s=%-2s " $i $j $[i*j]
done
let i++
echo
done
[root@rocky8 scripts]# ./9x9.sh
1x1=1
2x1=2 2x2=4
3x1=3 3x2=6 3x3=9
4x1=4 4x2=8 4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81

13、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: create_html.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
DIR=/root/test
for((i=1;i<=10;i++));do
ran=`cat /dev/urandom |tr -dc '[:alpha:]'|head -c8`
touch ${DIR}/${i}${ran}.html
done
[root@rocky8 scripts]# ll ../test
total 0
-rw-r--r-- 1 root root 0 Jul 21 22:32 10aEaPaWUi.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 1PinofTTo.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 2TafbWeUQ.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 3UpXerOft.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 4CNaFZghc.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 5iEGnAOuP.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 6sYVnbcWt.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 7NdJjxRtR.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 8xyGjazkt.html
-rw-r--r-- 1 root root 0 Jul 21 22:32 9LOzuBqys.html

14、打印等腰三角形

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: dysj.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入等腰三角形的行数:" n
for ((i=1;i<=n;i++));do
for ((j=0;j<=n-i;j++));do
echo -en " "
done
for ((h=1;h<=i*2-1;h++));do
echo -en "\e[1;5;$[RANDOM%7+31]m*\e[0m"
done
echo
done
[root@rocky8 scripts]# ./dysj.sh
请输入等腰三角形的行数:5
*
***
*****
*******
*********

15、猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: taozi.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
j=1
for ((i=1;i<10;i++));do
let j=$[($j+1)*2]
done
echo $j
[root@rocky8 scripts]# ./taozi.sh
1534

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

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: zhengqishu.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
sum=0
for i in {1..100..2};do
let sum+=i
done
echo $sum
[root@rocky8 scripts]# ./zhengqishu.sh
2500

17、编写脚本,提示请输入网络地址,如:192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-21
#FileName: ipscan.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入网络地址,如:192.168.0.0 ==>>" IP
[[ $IP =~ ^(([0]|[1-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-4])\.){3}([0]|[1-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-4])$ ]] || { echo -e "请输入合法的IP地址段" ;exit ; }
ipnet=`echo $IP |egrep -o ".*\."`
for ((i=1;i<=254;i++));do
if ping -c1 ${ipnet}${i} &> /dev/null ;then
echo -e "${ipnet}${i} is UP" |tee -a /root/scripts/ipup.txt
else
echo -e "${ipnet}${i} is DOWN"|tee -a /root/scripts/ipdown.txt
fi
done
echo -e "在线IP数:`cat /root/scripts/ipup.txt|wc -l`"
echo -e "离线IP数:`cat /root/scripts/ipdown.txt|wc -l`"

[root@rocky8 scripts]# ./ipscan.sh
请输入网络地址,如:192.168.0.0 ==>>10.0.0.0
10.0.0.1 is UP
10.0.0.2 is UP
10.0.0.3 is DOWN
10.0.0.4 is DOWN
...
10.0.0.253 is DOWN
10.0.0.254 is DOWN
在线IP数:6
离线IP数:394

18、编写脚本,打印九九乘法表

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: 999.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
for ((i=1;i<=9;i++));do
for ((j=1;j<=i;j++));do
printf "%sx%s=%-2s " $j $i $[j*i]
done
echo
done
[root@rocky8 scripts]# ./999.sh
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

19、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: ranmax.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
max=0
min=99999999
for i in {1..10};do
ran=`echo $RANDOM`
if [ $ran -gt $max ];then
max=$ran
elif [ $ran -lt $min ];then
min=$ran
fi
done
echo "max $max"
echo "min $min"
[root@rocky8 scripts]# ./ranmax.sh
max 29213
min 3760

20、编写脚本,实现打印国际象棋棋盘

!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: xiangqi.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入棋盘的行数:" n
for ((i=1;i<=n;i++));do
for ((j=1;j<=n;j++));do
if [ $[($i+$j)%2] -eq 1 ];then
echo -e "\033[1;47m \033[0m\c"
else
echo -e "\033[1;45m \033[0m\c"
fi
done
echo
done


21、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令: echo $RANDOM|md5sum|cut -c1-10 后的结果,请破解这些字符串对应的RANDOM值

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: pojieran.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
char=(efbaf275cd 4be9c40b8b 44b2395c46 f8c8873ce0 b902c16c8b ad865d2f63)
for i in ${char[*]};do
while true;do
R=$RANDOM
ran=$(echo $R | md5sum | cut -c1-10)
[[ $ran == $i ]] && { echo -e "$R =====> $ran" |tee -a CharRan.txt;break ; }
done
done
[root@rocky8 scripts]# ./pojieran.sh
15000 =====> efbaf275cd
12000 =====> 4be9c40b8b
9000 =====> 44b2395c46
6000 =====> f8c8873ce0
3000 =====> b902c16c8b
1000 =====> ad865d2f63

22、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: hacker.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
while true;do
if who | egrep "^hacker\>" &> /dev/null ; then
{ who |egrep "^hacker\>"|tr -s " " |cut -d" " -f1,3,4 >> /var/log/login.log ; }&& exit 3
fi
sleep 3
done
[root@rocky8 scripts]# tail -1 /var/log/login.log
hacker 2022-07-22 21:52

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

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: caishuzi.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
ran=`echo $[RANDOM%9+1]`
while : ;do
read -p "请输入数字:" digit
echo $digit
if [ $digit -eq "$ran" ];then
echo -e "您猜对了"
exit
elif [ $digit -gt $ran ];then
echo -e "您猜大了"
else
[[ $digit -lt $ran ]]
echo -e "您猜小了"
fi
done
[root@rocky8 scripts]# ./caishuzi.sh
请输入数字:8
8
您猜大了
请输入数字:5
5
您猜对了

24、用文件名做为参数,统计所有参数文件的总行数

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: countfile.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
[ $# -eq 0 ] && { echo "请输入最少一个文件名为参数" ; exit ; }
sum=0
for i in `seq $#` ; do
let sum=$sum+`cat ${!i} |wc -l`
done
echo $sum
[root@rocky8 scripts]# ./countfile.sh ipdown.txt ipup.txt
400
[root@rocky8 scripts]# cat ipdown.txt ipup.txt |wc -l
400

25、用二个以上的数字为参数,显示其中的最大值和最小值

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-22
#FileName: 2digit.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
[ $# -lt 2 ] && { echo "最少输入两个数字作为参数" ; exit; }
max=0
min=99999999
for i in $@;do
if [ $i -gt $max ];then
max=$i
elif [ $i -lt $min ];then
min=$i
fi
done
echo "max=$max"
echo "min=$min"
[root@rocky8 scripts]# ./2digit.sh 9 8 10 10 45 879 321 54 864 3125 8546 3546 31564 31254 51321 666666 1111
max=666666
min=8

26、编写函数,实现OS的版本判断

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: OS.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
OS_version () {
version=`cat /etc/os-release | head -2|cut -d '"' -f2|xargs`
if [[ $version =~ "Rocky Linux 8" ]] ;then
echo -e "OS is Rocky, Version is $version"
elif [[ $version =~ Ubuntu ]];then
echo -e "OS is Ubuntu,Version is $version"
elif [[ $versuib =~ Centos ]];then
echo -e "OS version is Centos"
fi
}
OS_version
[root@rocky8 scripts]# ./OS.sh
OS is Rocky, Version is Rocky Linux 8.6 (Green Obsidian)

27、编写函数,实现取出当前系统eth0的IP地址

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: cutIP.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
CUT_IP () {
IFACE=`ifconfig |head -1|cut -d: -f1`
ensIP=`ifconfig $IFACE | sed -rn '2s/.* inet ([0-9.]+) .*/\1/p'`
echo -e "${IFACE}网卡地址是:${ensIP}"
}
CUT_IP
[root@rocky8 scripts]# ./cutIP.sh
ens160网卡地址是:10.0.0.151

28、编写函数,实现打印绿色OK和红色FAILED

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: printOK.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/init.d/functions
OK_FAILED() {
action "正确" true
action "错误" false
}
OK_FAILED
[root@rocky8 scripts]# ./printOK.sh
正确 [ OK ]
错误 [FAILED]

29、编写函数,实现判断是否无位置参数,如无参数,提示错误

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: judgment.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/init.d/functions
Jugement() {
if [ $# -lt 1 ];then
action "请输入参数" false
else
action "您输入了$#个参数" true
fi
}
Jugement $@

[root@rocky8 scripts]# ./judgment.sh
请输入参数 [FAILED]
[root@rocky8 scripts]# ./judgment.sh 234
您输入了1个参数 [ OK ]

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

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: maxdigit.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
maxdigit() {
if [ $1 -gt $2 ];then
echo "最大值"$1
else
echo "最大值"$2
fi
}
maxdigit $@
[root@rocky8 scripts]# ./maxdigit.sh 5642 54
最大值5642

31、编写服务脚本/root/bin/testsrv.sh,完成如下要求

(1) 脚本可接受参数:start, stop, restart, status

(2) 如果参数非此四者之一,提示使用格式后报错退出

(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功” 。考虑:如果事先已经启动过一次,该如何处理?

(4) 如是stop:则删除/var/lock/subsys/SCRIPT_NAME, 并显示“停止完成” 。考虑:如果事先已然停止过了,该如何处理?

(5) 如是restart,则先stop, 再start 。考虑:如果本来没有start,如何处理?

(6) 如是status, 则如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME is running...”,如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is stopped...”

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: testsrv.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/init.d/functions
dir=/var/lock/subsys
file=`echo $0|cut -d'/' -f2`
if [[ ! $1 =~ ^(start|stop|restart|status)$ ]];then
echo -e "参数错误,请重新输入"
exit 1
elif [[ $1 =~ ^start$ ]];then
[ -e "${dir}/${file}" ] && echo -e "已启动,请勿重复启动" || { touch "${dir}/${file}" ;action "启动成功" true; }
elif [[ $1 =~ ^stop$ ]];then
[ ! -e "${dir}/${file}" ] && echo -e "已停止,请勿重复操作" || { rm -f "${dir}/$file" ; action "停止成功" false; }

elif [[ $1 =~ ^restart$ ]];then
[ ! -e "${dir}/${file}" ] && echo -e "服务未启动,请先启动" || { rm -f "${dir}/$file" && touch "${dir}/$file" && action "重启成功" true; }
elif [[ $1 =~ ^status$ ]];then
[ -e "${dir}/${file}" ] && echo -e "$file is running...." || echo -e "$file is stopped...."
fi
[root@rocky8 scripts]# ./testsrv.sh st
参数错误,请重新输入
[root@rocky8 scripts]# ./testsrv.sh start
启动成功 [ OK ]
[root@rocky8 scripts]# ./testsrv.sh stop
停止成功 [FAILED]
[root@rocky8 scripts]# ./testsrv.sh restart
重启成功 [ OK ]
[root@rocky8 scripts]# ./testsrv.sh status
testsrv.sh is running....
[root@rocky8 scripts]# ./testsrv.sh stop
停止成功 [FAILED]
[root@rocky8 scripts]# ./testsrv.sh status
testsrv.sh is stopped....

32、编写脚本/root/bin/copycmd.sh

(1) 提示用户输入一个可执行命令名称

(2) 获取此命令所依赖到的所有库文件列表

(3) 复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下

如:/bin/bash ==> /mnt/sysroot/bin/bash

/usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd

(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下: 如:/lib64/ld-linux-x86-64.so.2 ==> /mnt/sysroot/lib64/ld-linux-x86-64.so.2

(5)每次复制完成一个命令后,不要退出,而是提示用户键入新的要复制的命令,并重复完成上述。功能;直到用户输入quit退出

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: copycmd.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
. /etc/init.d/functions
while true;do
read -p "请输入一个可执行的命令:" Command
[[ $Command == quit ]] && exit
comPath=`which --skip-alias $Command`
dirName=$( dirname $comPath)
baseName=$(basename $comPath)
lddPath=$(ldd $comPath | egrep -o '/[^[:space:]]+')
echo ${comPath} &> /dev/null || echo -e "您输入的命令不存在"
copycom() {
[ -d /mnt/sysroot$dirName ] || mkdir -p /mnt/sysroot$dirName
[ -d /mnt/sysroot$dirName/$comPath ] || { cp -a $comPath /mnt/sysroot$dirName/ ; action "命令复制成功" true; }
}
copylib() {
[ -d /mnt/sysroot/lib64 ] || mkdir -p /mnt/sysroot/lib64
cp -a $lddPath /mnt/sysroot/lib64 ; action "命令复制成功" true;

}
copycom
copylib
done
[root@rocky8 mnt]# tree
.
├── hgfs
└── sysroot
├── bin
│ ├── ls
│ └── mv
└── lib64
├── ld-linux-x86-64.so.2 -> ld-2.28.so
├── libacl.so.1 -> libacl.so.1.1.2253
├── libattr.so.1 -> libattr.so.1.1.2448
├── libcap.so.2 -> libcap.so.2.48
├── libc.so.6 -> libc-2.28.so
├── libdl.so.2 -> libdl-2.28.so
├── libpcre2-8.so.0 -> libpcre2-8.so.0.7.1
├── libpthread.so.0 -> libpthread-2.28.so
└── libselinux.so.1
[root@rocky8 scripts]# ./copycmd.sh
请输入一个可执行的命令:ls
命令复制成功 [ OK ]
命令复制成功 [ OK ]
请输入一个可执行的命令:mv
命令复制成功 [ OK ]
命令复制成功 [ OK ]
请输入一个可执行的命令:quit

33、斐波那契数列又称黄金分割数列,因数学家列昂纳多·斐波那契以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2),利用函数,求n阶斐波那契数列

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: tuzishulie.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
read -p "请输入数字:" n
[ $n -lt 2 ] && { echo "请输入大于2的数字"; exit; }
args=(0 1)
tuzi() {
for i in `seq $[$n-2]`;do
#args=(0 1 1 2)
n1=`echo ${args[$[${#args[*]}-1]]}`
n2=`echo ${args[$[${#args[*]}-2]]}`
args[${#args[*]}]=$[$n1+$n2]
done
echo ${args[*]}
}
tuzi
[root@rocky8 scripts]# ./tuzishulie.sh
请输入数字:14
0 1 1 2 3 5 8 13 21 34 55 89 144 233

34、编写一个猜数字的游戏,实在以下功能。

#写一个猜数字游戏,填4个数字,猜中多少按以下方式输出 #如0359

#若数字对,且位置对,则提示nA #如数字对,但位置不对,则提示nB #比如,猜0297,则应提示1A1B #比如,猜1359,则应提示3A0B #比如,猜3958,则应提示0A3B #如果猜中,则输出“猜中了,你用了n次”

#!/bin/bash
#
#********************************************************************
#Author: wangdayu
#QQ: 965507991
#Date: 2022-07-23
#FileName: digitgame.sh
#URL: https://blog.51cto.com/dayu
#Description: test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
while true;do
ran1=`echo $[RANDOM%10]`
ran2=`echo $[RANDOM%10]`
ran3=`echo $[RANDOM%10]`
ran4=`echo $[RANDOM%10]`
[ $ran1 = $ran2 ] && { echo "随机数重复" ; continue ; }
[ $ran2 = $ran3 ] && { echo "随机数重复" ; continue ; }
[ $ran3 = $ran4 ] && { echo "随机数重复" ; continue ; }
[ $ran1 = $ran3 ] && { echo "随机数重复" ; continue ; }
[ $ran1 = $ran4 ] && { echo "随机数重复" ; continue ; }
[ $ran2 = $ran4 ] && { echo "随机数重复" ; continue ; }
break
done
C=0
while true;do
read -n 1 Num1;read -n 1 Num2;read -n 1 Num3;read -n 1 Num4;
A=0
B=0
let C+=1
[[ Num1 = Num2 ]] && { echo "不能有重复数" ; continue ; }
[[ Num1 = Num3 ]] && { echo "不能有重复数" ; continue ; }
[[ Num1 = Num4 ]] && { echo "不能有重复数" ; continue ; }
[[ Num2 = Num3 ]] && { echo "不能有重复数" ; continue ; }
[[ Num2 = Num4 ]] && { echo "不能有重复数" ; continue ; }
[[ Num3 = Num4 ]] && { echo "不能有重复数" ; continue ; }
if [ $Num1$Num2$Num3$Num4 = $ran1$ran2$ran3$ran4 ];then
break
fi
if [ $Num1 = $ran1 ];then
let A+=1
elif [ $Num1 = $ran2 ];then
let B+=1
elif [ $Num1 = $ran3 ];then
let B+=1
elif [ $Num1 = $ran4 ];then
let B+=1
fi
if [ $Num2 = $ran2 ];then
let A+=1
elif [ $Num2 = $ran1 ];then
let B+=1
elif [ $Num2 = $ran3 ];then
let B+=1
elif [ $Num2 = $ran4 ];then
let B+=1
fi
if [ $Num3 = $ran3 ];then
let A+=1
elif [ $Num3 = $ran1 ];then
let B+=1
elif [ $Num3 = $ran2 ];then
let B+=1
elif [ $Num3 = $ran4 ];then
let B+=1
fi
if [ $Num4 = $ran4 ];then
let A+=1
elif [ $Num4 = $ran1 ];then
let B+=1
elif [ $Num4 = $ran2 ];then
let B+=1
elif [ $Num4 = $ran3 ];then
let B+=1
fi
echo ${A}A${B}B
done
echo "您一共猜了${C}次"