# 基础脚本

1.打印九九乘法口诀

```1 for i in `seq 9` ;do
2     for j in `seq 9` ;do
3         if [ \$i -ge \$j ];then
4             let sum=\${i}*\${j}
5             echo -e "\${i}x\${j}=\$sum\t\c"
6         fi
7     done
8     echo
9 done
```

2.检查磁盘空间，一旦发现任何分区利用率高于80%，就发出警报

``` 1 disk=`fdisk -l | egrep -o "^/dev/[[:alpha:]]+[0-9]"`
2 #disk=`lsblk | sed -rn "s/^├─([[:alpha:]]{3}[1-9]).*/\1/p"`
3 max_num=0
4
5 for name in `echo \$disk`;do
6      #有可能是交换空间，df命令就没有显示。直接跳过
7     if ! df | egrep -o "^\$name.*" &> /dev/null;then
8         continue
9     fi
10      #获取使用空间和inodes
11     num=`df | egrep -o "\$name.*" | tr -s " " "%" | cut -d "%" -f 5`
12     Inodes_num=`df -i | egrep -o "\$name.*" | tr -s " " "%" | cut -d "%" -f 5`
13     #使用空间和inodes大于80就发出警告。
14     if [ \$num -ge 80 -o \$Inodes_num -ge 80 ];then
15         if [ \$num -gt \$max_num ];then
16             max_num=\$num
17             max_name=\$name
18             max_Inodes_num=\$Inodes_num
19
20         elif [ \$num -eq \$max_num ];then
21             max_equal_num=\$num
22             max_equal_name=\$name
23             max_equal_Inodes_num=\$Inodes_num
24         fi
25      fi
26 done
27 #判断是否有超过80的
28 if [ \$max_num -ne 0 ];then
29     let remaining=100-\$max_num
30     let Inodes_remaining=100-\$max_Inodes_num
31
32     #判断是否有两个分区使用量一样
33     if [ -z \$max_equal_num ];then
34         wall The \$max_name disk is about to run out, Only the remaining disk is \${remaining}% and Inodes\${Inodes_remaining}%
35     else
36     wall The \$max_name and \$max_equal_name disk is about to run out, Only the remaining disk is \${remaining}% and Inodes\${Inodes_remaining}%
37     fi
38 fi
```

3.打印国际象棋

``` 1 for j in `seq 8`;do
2     if [ \$[\$j%2] -eq 0 ];then
3         for i in `seq 4`;do
4             echo -e "\e[41m \e[0m\e[43m \e[0m\c"
5         done
6     else
7          for i in `seq 4`;do
8             echo -e "\e[43m \e[0m\e[41m \e[0m\c"
9         done
10     fi
11     echo
12 done
```

4.生成十个随机数，并进行比较

``` 1 cat /dev/null > /tmp/suiji.txt
2
3 for i in {1..10};do
4     echo \$RANDOM >> /tmp/suiji.txt
5 done
6
7 max=`cat /tmp/suiji.txt | sort -n | tail -n 1`
8 min=`cat /tmp/suiji.txt | sort -n | head -n 1`
9
10 echo all random is `cat /tmp/suiji.txt | xargs`
11 echo max is \$max
12 echo min is \$min
13
14 rm -rf /tmp/suiji.txt
```

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

``` 1 for char in efbaf275cd 4be9c40b8b 44b2395c46 f8c8873ce0 b902c16c8b ad865d2f63;do
2
3     for num in `seq 32767`;do
4         k=`echo \${num} | md5sum | cut -c 1-10`
5         if [ \$k == \$char ];then
6             echo \$num
7             break
8         fi
9     done
10 done
```
6.在/testdir目录下创建10个html文件,文件名格式为数字N（从1到10）加随机8个字 母，如： 1AbCdeFgH.html
```for i in `seq 10`;do
#生成8个字母
name=`openssl rand -base64 20 | grep -o "[[:alpha:]]" | head -n 8 | xargs | tr -d " "`
#控制数字1~10
while true;do
num=\$[\$RANDOM%11]
if [ \$num -ne 0 ];then
break
fi
done
#创建文件
touch /testdir/\$num\$name.html
done
```
7.添加10个用户user1-user10，密码为8位随机字符
```1 for user in `echo test{1..10}`;do
2
3     id \$user &> /dev/null && echo "\$user exist" && continue
5     pass=`openssl rand -base64 12 | cut -c 1-10`
6     echo \$pass | passwd --stdin \$user &> /dev/null
7     echo -e "user:\$user\npasswd:\$pass"
8
9 done
```
8.计算100以内所有能被3整除的整数之和
```1 sum=0
2 for num in `seq 100`;do
3      if [ \$[\$num%3] -eq 0 ];then
4         let sum+=\$num
5     fi
6 done
7 echo "sum=\$sum"
```
9.编写脚本，提示输入正整数n的值，计算1+2+…+n的总和
```1 read -p "Please enter 1 to add to? : " n
2 sum=0
3
4 for num in `eval seq \$n`;do
5     let sum+=\$num
6 done
7
8 echo "sum=\$sum"
```
10.判断/var/目录下所有文件的类型
``` 1 read -p "Please enter the directory to be judged: " dir
2
3 if [[ "`echo \$dir | sed -rn "s/^.*(.)\$/\1/p"`" != "/" ]];then
4     if [[ ! -f \$dir ]];then
5         dir=\$dir/
6     else
7         echo -e "\e[31mPlease enter the directory\e[0m"
8         exit
9     fi
10 fi
11
12 for file in `ls \$dir`;do
13     if [[ -f \$dir\$file ]];then
14         echo "\$file is file"
15     elif [[ -d \$dir\$file ]];then
16         echo "\$file is direction"
17     elif [[ -c \$dir\$file ]];then
18         echo "\$file is character"
19     elif [[ -L \$dir\$file ]];then
21     else
22         echo "Unknown type"
23     fi
24 done
```
11.生成0-10的一个数，猜数字
``` 1 i=\$[\$RANDOM%11]
2 while true;do
4
5     if [ \$i -eq \$num ];then
6         echo "Congratulations, you guessed it."
7         exit
8     elif [ \$num -lt \$i ];then
9         echo -e "Sorry, the input is too small.\n"
10     else
11         echo -e "Sorry, the input is too big.\n"
12     fi
13 done
```
12.编写脚本，提示请输入网络地址，如192.168.0.0，判断输入的网段中主机在线状态
``` 1 read -p "Enter the ip address segment to be detected. Such as 192.168.100.0: " ip
2 net=`echo \$ip | sed -nr "s/^(([0-9]{1,3}\.){3}).*/\1/p"`
3
4 if [[ -z \$net ]];then
6     exit
7 fi
8
9 echo -e "\nStart detection at `date` o'clocki" >> /data/host_up.log
10 echo -e "\nStart detection at `date` o'clocki" >> /data/host_down.log
11
12 i=1
13 while [ \$i -le 10 ];do
14     if ping -c 1 -w 1 \$net\$i &> /dev/null;then
15         echo \$net\$i is up >> /data/host_up.log
16     else
17         echo \$net\$i is down >> /data/host_down.log
18      fi
19     let i++
20 done
```
```1 read -p "Input needs to monitor users: " user
2 while true ; do
3     if who | grep "^\$user" > /dev/null ;then
4         echo "\$user is logging in at `date '+%F %T'`" >> /data/login.log
5         exit
6     fi
7     sleep 3
8 done
```
14.用二个以上的数字为参数，显示其中的最大值和最小值
``` 1 for((i=1;i<=10;i++));do
2
3     read -p "Enter the number to compare(Enter end input): " num_\$i
4     val=`eval echo '\$'num_\$i`
5
6     if [[ -z \$val ]];th
7         break
8     fi
9
10     [ \$i -eq 1 ] && max=\$val
11     [ \$i -eq 2 ] && min=\$val
12
13     if [ \$max -le \$val ];then
14         max=\$val
15     fi
16
17     [ \$i -ge 2 ] && if [ \$min -ge \$val ];then min=\$val ;fi
18 done
19
20 echo "max=\$max;min=\$min"
```
15.求100以内所有正奇数之和
``` 1 odd_num=0
2 even_num=0
3
4 i=1
5 while [ \$i -le 100 ];do
6     if [ \$[\$i%2] -eq 0 ];then
7         let even_num+=i
8     else
9         let odd_num+=i
10     fi
11     let i++
12 done
13
14 echo odd number is \$odd_num
15 echo even is \$even_num
```
16.判断用户输入的参数是否为正整数
``` 1 read -p "Please enter a positive integer: " num
2 if [[ "\$num" =~ ^[0-9]+\$ ]];then
3     if [ \$num -gt 0 ];then
4         echo "You are entering a positive integer."
5     else
6         echo "0 is not a positive integer"
7     fi
8 else
9     echo "You are not entering a positive integer"
10 fi
```
17./etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件；分别读取每个文件，以K开头的输出为文件加stop，以S开头的输出为文件名加start，如K34filename stop S66filename start
```1 for file in `ls /etc/rc.d/rc3.d/`;do
2
3     if echo \$file | sed -rn "s/^(S.*)\$/\1/";then
4         echo "\$file start"
5     elif echo \$file | sed -rn "s/^(K.*)\$/\1/";then
6         echo "\$file stop"
7     fi
8
9 done
```
18.用文件名做为参数，显示其中的最大值和最小值
``` 1 file=1
2 until [[ "\$file" == "0" ]];do
3     read -p "Please enter the file to be counted(0 exit script): " file
4
5     if [[ -f \$file ]];then
6         echo -e "\$file The total number of lines is `wc -l < \$file`\n"
7         elif [[ "\$file" != "0" ]];then
8         echo -e "\$file not file\n"
9     fi
10
11 done
```
19.打印等腰三角形
``` 1 read -p "Please enter the height: " high
2 for row in `seq \$high`;do
3     let num_1=\$high-\$row
4     let num_2=2*\$row-1
5
6     for space in `seq \$num_1`;do
7         echo -e " \c"
8     done
9
10     for star in `seq \$num_2`;do
11         echo -e "*\c"
12     done
13
14     echo
15 done
```

20.编写脚本/root/bin/yesorno.sh，提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

```1 read -p "Please enter yes or no: " input
2
3 if [[ "\$input" =~ [yY]([eE][Ss]){,1} ]];then
4     echo "Your choice is yes"
5 elif [[ "\$input" =~ [Nn]([oO]]){,1} ]];then22:45:12
6     echo "Your choice is no"
7 else
8     echo "Your choice is neither yes nor no"
9 fi
```