Skip to content

Commit c45422b

Browse files
committed
Learn the conditional statement ~
1 parent 3c982f3 commit c45422b

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Description : The matched model of case statement.
2+
#Author : HuangYuhui
3+
#Date : January 16,2018
4+
5+
#!/bin/bash
6+
7+
#Get the data inputed by the user.
8+
read -p "Please press some key,then press return :" KEY
9+
10+
#matche by the case statement.
11+
case $KEY in
12+
13+
[a-z] | [A-Z])
14+
echo "You press a letter ~"
15+
;;
16+
17+
[0-9])
18+
echo "You press a digit ~"
19+
;;
20+
21+
*)
22+
echo "You press anthor key rather letter or digit ~"
23+
24+
esac
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Description : Case statement
2+
#Author : HuangYuhui
3+
#Date January 16,2018
4+
5+
#!/bin/bash
6+
7+
SYSTEM=`uname -s` #Output the name of keranl.
8+
case "$SYSTEM" in
9+
10+
"Linux") #The kernal name of Linux sysem operation be called : Linux
11+
echo "Linux"
12+
;;
13+
14+
"FreeBSD") #...
15+
echo "FreeBSD"
16+
;;
17+
18+
"Solaris") #...
19+
echo "Solaris"
20+
;;
21+
22+
"Windows") #...
23+
echo "Windows"
24+
;;
25+
*) #Default
26+
echo "The ture kernal name : $SYSTEM"
27+
;;
28+
29+
esac
30+

Conditional-Statement/If_Sorting.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#Description : Sorting
2+
#Author : HuangYuhui
3+
#Date : January 16,2018
4+
5+
#!/bin/bash
6+
7+
#Give a tip if the total of number is error inputed by the user.
8+
if [ "$#" -ne 3 ];then
9+
echo "Please enter the number ! such as : /.$0 num1 num2 num3"
10+
exit 0
11+
fi
12+
13+
#Receive the number inputed by the user.
14+
a=$1
15+
b=$2
16+
c=$3
17+
18+
#If a greater than b,a and b swap positions.
19+
if [ $a -gt $b ];then
20+
tmp=$a
21+
a=$b
22+
b=$tmp
23+
fi
24+
25+
#If a greater than c, a and c swap positions.
26+
if [ $a -gt $c ];then
27+
tmp=$a
28+
a=$c
29+
c=$tmp
30+
fi
31+
32+
#If b greater than c, b and c swap positions.
33+
if [ $b -gt $c ];then
34+
tmp=$b
35+
b=$c
36+
c=$tmp
37+
fi
38+
39+
#Output the result as sorting.
40+
echo "The result as sorting : $a $b $c"
41+

0 commit comments

Comments
 (0)