Shell基础
Shell脚本开始
#!/bin/bash
#!/bin/bash -e
function 获取参数
updateContainer(){
Name=$1
URL=$2
}
updateContainer $name $url
获取function 返回值。方法体内通过echo返回值。
updateContainer(){
Name=$1
URL=$2
echo $Name $URL
}
echo $(updateContainer "serverA" "http://127.0.0.1:8080")
循环
TRIED_TIMES=0
while [[ "$TRIED_TIMES" != "$MAX_ALLOW_TIME" ]]
do
do something...
TRIED_TIMES=$((TRIED_TIMES+1))
done
数组
JavaOptions[0]="-Xmx2048M -XX:+PrintFlagsFinal -XX:+PrintGCDetails"
JavaOptions[1]="-Xmx4096M -XX:+PrintFlagsFinal -XX:+PrintGCDetails"
echo ${JavaOptions[0]}
数组并循环
services=(eureka zipkin serviceA serviceB)
for someService in "${services[@]}"
do
echo $someService
# eureka...
done
字符串转数组并循环
str="eureka zipkin serviceA serviceB"
services=( $str )
for someService in "${services[@]}"
do
echo $someService
# eureka...
done
循环时获取索引
str="eureka zipkin serviceA serviceB"
services=( $str )
for i in "${!services[@]}" do
echo $i
# 0 1 2 3
done
条件判断。注意[
和]
的空格!
#!/bin/bash
a=abcd
b=22
c=abcd
d=22
if [ $a = $c ]; then
echo "a = c"
else
echo "a <> c"
fi
if [ $a = $b ]; then
echo "a = b"
else
echo "a <> b"
fi
if [[ $a = $c && $b = $d ]]; then
echo "a = c and b = d"
fi
if [ $a = $c ] && [ $b = $d ]; then
echo "a = c and b = d"
fi
if [ $a = $c ] || [ $b -eq $d ]; then
echo "a = c or b = d"
fi
重定向,避免pipeline中太多无用(例如下载进度)的日志:
mvn ... > /dev/null
脚本调用,当不同的shell脚本互相调用时,可以通过source命令将当前环境的变量传递给被调用的脚本。
a.sh
#!/bin/bash
Server=hello
source ./b.sh
b.sh
#!/bin/bash
echo $Server
./a.sh
hello
文件操作
判断文件中是否有特定字符串
if [ "$(grep 'is not running' service-version.txt)" ]; then
echo "Some services are not running."
else
echo "All services are running."
fi
读取”key=value”格式文件的value:
file: services.version
serviceA=1.2
serviceB=1.3
file: version.sh
#!/bin/bash
getVersion() {
serviceName=$1
fileName=$2
tmpStr="$(grep -Po "(?<=^$serviceName).*" $fileName)"
#tmpStr: =1.2
version=${tmpStr:1}
#version: 1.2
echo $version
}
getVersion serviceA ./services.version
文件内容替换
sed -e "s/search content/replace content/" filename > savetofilename
示例1:将apply plugin: 'java'
替换为apply plugin: 'war'
sed -e "s/apply plugin: 'java'/apply plugin: 'war'/" build.tmp > build.gradle
示例2:同时进行多次替换: 将包含spring-cloud-starter-hystrix
的行替换为providedCompile("org.springframework.boot:spring-boot-starter-tomcat")
,替换文本包括注释//
时要用转义符,所以是\/\/
sed -e "s/apply plugin: 'java'/apply plugin: 'war'/" \
-e 's/.*spring-cloud-starter-hystrix.*/providedCompile("org.springframework.boot:spring-boot-starter-tomcat")/' \
-e 's/.*spring-cloud-starter-sleuth.*/\/\/spring-cloud-starter-sleuth is removed/' \
-e 's/.*spring-cloud-starter-zipkin.*/\/\/spring-cloud-starter-zipkin is removed/' build.tmp > build.gradle
文本追加,>>
是追加,而>
就直接替换了:
echo "war { baseName = '$SERVICE_NAME'}" >> build.gradle
网络操作
SSH远程执行命令
注意echo \$Action $Name
中,\$Action
表示远程服务器上的变量,而$Name
会在本地解释出来。
Name=bar
ssh user@remote_ip /bin/bash << EOF
sudo -i
Action=foo
echo \$Action $Name
EOF
# foo bar
利用CURL的HTTP status判断服务是否已经启动
STATUS=$(curl -s -o /dev/null -w '%{http_code}' "$URL")
echo "Got $STATUS "
if [ $STATUS -eq 200 ]; then
echo "is running."
exit 0
else
echo ":( Wating startup..."
fi
字符串操作
替换
示例一:将_
替换成-
,将小写替换成大写
#!/bin/bash
a=abc_def_gh
b=`echo $a | tr "_" "-"`
c=$(echo $a | tr [a-z] [A-Z]) #与`的作用相同
echo $b #abc-def-gh
echo $c #ABC_DEF_GH
示例二:求嵌套的字符串值。
a="ab"
c="c"
abc="I want you"
key="$a$c"
value=`echo ${!key}`
echo $value #I want you
Maven
获取指定版本:
mvn dependency:get -Dartifact=beta.release:services-version:$GO_PIPELINE_LABEL \
-DrepoUrl=http://$NEXUS_USER:$NEXUS_PASSWORD@$NEXUS_REPO_IP:$NEXUS_REPO_PORT/repository/production-repo -Dpackaging=tar.gz \
-Ddest=./somefile.tar.gz
上传
mvn deploy:deploy-file -DgroupId=beta.release \
-DartifactId=services-version -Dversion=$GO_PIPELINE_LABEL \
-DgeneratePom=true -Dpackaging=tar.gz -DrepositoryId=nexus \
-Durl=http://$NEXUS_USER:$NEXUS_PASSWORD@$NEXUS_REPO_IP:$NEXUS_REPO_PORT/repository/production-repo \
-Dfile=somefile.tar.gz
网友评论