diff --git a/codes/shell/action/oper/input.sh b/codes/shell/action/oper/input.sh new file mode 100644 index 0000000..29ad918 --- /dev/null +++ b/codes/shell/action/oper/input.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +############################################################## +# 很多场景下,我们需要在执行脚本时,输入指定的参数,脚本会 +# 根据参数执行不同的行为。这就需要读取脚本参数并校验。 +############################################################## + +################### 读取脚本输入参数并校验 ################### +declare -a serial +serial=(start stop restart) +echo -n "请选择操作(可选值:start|stop|restart):" +read oper +if ! echo ${serial[@]} | grep -q ${oper}; then + echo "请选择正确操作(可选值:start|stop|restart)" + exit 1 +fi + +declare -a serial2 +serial2=(dev test prod) +echo -n "请选择运行环境(可选值:dev|test|prod):" +read profile +if ! echo ${serial2[@]} | grep -q ${profile}; then + echo "请选择正确运行环境(可选值:dev|test|prod)" + exit 1 +fi diff --git a/codes/shell/action/system/dir.sh b/codes/shell/action/system/dir.sh new file mode 100644 index 0000000..b9499d0 --- /dev/null +++ b/codes/shell/action/system/dir.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +current_dir=$(cd `dirname $0`; pwd) +echo "当前目录是:${current_dir}" + +parent_dir=$(dirname $(pwd)) +echo "父目录是:${parent_dir}" diff --git a/codes/shell/demos/string-demo.sh b/codes/shell/demos/string-demo.sh index 26bd6ae..652c57a 100644 --- a/codes/shell/demos/string-demo.sh +++ b/codes/shell/demos/string-demo.sh @@ -35,3 +35,20 @@ text="hello" echo `expr index "${text}" ll` # Output: # 3 + +################### 截取关键字左边内容 ################### +str="feature/1.0.0" +branch=`echo ${str#feature/}` +echo "branch is ${branch}" + +################### 截取关键字右边内容 ################### +key=`echo ${str%/1.0.0}` +echo "key is ${key}" + +################### 判断字符串中是否包含子字符串 ################### +result=$(echo "${str}" | grep "feature/") +if [[ "$result" != "" ]] ; then + echo "feature/ 是 ${str} 的子字符串" +else + echo "feature/ 不是 ${str} 的子字符串" +fi