-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.sh
131 lines (106 loc) · 2.27 KB
/
variable.sh
1
2
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
!/bin/bash
#define variable
# No space between variable name equation symobl
hello="hello world"
#use variable
echo $hello
#some test case
for file in `ls /etc`; do
echo $file
done
#{} can bse use to explicitly show the boundary of variable
for skill in Ada Coffee Action Jave; do
echo "I am good at ${skill}Script"
done
#readonly variable
myUrl="https://github.com"
readonly myUrl
#follwing line will complain since the readonly variable cann't be changed
#myUrl="https://goole.com"
#Delte variable
#unset myUrl #Read Only Variable cannot be deleted
#echo $myUrl
unset hello
#Since hello is deleted, there will be no output
echo $hello
#String
#Single quote : varaible cannot be used in single quote
str='this is a string'
#Double quote: variable and escape character are allowed in double quote
your_name='hello'
str="Hello, I know your are \"$your_name\"!\n"
echo -e $str
#concat string
your_name="runoob"
greeting="hello."$your_name"!"
greeting_1="hello,${your_name}!"
echo $greeting $greeting_1
#concat using single quote
greeting_2='hello,'$your_name'!'
greeting_3='hello,${your_name}!'
echo $greeting_2 $greeting_3
#get length of string
string='abcd'
echo ${#string}
#extract substring
string="runnoob is a great site"
echo ${string:1:4}
#find substring, output the location of finded substring
string="runnob is a great site"
echo `expr index "$string" io`
#array
arr=(1,2,3)
arr[0]=2
echo $arr
echo ${arr[0]}
#get all element
echo ${arr[@]}
#get length
length=${#arr[@]}
length_1=${#arr[*]}
echo $length
echo $length_1
#file test expression
file="argument.sh"
if [ -r $file ] # test whether file is readable
then
echo "File is readable"
else
echo "File is not readable"
fi
if [ -w $file ]
then
echo "File is writeable"
else
echo "File is not writeable"
fi
if [ -x $file ]
then
echo "File is executable"
else
echo "File is not executable"
fi
if [ -f $file ]
then
echo " File is a regular file"
else
echo " File is not a regular file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "File is not a directory"
fi
if [ -s $file ]
then
echo "File is not empty"
else
echo "File is empty"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File doesnot exists"
fi