首页 > 其他 > 详细

for循环语句示例

时间:2019-07-16 13:07:41      阅读:69      评论:0      收藏:0      [点我收藏+]

 

 

 

for循环语句示例

 

 

 

 

 

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

 

 

 

 完整脚本

[root@centos73 ~]# cat  shell_scripts/filetype.sh 
#!/bin/bash
#Author=wang
RED="\033[31m"
YELLOW="\033[0;33m"
CYAN="\033[36m"
PURPLE="\033[0;35m"
RESET="\033[0m"
if [ $# -ne 1 ] ; then
    echo -e "$RED you must enter a parameter$RESET"
        exit 1
fi


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

#使用case判断文件类型
case $type in
    -)
        echo  -e   "$CYAN general file$RESET"
        ;;
    d)
        echo -e   "$YELLOW dir$RESET"
        ;;
    l)
        echo -e  "$PURPLE link file$RESET"
        ;;
    *)
        echo "other"
        ;;
esac

 

 

 

 

 

 

 执行结果

[root@centos73 ~]# bash   filetype.sh   
 you must enter a parameter
[root@centos73 ~]# bash   filetype.sh   /etc/passwd
 general file
[root@centos73 ~]# bash   filetype.sh   /etc/
 dir
[root@centos73 ~]# bash   filetype.sh   /etc
 dir

 

 

[root@centos73 ~]# bash   filetype.sh   /bin/python
 link file
[root@centos73 ~]# bash   filetype.sh   /bin/python
 link file
[root@centos73 ~]# bash   filetype.sh   /bin/python
 link file
[root@centos73 ~]# echo $PATH 
/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

 

 

 

第1个参数赋值给变量

file=$1

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

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

 

 

 

 

 

 

 

 

 

 

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

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

如K34filename stop,S66filename start

 

 

完整脚本

[root@centos73 shell_scripts]# cat   file_KS.sh
#!/bin/bash
#Author=wang
for i in `ls -1 /etc/rc.d/rc3.d` ; do
    type=`echo $i | cut -c 1 `
    if [ "$type" == "S" ] ; then
       echo "$i start"
    elif [ "$type" == "K" ] ; then
       echo "$i stop"
    else
       echo "$i unkown"
    fi
done

 

 

 

 

[root@centos73 shell_scripts]# ls -1 /etc/rc.d/rc3.d
K50netconsole
S10network
[root@centos73 shell_scripts]# ls -1 /etc/rc.d/rc3.d  |  cut -c 1
K
S

 

 

 

 

 执行结果

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

 

 

 

 

 

 

 

 

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

 

 

完整脚本

[root@centos73 shell_scripts]# cat sum.sh 
#!/bin/bash
#Author=wang
if [ $# -ne 1 ] ; then
        echo "you must enter a parameter"
        exit  1
fi

n=$1
digit="^[0-9]+$"
if [[ ! $n =~ $digit ]]; then
        echo "not a digit"
        exit 2
fi

declare -i sum=0
#将变量定义为整形
for i in `seq 1 $n`;do
        sum+=$i
done
echo $sum

 

 

 

 

 

 

执行结果

[root@centos73 shell_scripts]# bash  sum.sh   1
1
[root@centos73 shell_scripts]# bash  sum.sh   2
3
[root@centos73 shell_scripts]# bash  sum.sh   3
6
[root@centos73 shell_scripts]# bash  sum.sh   4
10
[root@centos73 shell_scripts]# bash  sum.sh   5
15
[root@centos73 shell_scripts]# bash  sum.sh   6
21
[root@centos73 shell_scripts]# bash  sum.sh   7
28
[root@centos73 shell_scripts]# bash  sum.sh   8
36
[root@centos73 shell_scripts]# bash  sum.sh   9
45
[root@centos73 shell_scripts]# bash  sum.sh   100
5050

 

 

 

 

 

 

 

 

 

 

四打印九九乘法表

 

 

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到所在的行号,乘数就是所在行的行号。

 

 

 完整脚本

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

 

 

 

 

 

执行结果

[root@centos73 shell_scripts]# bash  nine_nine_multiplication_table.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    

 

 

 

 

脚本解析

使用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
"  ":弱引用,其中的变量引用会被替换为变量值
‘  ‘:强引用,其中的变量引用不会被替换为变量值,而保持原字符串

[root@centos73 ~]# echo "{a}"
{a}
[root@centos73 ~]# echo "${a}"
1
[root@centos73 ~]# echo ${a}
1
[root@centos73 ~]# echo ${a}
${a}

 

 

 

 

 

 

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

[root@centos73 shell_scripts]# cat   nine_nine_multiplication_table.sh 
#!/bin/bash
#Author=wang
for ((i=1;i<=9;i++));do 
    for ((j=1;j<=i;j++));do
        echo  -ne "${j}x${i}=$[$i*$j]"
    done
    echo
done

 

 

 

 

 

[root@centos73 shell_scripts]# bash  nine_nine_multiplication_table.sh 
1x1=1
1x2=22x2=4
1x3=32x3=63x3=9
1x4=42x4=83x4=124x4=16
1x5=52x5=103x5=154x5=205x5=25
1x6=62x6=123x6=184x6=245x6=306x6=36
1x7=72x7=143x7=214x7=285x7=356x7=427x7=49
1x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=64
1x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81

 

 

 

 

 

 

 

 

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

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

 

 

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

 

 

完整脚本

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

 

 

 

 

 

 

执行结果

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

 

 

 

 

 

 

 

 脚本解析

[root@centos73 ~]# openssl rand -base64 10  
9HlYaAoiWqYhHQ==
[root@centos73 ~]# openssl rand -base64 10  
hhT85PKosPFIrQ==
[root@centos73 ~]# openssl rand -base64 10  
cZW6W01bbEHAlA==

 

 

 

 

[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@g"
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@g"
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@g"
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@g"
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@gp"
nKBMSLAnpyMg
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@gp"
sSXTbVqzYQ
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@gp"
MVCoWaDCmjcw
[root@centos73 ~]# openssl rand -base64 10  |sed -rn "s@[^[:alpha:]]@@gp"
guLpqstbTJw

 

 

 

[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
CJDYqHkQ[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
VesypmUO[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
hySmdYup[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
gKzXWLKl[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8
ZxfSnlmE[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
      0       1       8
[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
      0       1       8
[root@centos73 ~]# openssl rand -base64 10  |  sed -rn "s@[^[:alpha:]]@@gp"|head -c 8 | wc
      0       1       8

 

 

 

 

 

 

 

六使用循环嵌套打印矩形

 

 

完整脚本

[root@centos73 shell_scripts]# cat    rectangle.sh 
#!/bin/bash
#Author=wang
line=10
colume=8
for i  in `seq  1  $line ` ;do
    for j  in  `seq   $colume`;do
    echo  -e   "*\c"
    done
    echo

 #执行完一次循环要换行,要执行10次循环就会换10行
done

 

 

 

 

 

执行结果

[root@centos73 shell_scripts]# bash  rectangle.sh 
********
********
********
********
********
********
********
********
********
********

 

 

 

 

 

 

要加反引号

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

[root@centos73 shell_scripts]# bash  rectangle.sh 
********
********
********
********
********
********
********
********
********
********
[root@centos73 shell_scripts]# bash  rectangle.sh   |  wc
     10      10      90
[root@centos73 shell_scripts]# cat  rectangle.sh 
#!/bin/bash
#Author=wang
line=10
colume=8
for i  in `seq  1  $line ` ;do
    echo  "********"
done

 

 

 

 

 

脚本解析

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

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

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

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

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

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

这个示例比较难理解

[root@centos73 shell_scripts]# bash  rectangle.sh 
********
********
********
********
********
********
********
********
********
********
[root@centos73 shell_scripts]# vim  rectangle.sh 
[root@centos73 shell_scripts]# man  echo
[root@centos73 shell_scripts]# cat  rectangle.sh 
#!/bin/bash
#Author=wang
line=10
colume=8
for i  in `seq  1  $line ` ;do
    for j  in  `seq   $colume`;do
    echo  -e   "*\c"
    done
    echo

#执行完一次循环要换行,要执行10次循环就会换10行 done

 

 

 

 

 

 

bug1:

[root@centos73 shell_scripts]# bash  rectangle.sh 
*
*
*
*
*
*
*
*

*
*
*
*
*
*
*
*
.......
[root@centos73 shell_scripts]# cat  rectangle.sh 
#!/bin/bash
#Author=wang
line=10
colume=8
for i  in `seq  1  $line ` ;do
    for j  in  `seq   $colume`;do
    echo     "*"
    done
    echo 
done

 

 

 

 

 

 

 

echo打印的是空行

[root@centos73 ~]# echo 

[root@centos73 ~]# echo 

[root@centos73 ~]# echo 

[root@centos73 ~]# echo   | wc
      1       0       1

 

 

 

 

 

 

 

bug2:

[root@centos73 shell_scripts]# cat  rectangle.sh 
#!/bin/bash
#Author=wang
line=10
colume=8
for i  in `seq  1  $line ` ;do
    for j  in  `seq   $colume`;do
    echo     "*"
    done
    done
[root@centos73 shell_scripts]# bash  rectangle.sh 
*
*
*
*
*
*
*
*
*
*
*
*

 

 

 

 

 

 

 

 

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

*

**
***
****

 

 

完整脚本

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

 

 





执行结果

[root@centos73 shell_scripts]# bash  triangle.sh 
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$

 

 

 

 

 

 

脚本解析

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

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

 

 

 

 

 

 

 

 

八打印等腰三角形

  *
 ***
*****

 

 

 

完整脚本


[root@centos73 shell_scripts]# cat  isoscelestriangle.sh 
#!/bin/bash
#Author=wang
#num=总行号  i=第几行  j=*个数  k=空格个数
read -p "请输入一个数字: "  num
 

#每行的空格数,以确定*的开始位置
for i in `seq 1 $num`;do
    for k in `seq 1 $[$num-$i]`; do
        echo -n   " "
#-n表示换行,并且光标移至行首
    done


#每行*的个数
    for j in `seq 1 $[2*$i-1]`;do
            echo  -n  "*"
    done
    echo
done


#删除变量
unset num i j k color

 

 

 

 

 

 

执行结果

[root@centos73 shell_scripts]# bash  isoscelestriangle.sh 
请输入一个数字: 4
   *
  ***
 *****
*******

 

 

 

 

 

[root@centos73 shell_scripts]# bash  isoscelestriangle.sh
请输入一个数字: 52
                                                   *
                                                  ***
                                                 *****
                                                *******
                                               *********
                                              ***********
                                             *************
                                            ***************
                                           *****************
                                          *******************
                                         *********************
                                        ***********************
                                       *************************
                                      ***************************
                                     *****************************
                                    *******************************
                                   *********************************
                                  ***********************************
                                 *************************************
                                ***************************************
                               *****************************************
                              *******************************************
                             *********************************************
                            ***********************************************
                           *************************************************
                          ***************************************************
                         *****************************************************
                        *******************************************************
                       *********************************************************
                      ***********************************************************
                     *************************************************************
                    ***************************************************************
                   *****************************************************************
                  *******************************************************************
                 *********************************************************************
                ***********************************************************************
               *************************************************************************
              ***************************************************************************
             *****************************************************************************
            *******************************************************************************
           *********************************************************************************
          ***********************************************************************************
         *************************************************************************************
        ***************************************************************************************
       *****************************************************************************************
      *******************************************************************************************
     *********************************************************************************************
    ***********************************************************************************************
   *************************************************************************************************
  ***************************************************************************************************
 *****************************************************************************************************
*******************************************************************************************************

 

 

 

 

 

 

脚本解析

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

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

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

 

循环的次数和行数一样。

主要是逻辑思维

 

line=3

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

 

 

 

 

 

 

 

 

 不合格脚本

[root@centos73 shell_scripts]# cat    isoscelestriangle.sh 
#!/bin/bash
#Author=wang
#num=总行号  i=第几行  j=*个数  k=空格个数
read -p "请输入一个数字: "  num
 

#每行的空格数,以确定*的开始位置
for i in `seq 1 $num`;do
    for k in `seq 1 $[$num-$i]`; do
        echo   " "
#-n表示换行,并且光标移至行首
    done


#每行*的个数
    for j in `seq 1 $[2*$i-1]`;do
            echo  "*"
    done
    echo
done


#删除变量
unset num i j k color

 

 

 

 

 

 

[root@centos73 shell_scripts]# bash  isoscelestriangle.sh 
请输入一个数字: 4
 
 
 
*

 
 
*
*
*

 
*
*
*
*
*

*
*
*
*
*
*
*

 

 

 

 

 

 

 

九打印国际象棋

 

国际象棋是8行8列

 

 

技术分享图片

 

 

 

 

 

打印有颜色的格子

 

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

[root@centos73 shell_scripts]# echo   -e    \033[41m       \033[0m 
         
[root@centos73 shell_scripts]# echo   -e    \033[43m       \033[0m 

 

 

 

 

 

技术分享图片

 

 

 

 

 

 

[root@centos73 shell_scripts]# echo   -e    \033[43m       \033[0m ‘; echo   -e    \033[41m       \033[0m 
         
         
[root@centos73 shell_scripts]# echo   -e    \033[41m       \033[0m ‘; echo   -e    \033[43m       \033[0m 
         
         
[root@centos73 shell_scripts]# echo   -e    \033[41m                                                                \033[0m 

 

 

 

技术分享图片

 

 

 

 技术分享图片

 

 

 

 

 

 技术分享图片

 

 

 

 

 

 

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

 

 

 

 

 技术分享图片

 

 

 

 技术分享图片

 

 

 

 

 

 

 

[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个格子

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

 

 

 

 

 

 

 

执行结果

 

 

技术分享图片

 

 

 

 

 脚本解析

使用了if语句的嵌套

 

 

 

 

 

 

 

 法2完整脚本

[root@centos73 shell_scripts]# cat  Chess_1.sh
#!/bin/bash
#Author=wang
read -p "please input NUM : " num
#交互式输入数字,这样更灵活

for i in $(seq 1 $num)
#输入的数字作为变量值
do
    j=$[i%2]
#j是取模值,和颜色有关,为0为1颜色相反
    times=$[num/2]
#决定每行打印格子的次数,因为两种颜色有交替,那么就是要经过(打印格子数/2)次循环
    case  $j  in
    0)
        for times in $(seq 1   $times)
        do
            echo -e \033[47m  \033[0m\cecho -e \033[45m  \033[0m\cdone
        echo -e ""
    ;;
    1)
        for times in $(seq 1 $times)
        do
            echo -e \033[45m  \033[0m\cecho -e \033[47m  \033[0m\cdone
        echo -e ""
    ;;
    esac
#case嵌套了for循环
done

 

 

 

 

 

 

 执行结果

 

技术分享图片

 

 

 

 

脚本解析

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

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

 

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

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

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

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

 

 

 

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

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

[root@centos73 shell_scripts]# echo aa
aa
[root@centos73 shell_scripts]# echo aa | wc
      1       1       3
[root@centos73 shell_scripts]# echo aa;echo  -e  "" 
aa

[root@centos73 shell_scripts]# echo aa;echo  -e  "" |  wc
aa
      1       0       1

 

 

 

 

 

 

 

没有添加echo -e ""

[root@centos73 shell_scripts]# cat    Chess_1.sh
#!/bin/bash
#Author=wang
read -p "please input NUM : " num
#交互式输入数字,这样更灵活

for i in $(seq 1 $num)
#输入的数字作为变量值
do
    j=$[i%2]
#j是取模值,和颜色有关,为0为1颜色相反
    times=$[num/2]
#决定每行打印格子的次数,因为两种颜色有交替,那么就是要经过(打印格子数/2)次循环
    case  $j  in
    0)
        for times in $(seq 1   $times)
        do
            echo -e \033[47m  \033[0m\cecho -e \033[45m  \033[0m\cdone
       # echo -e ""
    ;;
    1)
        for times in $(seq 1 $times)
        do
            echo -e \033[45m  \033[0m\cecho -e \033[47m  \033[0m\cdone
       # echo -e ""
    ;;
    esac
#case嵌套了for循环
done

 

 

 

 

 

技术分享图片

 

for循环语句示例

原文:https://www.cnblogs.com/wang618/p/11192496.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!