Skip to content

Commit 39c2054

Browse files
committed
Conditional testing
1 parent c20ce0d commit 39c2054

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

Conditional-Testing/File_testing.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#Description : Conditional testing
2+
#Author : HuangYuhui
3+
#Date : Janual 10,2018
4+
5+
#!/bin/bash
6+
7+
#Initialize the path of file.
8+
file="File_testing.txt"
9+
10+
#Determine whether the file is readable.
11+
if [ -r $file ]
12+
then
13+
echo "This file is readable ~"
14+
else
15+
echo "This file is not readable ~"
16+
fi
17+
18+
#Determine whether the file is writable.
19+
if [ -w $file ]
20+
then
21+
echo "This file is writable ~"
22+
else
23+
echo "This file is not writable ~"
24+
fi
25+
26+
#Determine whether the file is executable.
27+
if [ -x $file ]
28+
then
29+
echo "This file is executable ~"
30+
else
31+
echo "This file is not executable ~"
32+
fi
33+
34+
#Determine the type of file.
35+
if [ -f $file ]
36+
then
37+
echo "This file is an ordinary file ~"
38+
else
39+
echo "This file is a special file ~"
40+
fi
41+
42+
#Determine whether the file is directory.
43+
if [ -d $file ]
44+
then
45+
echo "This file is a directory ~"
46+
else
47+
echo "This file is not a directory ~"
48+
fi
49+
50+
#Determine whether the file is empty.
51+
if [ -s $file ]
52+
then
53+
echo "This file is not empty ~"
54+
else
55+
echo "This file is empty ~"
56+
fi
57+
58+
#Determine whether the file is exist.
59+
if [ -e $file ]
60+
then
61+
echo "This file is exist ~"
62+
else
63+
echo "This file is not exist ~"
64+
fi
65+
66+
#...
67+

Conditional-Testing/File_testing.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Create this file for 'File_testing.sh' !
2+
You can delete it everythime.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Description : The integer value testing.
2+
#Author : HuangYuhui
3+
#Date : January 10,2018
4+
5+
#!/bin/bash
6+
7+
#The common arithmetic manipulation symbols.
8+
# -eq (Reverse:-ne)
9+
# -ge (Reverse:-le)
10+
# -gt (Reverse:-lt)
11+
# -le
12+
# -lt
13+
# -ne
14+
15+
##
16+
##You can use the '[[ xxx ]]'/command:'test' to test or learn use these symbols.
17+
##
18+
19+
20+
#Determine whether the two numbers are the same.
21+
[[ 1 -eq 2 ]]
22+
echo $?
23+
#Reverse
24+
[[ 1 -ne 2 ]]
25+
echo $?
26+
27+
#Determine the size.
28+
[[ 1 -lt 2 ]]
29+
echo $?
30+
#Reverse
31+
[[ 1 -gt 2 ]]
32+
echo $?
33+
34+
#Determine the size.
35+
x=1
36+
y=2
37+
[ "$x" -ge "$y" ]
38+
echo $?
39+
#Reverse
40+
[ "$x" -le "$y" ]
41+
echo $?
42+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Description : The logical operator.
2+
#Author : HuangYuhui
3+
#Date : January 10,2018
4+
5+
#The common logical operator:
6+
# -a
7+
# -o
8+
# !
9+
# &&
10+
# ||
11+
12+
test -e File_testing.sh -a -s File_testing.txt && echo "The condition is true ~"
13+
14+

Conditional-Testing/String_testing.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#Description : String testing.
2+
#Author : HuangYuhui
3+
#Date : January 10,2018
4+
5+
#!/bin/bash
6+
7+
#The common string manipulation symbols are as follows :
8+
# '<', '>' ,'==','=','=~','!=','-z','-n'
9+
10+
#---------------------------------------------------------------------------------
11+
#In the Shell program,the zero means a Boolean value is true,1 or other is false.|
12+
#---------------------------------------------------------------------------------
13+
14+
##
15+
##Attention : Use the system variable of '$!' can get the result of executing the sepcified command.
16+
##
17+
#Attention : The false grammar.
18+
[[ "abcd"=="a" ]]
19+
echo $?
20+
#The ture grammmer.
21+
[[ "abcd" == "a" ]]
22+
echo $?
23+
24+
#The String can contrain wildcards.
25+
[[ "abcd" == abc* ]]
26+
echo $?
27+
28+
#Compare the string.
29+
#The command : -z (If the string is empty It will return false)
30+
var=""
31+
##
32+
##Attention : The command name must be 'test' !
33+
##
34+
#For example : User the name of 'error'
35+
#error -z "var"
36+
#echo $? #It's will output : The command not found !
37+
#The ture grammar.
38+
test -z "$var"
39+
echo $?
40+
#Or
41+
var="Shell"
42+
test -z "$var"
43+
echo $?
44+
45+
#The command : -n (If the string is empty It Will return true)
46+
command_n=""
47+
test -n "$command_n"
48+
echo $?
49+
#Or
50+
command_n="Shell"
51+
test -n "$command_n"
52+
echo $?
53+
54+
#The command : !=
55+
command_1="YUbuntu0109.github.io" #Ahhh ~ It's my websit address !
56+
command_2="GitHub.github.io"
57+
#The error grammar:o(╥﹏╥)o ~ What should i do ?! (I don't find the specified example in my book...)
58+
test $command_1 != $command_2
59+
#echo $?

0 commit comments

Comments
 (0)