脚本示例

This commit is contained in:
Zhang Peng 2019-03-26 14:05:24 +08:00
parent a105d6f4a7
commit 6ada50051e
3 changed files with 49 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
current_dir=$(cd `dirname $0`; pwd)
echo "当前目录是:${current_dir}"
parent_dir=$(dirname $(pwd))
echo "父目录是:${parent_dir}"

View File

@ -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