[Shell] reverse printing number 1 to 10

1. Use double parentheses conditional expression

[qinys@localhost 20200313]$ cat 02_reverse.sh 
#!/bin/bash
i=10
while ((i>0)) # 使用(())
do
    echo $i
    ((i--))
done

Print Results:

[qinys@localhost 20200313]$ sh 02_reverse.sh 
10
9
8
7
6
5
4
3
2
1

2. Use double brackets conditional expression

[qinys@localhost 20200313]$ cat 02_reverse_1.sh 
#!/bin/bash
i=10
while [[ $i>0 ]]
do
    echo $i
    ((i--))
done

Print Results:

[qinys@localhost 20200313]$ sh 02_reverse_1.sh 
10
9
8
7
6
5
4
3
2
1

3. Use a single conditional expression in parentheses

[qinys@localhost 20200313]$ cat 02_reverse_2.sh 
#!/bin/bash
i=10
while [ $i -gt 0 ]
do
    echo $i
    ((i--))
done

Print Results:

[qinys@localhost 20200313]$ sh 02_reverse_2.sh 
10
9
8
7
6
5
4
3
2
1

4. Use the command until

[qinys@localhost 20200313]$ cat 02_reverse_3.sh 
#!/bin/bash
i=10
until [[ $i < 1 ]]
do
    echo $i
    ((i--))
done

Print Results:

[qinys@localhost 20200313]$ sh 02_reverse_3.sh 
10
9
8
7
6
5
4
3
2
1

Click [shell] various brackets (), (()), [], [[]], using {}

Guess you like

Origin www.cnblogs.com/OliverQin/p/12486403.html