for循环语句示例

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

完整脚本

  1. [root@centos73 ~]# cat shell_scripts/filetype.sh
  2. #!/bin/bash
  3. #Author=wang
  4. RED="\033[31m"
  5. YELLOW="\033[0;33m"
  6. CYAN="\033[36m"
  7. PURPLE="\033[0;35m"
  8. RESET="\033[0m"
  9. if [ $# -ne 1 ] ; then
  10. echo -e "$RED you must enter a parameter$RESET"
  11. exit 1
  12. fi
  13.  
  14. file=$1
  15. type=`ls -ld $file |cut -c 1`
  16.  
  17. #使用case判断文件类型
  18. case $type in
  19. -)
  20. echo -e "$CYAN general file$RESET"
  21. ;;
  22. d)
  23. echo -e "$YELLOW dir$RESET"
  24. ;;
  25. l)
  26. echo -e "$PURPLE link file$RESET"
  27. ;;
  28. *)
  29. echo "other"
  30. ;;
  31. esac

执行结果

  1. [root@centos73 ~]# bash filetype.sh
  2. you must enter a parameter
  3. [root@centos73 ~]# bash filetype.sh /etc/passwd
  4. general file
  5. [root@centos73 ~]# bash filetype.sh /etc/
  6. dir
  7. [root@centos73 ~]# bash filetype.sh /etc
  8. dir
  1. [root@centos73 ~]# bash filetype.sh /bin/python
  2. link file
  3. [root@centos73 ~]# bash filetype.sh /bin/python
  4. link file
  5. [root@centos73 ~]# bash filetype.sh /bin/python
  6. link file
  7. [root@centos73 ~]# echo $PATH
  8. /root/shell_scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

脚本解析

涉及到了颜色,相关知识参考博客给输出的字符或者字符串添加颜色

先判断参数,也就是文件是否存在

if [ $# -ne 1 ] ; then

echo -e "$RED you must enter a parameter$RESET"

exit

第1个参数赋值给变量

file=$

type=`ls -ld $file |cut -c 1`

  1. [root@centos73 ~]# ll -d /etc/passwd
  2. -rw-r--r--. 1 root root 2766 Jun 29 16:52 /etc/passwd
  3. [root@centos73 ~]# ll -d /etc/passwd | cut -c 1
  4. -

二/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件

分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start

如K34filename stop,S66filename start

完整脚本

  1. [root@centos73 shell_scripts]# cat file_KS.sh
  2. #!/bin/bash
  3. #Author=wang
  4. for i in `ls -1 /etc/rc.d/rc3.d` ; do
  5. type=`echo $i | cut -c 1 `
  6. if [ "$type" == "S" ] ; then
  7. echo "$i start"
  8. elif [ "$type" == "K" ] ; then
  9. echo "$i stop"
  10. else
  11. echo "$i unkown"
  12. fi
  13. done
  1. [root@centos73 shell_scripts]# ls -1 /etc/rc.d/rc3.d
  2. K50netconsole
  3. S10network
  4. [root@centos73 shell_scripts]# ls -1 /etc/rc.d/rc3.d | cut -c 1
  5. K
  6. S

执行结果

  1. [root@centos73 shell_scripts]# bash file_KS.sh
  2. K50netconsole stop
  3. S10network start

三提示输入正整数n的值,计算1+2+…+n的总和

完整脚本

  1. [root@centos73 shell_scripts]# cat sum.sh
  2. #!/bin/bash
  3. #Author=wang
  4. if [ $# -ne 1 ] ; then
  5. echo "you must enter a parameter"
  6. exit 1
  7. fi
  8.  
  9. n=$1
  10. digit="^[0-9]+$"
  11. if [[ ! $n =~ $digit ]]; then
  12. echo "not a digit"
  13. exit 2
  14. fi
  15.  
  16. declare -i sum=0
  17. #将变量定义为整形
  18. for i in `seq 1 $n`;do
  19. sum+=$i
  20. done
  21. echo $sum

执行结果

  1. [root@centos73 shell_scripts]# bash sum.sh 1
  2. 1
  3. [root@centos73 shell_scripts]# bash sum.sh 2
  4. 3
  5. [root@centos73 shell_scripts]# bash sum.sh 3
  6. 6
  7. [root@centos73 shell_scripts]# bash sum.sh 4
  8. 10
  9. [root@centos73 shell_scripts]# bash sum.sh 5
  10. 15
  11. [root@centos73 shell_scripts]# bash sum.sh 6
  12. 21
  13. [root@centos73 shell_scripts]# bash sum.sh 7
  14. 28
  15. [root@centos73 shell_scripts]# bash sum.sh 8
  16. 36
  17. [root@centos73 shell_scripts]# bash sum.sh 9
  18. 45
  19. [root@centos73 shell_scripts]# bash sum.sh 100

四打印九九乘法表

1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9

......

1*9=9   2*9 ...  9*9

被乘数指四则运算的乘法中被乘的数字,一般来说放在算式的前面。
如:4×2=8
上述算式中4是被乘数,2是乘数。

要打印9行就要执行9次循环,第几行就循环几遍,第1行循环1遍,第9行循环9遍。

被乘数的值是从1到所在的行号,乘数就是所在行的行号。

完整脚本

  1. [root@centos73 shell_scripts]# cat nine_nine_multiplication_table.sh
  2. #!/bin/bash
  3. #Author=wang
  4. for ((i=1;i<=9;i++));do
  5. for ((j=1;j<=i;j++));do
  6. echo -ne "${j}x${i}=$[$i*$j]\t"
    #\t表示插入tab。联系Linux可以使用tab键补齐,并且在文件里面输入tab键会退固定的空格
  7. done
  8. echo
  9. done

执行结果

  1. [root@centos73 shell_scripts]# bash nine_nine_multiplication_table.sh
  2. 1x1=1
  3. 1x2=2 2x2=4
  4. 1x3=3 2x3=6 3x3=9
  5. 1x4=4 2x4=8 3x4=12 4x4=16
  6. 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
  7. 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
  8. 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
  9. 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
  10. 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

脚本解析

使用for语句第2种语法,并且涉及到了两个变量

for ((i=1;i<=9;i++));do

for ((j=1;j<=i;j++));do

"${j}x${i}=$[$i*$j]\t"  因为x是字母,两个变量要使用花括号括起来

涉及到变量引用

变量引用:${name} $name
"  ":弱引用,其中的变量引用会被替换为变量值
'  ':强引用,其中的变量引用不会被替换为变量值,而保持原字符串

  1. [root@centos73 ~]# echo "{a}"
  2. {a}
  3. [root@centos73 ~]# echo "${a}"
  4. 1
  5. [root@centos73 ~]# echo ${a}
  6. 1
  7. [root@centos73 ~]# echo '${a}'
  8. ${a}

不合格脚本,原因在于没有tab键

  1. [root@centos73 shell_scripts]# cat nine_nine_multiplication_table.sh
  2. #!/bin/bash
  3. #Author=wang
  4. for ((i=1;i<=9;i++));do
  5. for ((j=1;j<=i;j++));do
  6. echo -ne "${j}x${i}=$[$i*$j]"
  7. done
  8. echo
  9. done
  1. [root@centos73 shell_scripts]# bash nine_nine_multiplication_table.sh
  2. 1x1=1
  3. 1x2=22x2=4
  4. 1x3=32x3=63x3=9
  5. 1x4=42x4=83x4=124x4=16
  6. 1x5=52x5=103x5=154x5=205x5=25
  7. 1x6=62x6=123x6=184x6=245x6=306x6=36
  8. 1x7=72x7=143x7=214x7=285x7=356x7=427x7=49
  9. 1x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=64
  10. 1x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=

五在/testdir目录下创建10个html文件,

文件名格式为数字N(从1到10)加随机8个字母

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

完整脚本

  1. [root@centos73 shell_scripts]# cat randhtml_1.sh
  2. #!/bin/bash
  3. #Author=wang
  4. dir=/testdir
  5. if [ ! -d $dir ] ; then
  6. mkdir -pv $dir &>/dev/null
  7. fi
  8. for i in `seq 1 10` ; do
  9. rand=`openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8`
  10. touch $dir/$i$rand.html
  11. done

执行结果

  1. [root@centos73 shell_scripts]# bash randhtml_1.sh
  2. [root@centos73 shell_scripts]# ls /testdir/
  3. 10DtiEyzsk.html 2htpVNwja.html 4ysrokezy.html 6bPaFMedz.html 8hJRPmQxQ.html
  4. 1TUWVBvMC.html 3YtFXYSZm.html 5COTAQDfG.html 7RqvJatMs.html 9xnGiosvG.html
  5. [root@centos73 shell_scripts]# ls /testdir/ -l
  6. total 0
  7. -rw-r--r--. 1 root root 0 Jul 1 08:58 10DtiEyzsk.html
  8. -rw-r--r--. 1 root root 0 Jul 1 08:58 1TUWVBvMC.html
  9. -rw-r--r--. 1 root root 0 Jul 1 08:58 2htpVNwja.html
  10. -rw-r--r--. 1 root root 0 Jul 1 08:58 3YtFXYSZm.html
  11. -rw-r--r--. 1 root root 0 Jul 1 08:58 4ysrokezy.html
  12. -rw-r--r--. 1 root root 0 Jul 1 08:58 5COTAQDfG.html
  13. -rw-r--r--. 1 root root 0 Jul 1 08:58 6bPaFMedz.html
  14. -rw-r--r--. 1 root root 0 Jul 1 08:58 7RqvJatMs.html
  15. -rw-r--r--. 1 root root 0 Jul 1 08:58 8hJRPmQxQ.html
  16. -rw-r--r--. 1 root root 0 Jul 1 08:58 9xnGiosvG.html

脚本解析

  1. [root@centos73 ~]# openssl rand -base64 10
  2. 9HlYaAoiWqYhHQ==
  3. [root@centos73 ~]# openssl rand -base64 10
  4. hhT85PKosPFIrQ==
  5. [root@centos73 ~]# openssl rand -base64 10
  6. cZW6W01bbEHAlA==
  1. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@g"
  2. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@g"
  3. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@g"
  4. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@g"
  5. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@gp"
  6. nKBMSLAnpyMg
  7. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@gp"
  8. sSXTbVqzYQ
  9. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@gp"
  10. MVCoWaDCmjcw
  11. [root@centos73 ~]# openssl rand -base64 10 |sed -rn "s@[^[:alpha:]]@@gp"
  12. guLpqstbTJw
  1. [root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
  2. CJDYqHkQ[root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
  3. VesypmUO[root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
  4. hySmdYup[root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
  5. gKzXWLKl[root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
  6. ZxfSnlmE[root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
  7. 0 1 8
  8. [root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
  9. 0 1 8
  10. [root@centos73 ~]# openssl rand -base64 10 | sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
  11. 0 1

六使用循环嵌套打印矩形

完整脚本

  1. [root@centos73 shell_scripts]# cat rectangle.sh
  2. #!/bin/bash
  3. #Author=wang
  4. line=10
  5. colume=8
  6. for i in `seq 1 $line ` ;do
  7. for j in `seq $colume`;do
  8. echo -e "*\c"
  9. done
  10. echo
  11.  
  12. #执行完一次循环要换行,要执行10次循环就会换10行
  13. done

执行结果

  1. [root@centos73 shell_scripts]# bash rectangle.sh
  2. ********
  3. ********
  4. ********
  5. ********
  6. ********
  7. ********
  8. ********
  9. ********
  10. ********
  11. ********

要加反引号

下面是做10遍循环,人为的写上8个*

  1. [root@centos73 shell_scripts]# bash rectangle.sh
  2. ********
  3. ********
  4. ********
  5. ********
  6. ********
  7. ********
  8. ********
  9. ********
  10. ********
  11. ********
  12. [root@centos73 shell_scripts]# bash rectangle.sh | wc
  13. 10 10 90
  14. [root@centos73 shell_scripts]# cat rectangle.sh
  15. #!/bin/bash
  16. #Author=wang
  17. line=10
  18. colume=8
  19. for i in `seq 1 $line ` ;do
  20. echo "********"
  21. done

脚本解析

如果是嵌套循环,最外层是打印10行,也就是先打印10行。

要调用两个变量,使用脚本里面常用的i,j,k

使用j表示来调用列,完成8个*的打印

因为*默认是换行的,\c就是压缩掉换行

先执行最外面循环,执行i=1,再执行里面的for循环,也就是打印一行的8个*

在循环体的最后一行 echo表示执行完一次循环要换行,要执行10次循环就会换10行

这个示例比较难理解

  1. [root@centos73 shell_scripts]# bash rectangle.sh
  2. ********
  3. ********
  4. ********
  5. ********
  6. ********
  7. ********
  8. ********
  9. ********
  10. ********
  11. ********
  12. [root@centos73 shell_scripts]# vim rectangle.sh
  13. [root@centos73 shell_scripts]# man echo
  14. [root@centos73 shell_scripts]# cat rectangle.sh
  15. #!/bin/bash
  16. #Author=wang
  17. line=10
  18. colume=8
  19. for i in `seq 1 $line ` ;do
  20. for j in `seq $colume`;do
  21. echo -e "*\c"
  22. done
  23. echo
  24.  
  25. #执行完一次循环要换行,要执行10次循环就会换10行
  26. done

bug1:

  1. [root@centos73 shell_scripts]# bash rectangle.sh
  2. *
  3. *
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10.  
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. .......
  20. [root@centos73 shell_scripts]# cat rectangle.sh
  21. #!/bin/bash
  22. #Author=wang
  23. line=10
  24. colume=8
  25. for i in `seq 1 $line ` ;do
  26. for j in `seq $colume`;do
  27. echo "*"
  28. done
  29. echo
  30. done

echo打印的是空行

  1. [root@centos73 ~]# echo
  2.  
  3. [root@centos73 ~]# echo
  4.  
  5. [root@centos73 ~]# echo
  6.  
  7. [root@centos73 ~]# echo | wc
  8. 1 0

bug2:

  1. [root@centos73 shell_scripts]# cat rectangle.sh
  2. #!/bin/bash
  3. #Author=wang
  4. line=10
  5. colume=8
  6. for i in `seq 1 $line ` ;do
  7. for j in `seq $colume`;do
  8. echo "*"
  9. done
  10. done
  11. [root@centos73 shell_scripts]# bash rectangle.sh
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. *
  23. *

七打印三角形,要求行号和所在行的*个数相同

*

**
***
****

完整脚本

  1. [root@centos73 shell_scripts]# cat triangle.sh
  2. #!/bin/bash
  3. #Author=wang
  4. line=8
  5. for i in `seq $line`;do
  6. for j in `seq $i`;do
  7. echo -e '$\c'
  8. #\c表示最后不加上换行符合
  9. done
  10. echo
  11. done
  1.  

执行结果

  1. [root@centos73 shell_scripts]# bash triangle.sh
  2. $
  3. $$
  4. $$$
  5. $$$$
  6. $$$$$
  7. $$$$$$
  8. $$$$$$$
  9. $$$$$$$$

脚本解析

变量i的值就是行号,这是默认的

for j  in `seq  $i`表示的是从1循环到行号

八打印等腰三角形

*
 ***
*****

完整脚本

  1. [root@centos73 shell_scripts]# cat isoscelestriangle.sh
  2. #!/bin/bash
  3. #Author=wang
  4. #num=总行号 i=第几行 j=*个数 k=空格个数
  5. read -p "请输入一个数字: " num
  6.  
  7. #每行的空格数,以确定*的开始位置
  8. for i in `seq 1 $num`;do
  9. for k in `seq 1 $[$num-$i]`; do
  10. echo -n " "
  11. #-n表示换行,并且光标移至行首
  12. done
  13.  
  14. #每行*的个数
  15. for j in `seq 1 $[2*$i-1]`;do
  16. echo -n "*"
  17. done
  18. echo
  19. done
  20.  
  21. #删除变量
  22. unset num i j k color

执行结果

  1. [root@centos73 shell_scripts]# bash isoscelestriangle.sh
  2. 请输入一个数字: 4
  3. *
  4. ***
  5. *****
  6. *******
  1. [root@centos73 shell_scripts]# bash isoscelestriangle.sh
  2. 请输入一个数字: 52
  3. *
  4. ***
  5. *****
  6. *******
  7. *********
  8. ***********
  9. *************
  10. ***************
  11. *****************
  12. *******************
  13. *********************
  14. ***********************
  15. *************************
  16. ***************************
  17. *****************************
  18. *******************************
  19. *********************************
  20. ***********************************
  21. *************************************
  22. ***************************************
  23. *****************************************
  24. *******************************************
  25. *********************************************
  26. ***********************************************
  27. *************************************************
  28. ***************************************************
  29. *****************************************************
  30. *******************************************************
  31. *********************************************************
  32. ***********************************************************
  33. *************************************************************
  34. ***************************************************************
  35. *****************************************************************
  36. *******************************************************************
  37. *********************************************************************
  38. ***********************************************************************
  39. *************************************************************************
  40. ***************************************************************************
  41. *****************************************************************************
  42. *******************************************************************************
  43. *********************************************************************************
  44. ***********************************************************************************
  45. *************************************************************************************
  46. ***************************************************************************************
  47. *****************************************************************************************
  48. *******************************************************************************************
  49. *********************************************************************************************
  50. ***********************************************************************************************
  51. *************************************************************************************************
  52. ***************************************************************************************************
  53. *****************************************************************************************************
  54. *******************************************************************************************************

脚本解析

打印几行那么第1行的第1个内容,比如*就处在第几列

每行的*的个数与行号的关系:2n-1

每行的空格数和行数的关系:总行数-行数

循环的次数和行数一样。

主要是逻辑思维

line=3

for i in `seq 1 total`
do
for j in ;do
空格数=total-n
*数=2n-1
done
echo
done

不合格脚本

  1. [root@centos73 shell_scripts]# cat isoscelestriangle.sh
  2. #!/bin/bash
  3. #Author=wang
  4. #num=总行号 i=第几行 j=*个数 k=空格个数
  5. read -p "请输入一个数字: " num
  6.  
  7. #每行的空格数,以确定*的开始位置
  8. for i in `seq 1 $num`;do
  9. for k in `seq 1 $[$num-$i]`; do
  10. echo " "
  11. #-n表示换行,并且光标移至行首
  12. done
  13.  
  14. #每行*的个数
  15. for j in `seq 1 $[2*$i-1]`;do
  16. echo "*"
  17. done
  18. echo
  19. done
  20.  
  21. #删除变量
  22. unset num i j k color
  1. [root@centos73 shell_scripts]# bash isoscelestriangle.sh
  2. 请输入一个数字: 4
  3.  
  4. *
  5.  
  6. *
  7. *
  8. *
  9.  
  10. *
  11. *
  12. *
  13. *
  14. *
  15.  
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. *

九打印国际象棋

国际象棋是8行8列

打印有颜色的格子

打印颜色参考博客https://www.cnblogs.com/wang618/p/11047178.html

  1. [root@centos73 shell_scripts]# echo -e ' \033[41m \033[0m '
  2.  
  3. [root@centos73 shell_scripts]# echo -e ' \033[43m \033[0m '

  1. [root@centos73 shell_scripts]# echo -e ' \033[43m \033[0m '; echo -e ' \033[41m \033[0m '
  2.  
  3. [root@centos73 shell_scripts]# echo -e ' \033[41m \033[0m '; echo -e ' \033[43m \033[0m '
  4.  
  5. [root@centos73 shell_scripts]# echo -e ' \033[41m \033[0m '

  1. [root@centos73 shell_scripts]# echo -e '\033[41m \033[0m\c '; echo -e '\033[43m \033[0m '
  2.  
  3. [root@centos73 shell_scripts]# echo -e '\033[41m \033[0m\c '; echo -e '\033[43m \033[0m '

  1. [root@centos73 shell_scripts]# echo -e '\033[41m \033[0m\c '; echo -e '\033[43m \033[0m ';
    echo -e '\033[41m \033[0m\c '; echo -e '\033[43m \033[0m '

法1完整脚本

写死了,不能交互,只能打印8个格子

  1. [root@centos73 shell_scripts]# cat Chess.sh
  2. #!/bin/bash
  3. #Author=wang
  4. line=8
  5. line2=$[line*2]
  6. #打印格子和颜色有关,因为只涉及两种颜色,五五开
  7. #i是打印格子的数量,j是循环次数
  8. #从里到外进行循环,最里面是循环最前面的两个格子
  9. for i in `seq 1 8 ` ; do
  10. for j in `seq 1 $line2 ` ; do
  11. if [ $[i%2] -eq 1 ] ; then
  12. if [ $[j%4] -eq 1 -o $[j%4] -eq 2 ] ; then
  13. echo -ne "\033[41m \033[0m"
  14. else
  15. echo -ne "\033[42m \033[0m"
  16. fi
  17. else
  18. if [ $[j%4] -eq 1 -o $[j%4] -eq 2 ] ; then
  19. echo -ne "\033[42m \033[0m"
  20. else
  21. echo -ne "\033[41m \033[0m"
  22. fi
  23. fi
  24. done
  25. echo
  26. done

执行结果

脚本解析

使用了if语句的嵌套

法2完整脚本

  1. [root@centos73 shell_scripts]# cat Chess_1.sh
  2. #!/bin/bash
  3. #Author=wang
  4. read -p "please input NUM : " num
  5. #交互式输入数字,这样更灵活
  6.  
  7. for i in $(seq 1 $num)
  8. #输入的数字作为变量值
  9. do
  10. j=$[i%2]
  11. #j是取模值,和颜色有关,为0为1颜色相反
  12. times=$[num/2]
  13. #决定每行打印格子的次数,因为两种颜色有交替,那么就是要经过(打印格子数/2)次循环
  14. case $j in
  15. 0)
  16. for times in $(seq 1 $times)
  17. do
  18. echo -e '\033[47m \033[0m\c'
  19. echo -e '\033[45m \033[0m\c'
  20. done
  21. echo -e ""
  22. ;;
  23. 1)
  24. for times in $(seq 1 $times)
  25. do
  26. echo -e '\033[45m \033[0m\c'
  27. echo -e '\033[47m \033[0m\c'
  28. done
  29. echo -e ""
  30. ;;
  31. esac
  32. #case嵌套了for循环
  33. done

执行结果

脚本解析

read -p "please input NUM : " num交互式输入数字,这样更灵活

变量i是执行循环的次数,变量j决定了每个格子的颜色

对j取模来决定一行前面两个格子的颜色。

如果取模的值是0就是白紫,如果取模的值是1那么就是紫白,

上面执行脚本的时候反应慢是因为嵌套循环多了,如果每行每列打印8个格子,那么要经过4次循环。

如果是随机打印格子,那么要经过(打印格子数/2)次循环。

注意echo -e ""是必不可少的。

作用是每打印一行就空一行,这样不会粘在一起,全部内容打印到同一行

  1. [root@centos73 shell_scripts]# echo aa
  2. aa
  3. [root@centos73 shell_scripts]# echo aa | wc
  4. 1 1 3
  5. [root@centos73 shell_scripts]# echo aa;echo -e ""
  6. aa
  7.  
  8. [root@centos73 shell_scripts]# echo aa;echo -e "" | wc
  9. aa
  10. 1 0

没有添加echo -e ""

  1. [root@centos73 shell_scripts]# cat Chess_1.sh
  2. #!/bin/bash
  3. #Author=wang
  4. read -p "please input NUM : " num
  5. #交互式输入数字,这样更灵活
  6.  
  7. for i in $(seq 1 $num)
  8. #输入的数字作为变量值
  9. do
  10. j=$[i%2]
  11. #j是取模值,和颜色有关,为0为1颜色相反
  12. times=$[num/2]
  13. #决定每行打印格子的次数,因为两种颜色有交替,那么就是要经过(打印格子数/2)次循环
  14. case $j in
  15. 0)
  16. for times in $(seq 1 $times)
  17. do
  18. echo -e '\033[47m \033[0m\c'
  19. echo -e '\033[45m \033[0m\c'
  20. done
  21. # echo -e ""
  22. ;;
  23. 1)
  24. for times in $(seq 1 $times)
  25. do
  26. echo -e '\033[45m \033[0m\c'
  27. echo -e '\033[47m \033[0m\c'
  28. done
  29. # echo -e ""
  30. ;;
  31. esac
  32. #case嵌套了for循环
  33. done

for循环语句示例的更多相关文章

  1. for循环语句示例应用

    age = 22 #优化前 ''' for i in range(10): guess_num = int(input('input your guess num:')) if guess_num = ...

  2. Shell循环语句for、while、until

    Shell循环语句for.while.until            一.条件测试            二.删除字符            三.循环语句              示例1     ...

  3. python之最强王者(3)——变量,条件、循环语句

    1.Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的 ...

  4. JavaScript 的循环语句语法摘要

     if条件语句语法: if(condition){ statements; } 理解:圆括号里的是条件参数  ,花括号里的为执行的语句. 示例代码:if(1>2){ alert("Th ...

  5. 04- Shell脚本学习--条件控制和循环语句

    条件判断:if语句 语法格式: if [ expression ] then Statement(s) to be executed if expression is true fi 注意:expre ...

  6. 你可能不知道的java、python、JavaScript以及jquary循环语句的区别

    一.概述 java循环语句分为四种形式,分别是 while, do/while, for, foreach: python中循环语句有两种,while,for: JavaScript中循环语句有四种, ...

  7. 不可或缺 Windows Native (3) - C 语言: 运算符,表达式,条件语句,循环语句,转向语句,空语句等

    [源码下载] 不可或缺 Windows Native (3) - C 语言: 运算符,表达式,条件语句,循环语句,转向语句,空语句等 作者:webabcd 介绍不可或缺 Windows Native  ...

  8. python_条件、循环语句

    1. python中语句块如何定义: 在Python中,冒号(:)用来标识语句块的开始,块中的每一个语句都是缩进的.当回退到和已经闭合的块一样的缩进量时,就表示当前块已经结束.      默认推荐缩进 ...

  9. JavaScript的循环语句

    JavaScript的循环语句 1.JavaScript的循环语句 (1)for循环语句 - 循环代码块一定的次数: (2)for/in循环语句 - 循环遍历对象的属性: (3)while循环语句 - ...

随机推荐

  1. vue中路由传参的方式

    一.params的类型: 配置路由格式: /router/:id 传递的方式: 在path后面跟上对应的值 传递后形成的路径: /router/123, /router/abc 通过:to字符串拼接的 ...

  2. ckeditor实现WORD粘贴图片自动上传

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  3. nodejs环境安装

    centos7安装nodejs环境 原文地址: https://www.cnblogs.com/MY0101/p/6625344.html 下载地址: https://nodejs.org/dist/ ...

  4. [CSP-S模拟测试]:gcd(莫比乌斯反演)

    题目描述 有$n$个正整数$x_1\sim x_n$,初始时状态均为未选.有$m$个操作,每个操作给定一个编号$i$,将$x_i$的选取状态取反.每次操作后,你需要求出选取的数中有多少个互质的无序数对 ...

  5. linux6.5 RPM方式安装 mysql5.6

    步骤一.检查下linux是不是已经安装了MySQL # rpm -qa|grep mysql mysql-libs-5.1.71-1.el6.x86_64 # rpm -e --nodeps mysq ...

  6. Django中的get()和filter()区别

    前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...

  7. 2016年Esri技术公开课全年资料分享

    大家好,2016年的公开课活动在上周全部结束,感谢大家的支持. 2016年的公开课共进行20期,共有24位讲师参与,公开课视频播放.课件下载次数累计超10万次,在这里衷心的感谢大家的积极参与和分享精神 ...

  8. node js 操作redis promise

    连接 redis = require('redis') var client = redis.createClient('6379', '127.0.0.1'); client.on('connect ...

  9. MySQL-5.7填坑

    绿色版(zip archive 版)无 my-default.ini As of MySQL 5.7.18, my-default.ini is no longer included in or in ...

  10. TestStack.White安装详解

    一.安装 NuGet TestStack.White是通过NuGet进行安装的.NuGet最低支持VS2010.我使用的VS2015. 安装方式一 :从Visual Studio的工具->扩展和 ...