Skip to content

Commit 7d1ce36

Browse files
committed
update scripts
1 parent 1072e77 commit 7d1ce36

File tree

56 files changed

+792
-479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+792
-479
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ insert_final_newline = true
2323
end_of_line = crlf
2424

2525
[*.{java, sh}]
26+
indent_style = tab
2627
indent_size = 4
2728

2829
[*.md]

codes/shell/demos/string-demo.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ echo ${str3}_${str4}
2020

2121
################### 获取字符串长度 ###################
2222
text="12345"
23-
echo ${#text}
23+
echo "${text} length is: ${#text}"
2424
# Output:
25-
# 5
25+
# 12345 length is: 5
2626

2727
################### 获取字符串长度 ###################
2828
text="12345"
@@ -37,13 +37,14 @@ echo `expr index "${text}" ll`
3737
# 3
3838

3939
################### 截取关键字左边内容 ###################
40-
str="feature/1.0.0"
41-
branch=`echo ${str#feature/}`
40+
full_branch="feature/1.0.0"
41+
branch=`echo ${full_branch#feature/}`
4242
echo "branch is ${branch}"
4343

4444
################### 截取关键字右边内容 ###################
45-
key=`echo ${str%/1.0.0}`
46-
echo "key is ${key}"
45+
full_version="0.0.1-SNAPSHOT"
46+
version=`echo ${full_version%-SNAPSHOT}`
47+
echo "version is ${version}"
4748

4849
################### 判断字符串中是否包含子字符串 ###################
4950
result=$(echo "${str}" | grep "feature/")

codes/shell/demos/variable-demo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
################### 声明变量 ###################
44
name="world"
55
echo "hello ${name}"
6-
# Output: hello
6+
# Output: hello world
77

88
################### 只读变量 ###################
99
rword="hello"

codes/shell/示例脚本/变量/变量基本使用.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

codes/shell/示例脚本/变量/将输出结果赋值给变量.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

codes/shell/示例脚本/变量/系统变量.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

codes/shell/示例脚本/变量/自定义变量.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
3+
################### 声明变量 ###################
4+
name="world"
5+
echo "hello ${name}"
6+
# Output: hello world
7+
8+
################### 输出变量 ###################
9+
folder=$(pwd)
10+
echo "current path: ${folder}"
11+
12+
################### 只读变量 ###################
13+
rword="hello"
14+
echo ${rword}
15+
# Output: hello
16+
readonly rword
17+
# rword="bye" # 如果放开注释,执行时会报错
18+
19+
################### 删除变量 ###################
20+
dword="hello" # 声明变量
21+
echo ${dword} # 输出变量值
22+
# Output: hello
23+
24+
unset dword # 删除变量
25+
echo ${dword}
26+
# Output: (空)
27+
28+
################### 系统变量 ###################
29+
echo "UID:$UID"
30+
echo LOGNAME:$LOGNAME
31+
echo User:$USER
32+
echo HOME:$HOME
33+
echo PATH:$PATH
34+
echo HOSTNAME:$HOSTNAME
35+
echo SHELL:$SHELL
36+
echo LANG:$LANG
37+
38+
################### 自定义变量 ###################
39+
days=10
40+
user="admin"
41+
echo "$user logged in $days days age"
42+
days=5
43+
user="root"
44+
echo "$user logged in $days days age"
45+
# Output:
46+
# admin logged in 10 days age
47+
# root logged in 5 days age
48+
49+
################### 从变量读取列表 ###################
50+
colors="Red Yellow Blue"
51+
colors=$colors" White Black"
52+
53+
for color in $colors
54+
do
55+
echo " $color"
56+
done
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash
2+
3+
################### 使用单引号拼接字符串 ###################
4+
name1='white'
5+
str1='hello, '${name1}''
6+
str2='hello, ${name1}'
7+
echo ${str1}_${str2}
8+
# Output:
9+
# hello, white_hello, ${name1}
10+
11+
################### 使用双引号拼接字符串 ###################
12+
name2="black"
13+
str3="hello, "${name2}""
14+
str4="hello, ${name2}"
15+
echo ${str3}_${str4}
16+
# Output:
17+
# hello, black_hello, black
18+
19+
################### 获取字符串长度 ###################
20+
text="12345"
21+
echo "${text} length is: ${#text}"
22+
# Output:
23+
# 12345 length is: 5
24+
25+
# 获取子字符串
26+
text="12345"
27+
echo ${text:2:2}
28+
# Output:
29+
# 34
30+
31+
################### 查找子字符串 ###################
32+
text="hello"
33+
echo `expr index "${text}" ll`
34+
# Output:
35+
# 3
36+
37+
################### 判断字符串中是否包含子字符串 ###################
38+
result=$(echo "${str}" | grep "feature/")
39+
if [[ "$result" != "" ]]; then
40+
echo "feature/ 是 ${str} 的子字符串"
41+
else
42+
echo "feature/ 不是 ${str} 的子字符串"
43+
fi
44+
45+
################### 截取关键字左边内容 ###################
46+
full_branch="feature/1.0.0"
47+
branch=`echo ${full_branch#feature/}`
48+
echo "branch is ${branch}"
49+
50+
################### 截取关键字右边内容 ###################
51+
full_version="0.0.1-SNAPSHOT"
52+
version=`echo ${full_version%-SNAPSHOT}`
53+
echo "version is ${version}"
54+
55+
################### 字符串分割成数组 ###################
56+
str="0.0.0.1"
57+
OLD_IFS="$IFS"
58+
IFS="."
59+
array=( ${str} )
60+
IFS="$OLD_IFS"
61+
size=${#array[*]}
62+
lastIndex=`expr ${size} - 1`
63+
echo "数组长度:${size}"
64+
echo "最后一个数组元素:${array[${lastIndex}]}"
65+
for item in ${array[@]}
66+
do
67+
echo "$item"
68+
done
69+
70+
################### 判断字符串是否为空 ###################
71+
#-n 判断长度是否非零
72+
#-z 判断长度是否为零
73+
74+
str=testing
75+
str2=''
76+
if [[ -n "$str" ]]
77+
then
78+
echo "The string $str is not empty"
79+
else
80+
echo "The string $str is empty"
81+
fi
82+
83+
if [[ -n "$str2" ]]
84+
then
85+
echo "The string $str2 is not empty"
86+
else
87+
echo "The string $str2 is empty"
88+
fi
89+
90+
# Output:
91+
# The string testing is not empty
92+
# The string is empty
93+
94+
################### 字符串比较 ###################
95+
str=hello
96+
str2=world
97+
if [[ $str = "hello" ]]; then
98+
echo "str equals hello"
99+
else
100+
echo "str not equals hello"
101+
fi
102+
103+
if [[ $str2 = "hello" ]]; then
104+
echo "str2 equals hello"
105+
else
106+
echo "str2 not equals hello"
107+
fi

codes/shell/示例脚本/数组/数组基本使用.sh renamed to codes/shell/示例脚本/基本脚本/数组使用示例.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/env bash
22

3-
# 创建数组
3+
################### 创建数组 ###################
44
nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 )
55
colors=( red yellow "dark blue" )
66

7-
# 访问数组的单个元素
7+
################### 访问数组的单个元素 ###################
88
echo ${nums[1]}
99
# Output: 1
1010

11-
# 访问数组的所有元素
11+
################### 访问数组的所有元素 ###################
1212
echo ${colors[*]}
1313
# Output: red yellow dark blue
1414

@@ -32,23 +32,23 @@ printf "+ %s\n" "${colors[@]}"
3232
# + yellow
3333
# + dark blue
3434

35-
# 访问数组的部分元素
35+
################### 访问数组的部分元素 ###################
3636
echo ${nums[@]:0:2}
3737
# Output:
3838
# 0 1
3939

40-
# 访问数组长度
40+
################### 获取数组长度 ###################
4141
echo ${#nums[*]}
4242
# Output:
4343
# 3
4444

45-
# 向数组中添加元素
45+
################### 向数组中添加元素 ###################
4646
colors=( white "${colors[@]}" green black )
4747
echo ${colors[@]}
4848
# Output:
4949
# white red yellow dark blue green black
5050

51-
# 从数组中删除元素
51+
################### 从数组中删除元素 ###################
5252
unset nums[ 0 ]
5353
echo ${nums[@]}
5454
# Output:

0 commit comments

Comments
 (0)