Skip to content

Commit 6ada500

Browse files
committed
脚本示例
1 parent a105d6f commit 6ada500

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

codes/shell/action/oper/input.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
##############################################################
4+
# 很多场景下,我们需要在执行脚本时,输入指定的参数,脚本会
5+
# 根据参数执行不同的行为。这就需要读取脚本参数并校验。
6+
##############################################################
7+
8+
################### 读取脚本输入参数并校验 ###################
9+
declare -a serial
10+
serial=(start stop restart)
11+
echo -n "请选择操作(可选值:start|stop|restart):"
12+
read oper
13+
if ! echo ${serial[@]} | grep -q ${oper}; then
14+
echo "请选择正确操作(可选值:start|stop|restart)"
15+
exit 1
16+
fi
17+
18+
declare -a serial2
19+
serial2=(dev test prod)
20+
echo -n "请选择运行环境(可选值:dev|test|prod):"
21+
read profile
22+
if ! echo ${serial2[@]} | grep -q ${profile}; then
23+
echo "请选择正确运行环境(可选值:dev|test|prod)"
24+
exit 1
25+
fi

codes/shell/action/system/dir.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
current_dir=$(cd `dirname $0`; pwd)
4+
echo "当前目录是:${current_dir}"
5+
6+
parent_dir=$(dirname $(pwd))
7+
echo "父目录是:${parent_dir}"

codes/shell/demos/string-demo.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ text="hello"
3535
echo `expr index "${text}" ll`
3636
# Output:
3737
# 3
38+
39+
################### 截取关键字左边内容 ###################
40+
str="feature/1.0.0"
41+
branch=`echo ${str#feature/}`
42+
echo "branch is ${branch}"
43+
44+
################### 截取关键字右边内容 ###################
45+
key=`echo ${str%/1.0.0}`
46+
echo "key is ${key}"
47+
48+
################### 判断字符串中是否包含子字符串 ###################
49+
result=$(echo "${str}" | grep "feature/")
50+
if [[ "$result" != "" ]] ; then
51+
echo "feature/ 是 ${str} 的子字符串"
52+
else
53+
echo "feature/ 不是 ${str} 的子字符串"
54+
fi

0 commit comments

Comments
 (0)