format codes

This commit is contained in:
Zhang Peng 2019-10-10 08:56:31 +08:00
parent d336b79af1
commit 0c58f678fe
186 changed files with 2169 additions and 2013 deletions

View File

@ -1,7 +1,7 @@
# EditorConfig helps developers define and maintain consistent # EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs # coding styles between different editors and IDEs
# http://editorconfig.org # http://editorconfig.org
# 所有文件换行使用 Unix like 风格LFbat 文件使用 win 风格CRLF # 所有文件换行以 Unix like 风格LFwin 格式特定的除外bat
# 缩进 java 4 个空格,其他所有文件 2 个空格 # 缩进 java 4 个空格,其他所有文件 2 个空格
root = true root = true
@ -11,9 +11,8 @@ root = true
end_of_line = lf end_of_line = lf
# Change these settings to your own preference # Change these settings to your own preference
indent_size = 2
indent_style = space indent_style = space
max_line_length = 120 indent_size = 2
# We recommend you to keep these unchanged # We recommend you to keep these unchanged
charset = utf-8 charset = utf-8
@ -23,9 +22,8 @@ insert_final_newline = true
[*.bat] [*.bat]
end_of_line = crlf end_of_line = crlf
[*.java] [*.{java, sh}]
indent_size = 4 indent_size = 4
[*.md] [*.md]
max_line_length = 0
trim_trailing_whitespace = false trim_trailing_whitespace = false

View File

@ -2,7 +2,7 @@
# 打印UI页头信息 # 打印UI页头信息
function printHeadInfo() { function printHeadInfo() {
cat << EOF cat << EOF
*********************************************************************************** ***********************************************************************************
* 欢迎使用项目引导式发布脚本。 * 欢迎使用项目引导式发布脚本。
* 输入任意键进入脚本操作。 * 输入任意键进入脚本操作。
@ -12,7 +12,7 @@ EOF
# 打印UI页尾信息 # 打印UI页尾信息
function printFootInfo() { function printFootInfo() {
cat << EOF cat << EOF
*********************************************************************************** ***********************************************************************************
@ -25,18 +25,18 @@ EOF
# 检查文件是否存在,不存在则退出脚本 # 检查文件是否存在,不存在则退出脚本
function checkFileExist() { function checkFileExist() {
if [ ! -f "$1" ] if [ ! -f "$1" ]
then then
echo "关键文件 $1 找不到,脚本执行结束" echo "关键文件 $1 找不到,脚本执行结束"
exit 1 exit 1
fi fi
} }
# 检查文件夹是否存在,不存在则创建 # 检查文件夹是否存在,不存在则创建
function createFolderIfNotExist() { function createFolderIfNotExist() {
if [ ! -d "$1" ];then if [ ! -d "$1" ]; then
mkdir -p "$1" mkdir -p "$1"
fi fi
} }
# 记录发布的版本信息 # 记录发布的版本信息
@ -45,18 +45,18 @@ function createFolderIfNotExist() {
# 第三个参数为代码分支 # 第三个参数为代码分支
# 第四个参数为运行环境 # 第四个参数为运行环境
function saveVersionInfo() { function saveVersionInfo() {
if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then
echo "缺少参数,退出" echo "缺少参数,退出"
exit 1 exit 1
fi fi
VERSION_LOG_FILE=$1/$2-version.log VERSION_LOG_FILE=$1/$2-version.log
rm -rf ${VERSION_LOG_FILE} rm -rf ${VERSION_LOG_FILE}
touch ${VERSION_LOG_FILE} touch ${VERSION_LOG_FILE}
chmod 777 ${VERSION_LOG_FILE} chmod 777 ${VERSION_LOG_FILE}
echo -e "\n=================== $2 ===================" >> ${VERSION_LOG_FILE} echo -e "\n=================== $2 ===================" >> ${VERSION_LOG_FILE}
echo "Branch is: $3" >> ${VERSION_LOG_FILE} echo "Branch is: $3" >> ${VERSION_LOG_FILE}
echo "Profile is: $4" >> ${VERSION_LOG_FILE} echo "Profile is: $4" >> ${VERSION_LOG_FILE}
echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE}
} }

View File

@ -7,22 +7,22 @@
# 检查脚本参数,如必要参数未传入,退出脚本。 # 检查脚本参数,如必要参数未传入,退出脚本。
function checkInput() { function checkInput() {
if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then
echo "请输入脚本参数app oper javaArgs classpathArgs bootstrapClass" echo "请输入脚本参数app oper javaArgs classpathArgs bootstrapClass"
echo " app: 应用名。" echo " app: 应用名。"
echo " oper: 运行环境必填。可选值start|stop|restart" echo " oper: 运行环境必填。可选值start|stop|restart"
echo " javaArgs: JVM 参数(必填)。" echo " javaArgs: JVM 参数(必填)。"
echo " classpathArgs: classpath参数必填。" echo " classpathArgs: classpath参数必填。"
echo " bootstrapClass: 启动类(必填)。" echo " bootstrapClass: 启动类(必填)。"
exit 0 exit 0
fi fi
} }
# 检查文件夹是否存在,不存在则创建 # 检查文件夹是否存在,不存在则创建
function createFolderIfNotExist() { function createFolderIfNotExist() {
if [ ! -d "$1" ];then if [ ! -d "$1" ]; then
mkdir -p "$1" mkdir -p "$1"
fi fi
} }
# 检查服务是否已经启动 # 检查服务是否已经启动
@ -37,46 +37,46 @@ function checkStarted() {
} }
function main() { function main() {
case "${oper}" in case "${oper}" in
start ) start)
echo -n "starting server: " echo -n "starting server: "
# 检查服务是否已经启动 # 检查服务是否已经启动
if checkStarted ;then if checkStarted; then
echo "ERROR: server already started!" echo "ERROR: server already started!"
echo "PID: ${pids}" echo "PID: ${pids}"
exit 1 exit 1
fi fi
args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}" args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}"
echo -e "statup params:\n ${args}" echo -e "statup params:\n ${args}"
#启动服务 #启动服务
touch ${LOG_DIR}/${app}-startup.log touch ${LOG_DIR}/${app}-startup.log
nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 & nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 &
# echo -e "执行参数:\n${args}" # echo -e "执行参数:\n${args}"
echo -e "\nthe server is started..." echo -e "\nthe server is started..."
;; ;;
stop ) stop)
echo -n "stopping server: " echo -n "stopping server: "
#dubbo提供优雅停机, 不能使用kill -9 #dubbo提供优雅停机, 不能使用kill -9
if checkStarted ;then if checkStarted; then
kill ${pids} kill ${pids}
echo -e "\nthe server is stopped..." echo -e "\nthe server is stopped..."
else else
echo -e "\nno server to be stopped..." echo -e "\nno server to be stopped..."
fi fi
;; ;;
restart ) restart)
$0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" $0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}"
sleep 5 sleep 5
$0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" $0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}"
;; ;;
* ) *)
echo "Invalid oper: ${oper}." echo "Invalid oper: ${oper}."
exit 1 exit 1
esac esac
exit 0 exit 0
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################

View File

@ -7,41 +7,41 @@
# 检查脚本参数,如必要参数未传入,退出脚本。 # 检查脚本参数,如必要参数未传入,退出脚本。
checkInput() { checkInput() {
if [ "${branch}" == "" ] || [ "${profile}" == "" ]; then if [ "${branch}" == "" ] || [ "${profile}" == "" ]; then
echo "请输入脚本参数branch profile" echo "请输入脚本参数branch profile"
echo " branch: git分支必填。如 feature/1.1.16, master" echo " branch: git分支必填。如 feature/1.1.16, master"
echo " profile: 运行环境必填。可选值development | test" echo " profile: 运行环境必填。可选值development | test"
echo "例:./java-app-release.sh feature/1.1.16 test" echo "例:./java-app-release.sh feature/1.1.16 test"
exit 0 exit 0
fi fi
} }
# 检查文件是否存在,不存在则退出脚本 # 检查文件是否存在,不存在则退出脚本
checkFileExist() { checkFileExist() {
if [ ! -f "$1" ] if [ ! -f "$1" ]
then then
echo "关键文件 $1 找不到,脚本执行结束" echo "关键文件 $1 找不到,脚本执行结束"
exit 0 exit 0
fi fi
} }
# 检查文件夹是否存在,不存在则创建 # 检查文件夹是否存在,不存在则创建
createFolderIfNotExist() { createFolderIfNotExist() {
if [ ! -d "$1" ];then if [ ! -d "$1" ]; then
mkdir -p "$1" mkdir -p "$1"
fi fi
} }
# 记录发布的版本信息 # 记录发布的版本信息
saveVersionInfo() { saveVersionInfo() {
rm -rf ${VERSION_LOG_FILE} rm -rf ${VERSION_LOG_FILE}
touch ${VERSION_LOG_FILE} touch ${VERSION_LOG_FILE}
chmod 777 ${VERSION_LOG_FILE} chmod 777 ${VERSION_LOG_FILE}
echo -e "\n=================== Version Info ===================" >> ${VERSION_LOG_FILE} echo -e "\n=================== Version Info ===================" >> ${VERSION_LOG_FILE}
echo "Branch is: ${branch}" >> ${VERSION_LOG_FILE} echo "Branch is: ${branch}" >> ${VERSION_LOG_FILE}
echo "Profile is: ${profile}" >> ${VERSION_LOG_FILE} echo "Profile is: ${profile}" >> ${VERSION_LOG_FILE}
echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE}
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
@ -50,7 +50,8 @@ export LANG="zh_CN.UTF-8"
# 设置全局常量 # 设置全局常量
LOG_DIR=/home/zp/log/ LOG_DIR=/home/zp/log/
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) SCRIPT_DIR=$(cd "$(dirname "$0")";
pwd)
SOURCE_DIR=/home/zp/source/ SOURCE_DIR=/home/zp/source/
APP_NAME=XXX APP_NAME=XXX
RESOURCES_DIR=/home/zp/source/${APP_NAME}/src/main/resources RESOURCES_DIR=/home/zp/source/${APP_NAME}/src/main/resources
@ -73,10 +74,10 @@ echo ">>>>>>>>>>>>>> 2. 更新代码"
${UPDATE_CODE_SCRIPT_FILE} ${APP_NAME} ${branch} ${SOURCE_DIR} ${UPDATE_CODE_SCRIPT_FILE} ${APP_NAME} ${branch} ${SOURCE_DIR}
execode=$? execode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "更新代码成功" echo "更新代码成功"
else else
echo "更新代码失败" echo "更新代码失败"
exit 1 exit 1
fi fi
echo ">>>>>>>>>>>>>> 3. 替换配置" echo ">>>>>>>>>>>>>> 3. 替换配置"
@ -87,12 +88,12 @@ cd ${SOURCE_DIR}/ck-lion
mvn clean package -e -Dmaven.test.skip=true | tee ${MAVEN_LOG_FILE} mvn clean package -e -Dmaven.test.skip=true | tee ${MAVEN_LOG_FILE}
eexecode=$? eexecode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "构建编译成功" echo "构建编译成功"
echo "编译详情见:${MAVEN_LOG_FILE}" echo "编译详情见:${MAVEN_LOG_FILE}"
else else
echo "构建编译失败" echo "构建编译失败"
echo "编译详情见:${MAVEN_LOG_FILE}" echo "编译详情见:${MAVEN_LOG_FILE}"
exit 1 exit 1
fi fi
echo ">>>>>>>>>>>>>> 5. 启动应用" echo ">>>>>>>>>>>>>> 5. 启动应用"
@ -101,10 +102,10 @@ echo 3 > /proc/sys/vm/drop_caches
${SCRIPT_DIR}/java-app-run.sh ${profile} start ${SCRIPT_DIR}/java-app-run.sh ${profile} start
execode=$? execode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "启动应用成功" echo "启动应用成功"
else else
echo "启动应用失败" echo "启动应用失败"
exit 1 exit 1
fi fi
echo ">>>>>>>>>>>>>> 6. 记录发布的版本信息" echo ">>>>>>>>>>>>>> 6. 记录发布的版本信息"

View File

@ -7,51 +7,51 @@
# 检查脚本参数,如必要参数未传入,退出脚本。 # 检查脚本参数,如必要参数未传入,退出脚本。
function checkInput() { function checkInput() {
if [ "${profile}" == "" ] || [ "${oper}" == "" ]; then if [ "${profile}" == "" ] || [ "${oper}" == "" ]; then
echo "请输入脚本参数profile oper [debug]" echo "请输入脚本参数profile oper [debug]"
echo " profile: 运行环境必填。可选值development|test" echo " profile: 运行环境必填。可选值development|test"
echo " oper: 运行环境必填。可选值start|stop|restart" echo " oper: 运行环境必填。可选值start|stop|restart"
echo " debug: debug启动开关。默认不填为不启动。" echo " debug: debug启动开关。默认不填为不启动。"
exit 0 exit 0
fi fi
} }
#检查文件是否存在,不存在则退出脚本 #检查文件是否存在,不存在则退出脚本
function checkFileExist() { function checkFileExist() {
if [ ! -f "$1" ] if [ ! -f "$1" ]
then then
echo "关键文件 $1 找不到,脚本执行结束" echo "关键文件 $1 找不到,脚本执行结束"
exit 0 exit 0
fi fi
} }
# 封装启动参数,调用启动脚本 # 封装启动参数,调用启动脚本
function main(){ function main() {
APP_NAME=ck-lion APP_NAME=ck-lion
# JVM 参数 # JVM 参数
JAVA_OPTS=" -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true -Dspring.profiles.active=${profile} -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xmx1024m -Xss2m " JAVA_OPTS=" -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true -Dspring.profiles.active=${profile} -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xmx1024m -Xss2m "
JAVA_DEBUG_OPTS="" JAVA_DEBUG_OPTS=""
if [ "$2" == "debug" ]; then if [ "$2" == "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2236,server=y,suspend=n " JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2236,server=y,suspend=n "
shift shift
fi fi
javaArgs=" ${JAVA_OPTS} ${JAVA_DEBUG_OPTS} " javaArgs=" ${JAVA_OPTS} ${JAVA_DEBUG_OPTS} "
# classpath 参数 # classpath 参数
classpathArgs="${SERVER_ROOT}/WEB-INF/classes:${SERVER_ROOT}/WEB-INF/lib/*" classpathArgs="${SERVER_ROOT}/WEB-INF/classes:${SERVER_ROOT}/WEB-INF/lib/*"
# 启动类 # 启动类
bootstrapClass="com.alibaba.dubbo.container.Main" bootstrapClass="com.alibaba.dubbo.container.Main"
${SCRIPT_DIR}/java-app-boot.sh ${APP_NAME} ${oper} "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" ${SCRIPT_DIR}/java-app-boot.sh ${APP_NAME} ${oper} "${javaArgs}" "${classpathArgs}" "${bootstrapClass}"
execode=$? execode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "执行操作成功" echo "执行操作成功"
else else
echo "执行操作失败" echo "执行操作失败"
exit 1 exit 1
fi fi
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
@ -59,7 +59,8 @@ function main(){
export LANG="zh_CN.UTF-8" export LANG="zh_CN.UTF-8"
# 设置全局常量 # 设置全局常量
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) SCRIPT_DIR=$(cd "$(dirname "$0")";
pwd)
SOURCE_DIR=/home/zp/source/ SOURCE_DIR=/home/zp/source/
APP_NAME=XXX APP_NAME=XXX
SERVER_ROOT=/home/zp/source/${APP_NAME}/target/ SERVER_ROOT=/home/zp/source/${APP_NAME}/target/

View File

@ -5,50 +5,50 @@
# 检查脚本参数,如必要参数未传入,退出脚本。 # 检查脚本参数,如必要参数未传入,退出脚本。
function checkInput() { function checkInput() {
if [ "${branch}" == "" ]; then if [ "${branch}" == "" ]; then
echo "请输入脚本参数branch" echo "请输入脚本参数branch"
echo " branch: git分支。如 feature/1.1.16, master" echo " branch: git分支。如 feature/1.1.16, master"
exit 1 exit 1
fi fi
} }
# 脚本主方法 # 脚本主方法
function main() { function main() {
echo ">>>>>>>>>>>>>> 1. 更新代码" echo ">>>>>>>>>>>>>> 1. 更新代码"
${SCRIPT_DIR}/update-code.sh ${APP} ${branch} ${SOURCE_DIR} ${SCRIPT_DIR}/update-code.sh ${APP} ${branch} ${SOURCE_DIR}
execode=$? execode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "更新代码成功" echo "更新代码成功"
else else
echo "更新代码失败" echo "更新代码失败"
exit 1 exit 1
fi fi
echo ">>>>>>>>>>>>>> 2. 替换配置" echo ">>>>>>>>>>>>>> 2. 替换配置"
# 有的应用此处可能需要替换配置 # 有的应用此处可能需要替换配置
echo ">>>>>>>>>>>>>> 3. 构建编译" echo ">>>>>>>>>>>>>> 3. 构建编译"
cd ${SOURCE_DIR}/${APP} cd ${SOURCE_DIR}/${APP}
source "${HOME}/.nvm/nvm.sh" source "${HOME}/.nvm/nvm.sh"
nvm use 8.9 nvm use 8.9
npm install npm install
if [ "${profile}" == "develop" ] || [ "${profile}" == "test" ]; then if [ "${profile}" == "develop" ] || [ "${profile}" == "test" ]; then
npm start npm start
elif [ "${profile}" == "preview" ] || [ "${profile}" == "product" ]; then elif [ "${profile}" == "preview" ] || [ "${profile}" == "product" ]; then
npm run build npm run build
fi fi
execode=$? execode=$?
if [ "${execode}" == "0" ]; then if [ "${execode}" == "0" ]; then
echo "构建编译成功" echo "构建编译成功"
else else
echo "构建编译失败" echo "构建编译失败"
exit 1 exit 1
fi fi
echo ">>>>>>>>>>>>>> 4. 记录发布的版本信息" echo ">>>>>>>>>>>>>> 4. 记录发布的版本信息"
saveVersionInfo ${LOG_DIR} ${APP} ${branch} ${profile} saveVersionInfo ${LOG_DIR} ${APP} ${branch} ${profile}
echo ">>>>>>>>>>>>>> 发布应用结束" echo ">>>>>>>>>>>>>> 发布应用结束"
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
@ -59,7 +59,8 @@ export LANG="zh_CN.UTF-8"
APP=blog APP=blog
LOG_DIR=/home/zp/log LOG_DIR=/home/zp/log
SOURCE_DIR=/home/zp/source SOURCE_DIR=/home/zp/source
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) SCRIPT_DIR=$(cd "$(dirname "$0")";
pwd)
# 装载函数库 # 装载函数库
. ${SCRIPT_DIR}/helper.sh . ${SCRIPT_DIR}/helper.sh

View File

@ -7,7 +7,7 @@
# 选择应用 # 选择应用
function chooseAppName() { function chooseAppName() {
cat << EOF cat << EOF
请选择应用名(数字或关键字均可)。 请选择应用名(数字或关键字均可)。
可选值如下: 可选值如下:
[0] all (所有应用) [0] all (所有应用)
@ -15,28 +15,28 @@ cat << EOF
[2] APP2 [2] APP2
EOF EOF
while read app while read app
do do
case ${app} in case ${app} in
0 ) 0)
app=all app=all
break ;; break ;;
1 ) 1)
app=js-app app=js-app
break ;; break ;;
2 ) 2)
app=APP2 app=APP2
break ;; break ;;
all | js-app | APP2 ) all | js-app | APP2)
break ;; break ;;
* ) echo "无法识别 ${app}" ;; *) echo "无法识别 ${app}" ;;
esac esac
done done
} }
# 选择操作 # 选择操作
function chooseOper() { function chooseOper() {
cat << EOF cat << EOF
请选择想要执行的操作(数字或关键字均可)。 请选择想要执行的操作(数字或关键字均可)。
可选值如下: 可选值如下:
[1] start [1] start
@ -44,44 +44,44 @@ cat << EOF
[3] stop [3] stop
EOF EOF
while read oper while read oper
do do
case ${oper} in case ${oper} in
1 ) 1)
oper=start oper=start
break ;; break ;;
2 ) 2)
oper=restart oper=restart
break ;; break ;;
3 ) 3)
oper=stop oper=stop
break ;; break ;;
start | restart | stop ) start | restart | stop)
break ;; break ;;
* ) echo "无法识别 ${oper}" ;; *) echo "无法识别 ${oper}" ;;
esac esac
done done
} }
# 选择代码分支 # 选择代码分支
function chooseBranch() { function chooseBranch() {
cat << EOF cat << EOF
请输入 git 分支。 请输入 git 分支。
develop、master、feature/xxx develop、master、feature/xxx
EOF EOF
read branch read branch
if [[ "${branch}" =~ ^(feature/)([^ \f\n\r\t\v]+) ]] || [ "${branch}" == "develop" ] || [ "${branch}" == "master" ]; then if [[ "${branch}" =~ ^ ( feature/ ) ( [^ \f\n\r\t\v]+ ) ]] || [ "${branch}" == "develop" ] || [ "${branch}" == "master" ]; then
echo "输入了 ${branch}" echo "输入了 ${branch}"
else else
echo "无法识别 ${branch}" echo "无法识别 ${branch}"
chooseBranch chooseBranch
fi fi
} }
# 选择运行环境 # 选择运行环境
function chooseProfile() { function chooseProfile() {
cat << EOF cat << EOF
请选择运行环境(数字或关键字均可)。 请选择运行环境(数字或关键字均可)。
可选值: 可选值:
[1] develop (开发环境) [1] develop (开发环境)
@ -90,32 +90,32 @@ cat << EOF
[4] product (生产环境) [4] product (生产环境)
EOF EOF
while read profile while read profile
do do
case ${profile} in case ${profile} in
1 ) 1)
profile=develop profile=develop
break ;; break ;;
2 ) 2)
profile=test profile=test
break ;; break ;;
3 ) 3)
profile=preview profile=preview
break ;; break ;;
4 ) 4)
profile=product profile=product
break ;; break ;;
develop | test | preview | product ) develop | test | preview | product)
break ;; break ;;
* ) echo "无法识别 ${profile}" ;; *) echo "无法识别 ${profile}" ;;
esac esac
done done
} }
# 确认选择 # 确认选择
function confirmChoice() { function confirmChoice() {
cat << EOF cat << EOF
=================================================== ===================================================
请确认您的选择Y/N 请确认您的选择Y/N
app: ${app} app: ${app}
@ -125,66 +125,66 @@ cat << EOF
=================================================== ===================================================
EOF EOF
while read confirm while read confirm
do do
case ${confirm} in case ${confirm} in
y | Y ) y | Y)
echo -e "\n\n>>>>>>>>>>>>>> 开始发布应用" echo -e "\n\n>>>>>>>>>>>>>> 开始发布应用"
break ;; break ;;
n | N ) n | N)
echo -e "重新输入发布参数\n" echo -e "重新输入发布参数\n"
inputParams ;; inputParams ;;
* ) *)
echo "无法识别 ${confirm}" ;; echo "无法识别 ${confirm}" ;;
esac esac
done done
} }
# 引导式发布应用 # 引导式发布应用
function releaseApp() { function releaseApp() {
# 输入执行参数 # 输入执行参数
app="" app=""
branch="" branch=""
profile="" profile=""
chooseAppName chooseAppName
chooseOper chooseOper
if [ "${oper}" == "stop" ]; then if [ "${oper}" == "stop" ]; then
confirmChoice confirmChoice
if [ "${app}" == "all" ]; then if [ "${app}" == "all" ]; then
${SCRIPT_DIR}/${app}-run.sh stop ${profile} ${SCRIPT_DIR}/${app}-run.sh stop ${profile}
else
${SCRIPT_DIR}/${app}-run.sh stop ${profile}
fi
else else
${SCRIPT_DIR}/${app}-run.sh stop ${profile} chooseBranch
chooseProfile
confirmChoice
if [ "${app}" == "all" ]; then
${SCRIPT_DIR}/js-app-release.sh ${branch} ${profile}
else
${SCRIPT_DIR}/${app}-release.sh ${branch} ${profile}
fi
fi fi
else
chooseBranch
chooseProfile
confirmChoice
if [ "${app}" == "all" ]; then
${SCRIPT_DIR}/js-app-release.sh ${branch} ${profile}
else
${SCRIPT_DIR}/${app}-release.sh ${branch} ${profile}
fi
fi
} }
# 脚本主方法 # 脚本主方法
function main() { function main() {
printHeadInfo printHeadInfo
while read sign while read sign
do do
case ${sign} in case ${sign} in
exit) exit)
echo "主动退出脚本" echo "主动退出脚本"
exit 0 ;; exit 0 ;;
* ) *)
releaseApp ;; releaseApp ;;
esac esac
# 装载函数库 # 装载函数库
printFootInfo printFootInfo
done done
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
@ -192,7 +192,8 @@ done
export LANG="zh_CN.UTF-8" export LANG="zh_CN.UTF-8"
# 设置全局常量 # 设置全局常量
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) SCRIPT_DIR=$(cd "$(dirname "$0")";
pwd)
SOURCE_DIR=/home/zp/source/ SOURCE_DIR=/home/zp/source/
# 装载函数库 # 装载函数库

View File

@ -8,78 +8,78 @@
# 检查脚本参数,如必要参数未传入,退出脚本。 # 检查脚本参数,如必要参数未传入,退出脚本。
checkInput() { checkInput() {
if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then
echo "请输入脚本参数repository branch [source] [target]" echo "请输入脚本参数repository branch [source] [target]"
echo " repository: git 仓储(必填)。" echo " repository: git 仓储(必填)。"
echo " branch: git 分支(必填)。如 master/develop" echo " branch: git 分支(必填)。如 master/develop"
echo " source: 代码存放目录。默认为/home/zp/source。" echo " source: 代码存放目录。默认为/home/zp/source。"
echo " target: 代码存放目录。默认为脚本所在目录。" echo " target: 代码存放目录。默认为脚本所在目录。"
exit 1 exit 1
fi fi
} }
# 检查文件夹是否存在,不存在则创建 # 检查文件夹是否存在,不存在则创建
function createFolderIfNotExist() { function createFolderIfNotExist() {
if [ ! -d "$1" ];then if [ ! -d "$1" ]; then
mkdir -p "$1" mkdir -p "$1"
fi fi
} }
# 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。 # 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。
gitok=false gitok=false
function isGitExist() { function isGitExist() {
cd ${SOURCE_DIR}
if [ -d "${SOURCE_DIR}/${repository}/${target}" ]; then
cd ${SOURCE_DIR}/${repository}/${target}
#(1)删除git状态零时文件
if [ -f "gitstatus.tmp" ]; then
rm -rf gitstatus.tmp
fi
#(2) 判断是否存在.git目录
if [ -d "./.git" ]; then
#(3) 判断git是否可用
git status &> gitstatus.tmp
grep -iwq 'not a git repository' gitstatus.tmp && gitok=false || gitok=true
fi
#返回到主目录
cd ${SOURCE_DIR} cd ${SOURCE_DIR}
fi if [ -d "${SOURCE_DIR}/${repository}/${target}" ]; then
cd ${SOURCE_DIR}/${repository}/${target}
#(1)删除git状态零时文件
if [ -f "gitstatus.tmp" ]; then
rm -rf gitstatus.tmp
fi
#(2) 判断是否存在.git目录
if [ -d "./.git" ]; then
#(3) 判断git是否可用
git status &> gitstatus.tmp
grep -iwq 'not a git repository' gitstatus.tmp && gitok=false || gitok=true
fi
#返回到主目录
cd ${SOURCE_DIR}
fi
} }
# 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。 # 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。
function doFetchOrClone() { function doFetchOrClone() {
if ${gitok}; then if ${gitok}; then
cd ${SOURCE_DIR}/${repository}/${target} cd ${SOURCE_DIR}/${repository}/${target}
git reset --hard git reset --hard
git clean -ffdx git clean -ffdx
git fetch git fetch
echo "git fetch ${repository} remote repository 到本地成功" echo "git fetch ${repository} remote repository 到本地成功"
else else
#删除所有内容,便于重新进行git clone #删除所有内容,便于重新进行git clone
rm -rf ${repository} rm -rf ${repository}
git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target} git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target}
echo "git clone ${repository} remote repository 到本地成功" echo "git clone ${repository} remote repository 到本地成功"
cd ${SOURCE_DIR}/${repository}/${target} cd ${SOURCE_DIR}/${repository}/${target}
fi fi
} }
# 切换到 ${branch} 分支 # 切换到 ${branch} 分支
function doCheckout() { function doCheckout() {
echo "检出 ${repository} ${branch} 分支代码" echo "检出 ${repository} ${branch} 分支代码"
isRemoteBranch=false isRemoteBranch=false
gitRemoteBranch=`git branch -r` gitRemoteBranch=`git branch -r`
echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false
if ${isRemoteBranch}; then if ${isRemoteBranch}; then
echo "找到 ${branch} 分支。" echo "找到 ${branch} 分支。"
git checkout -f 'origin/'${branch} git checkout -f 'origin/'${branch}
else else
echo "未找到 ${branch} 分支!" echo "未找到 ${branch} 分支!"
exit 2 exit 2
fi fi
echo "更新子模块代码" echo "更新子模块代码"
git submodule update --init --recursive --force git submodule update --init --recursive --force
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
@ -96,7 +96,7 @@ checkInput
GITHUB_ACCOUNT=dunwu GITHUB_ACCOUNT=dunwu
SOURCE_DIR=/home/xyz/source SOURCE_DIR=/home/xyz/source
if [ "${source}" != "" ]; then if [ "${source}" != "" ]; then
SOURCE_DIR=${source} SOURCE_DIR=${source}
fi fi
createFolderIfNotExist ${SOURCE_DIR} createFolderIfNotExist ${SOURCE_DIR}

View File

@ -29,15 +29,17 @@ printf "${RESET}"
path=/home/scripts/linux-tutorial path=/home/scripts/linux-tutorial
printf "\n${GREEN}>>>>>>>> Download linux-tutorial to ${path} begin.${RESET}\n" printf "\n${GREEN}>>>>>>>> Download linux-tutorial to ${path} begin.${RESET}\n"
command -v yum >/dev/null 2>&1 || { printf "${RED}Not detected yum.${RESET}"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Not detected yum.${RESET}";
command -v git >/dev/null 2>&1 || { printf "${YELLOW}Not detected git. Install git.${RESET}\n"; yum -y install git; } exit 1; }
command -v git > /dev/null 2>&1 || { printf "${YELLOW}Not detected git. Install git.${RESET}\n";
yum -y install git; }
if [[ -d ${path} ]]; then if [[ -d ${path} ]]; then
cd ${path} cd ${path}
git pull git pull
else else
mkdir -p ${path} mkdir -p ${path}
git clone https://gitee.com/turnon/linux-tutorial.git ${path} git clone https://gitee.com/turnon/linux-tutorial.git ${path}
fi fi
chmod +x -R ${path} chmod +x -R ${path}
printf "\n${GREEN}<<<<<<<< Download linux-tutorial to ${path} end.${RESET}\n" printf "\n${GREEN}<<<<<<<< Download linux-tutorial to ${path} end.${RESET}\n"

View File

@ -14,8 +14,8 @@ RESET="$(tput sgr0)"
# 打印头部信息 # 打印头部信息
printHeadInfo() { printHeadInfo() {
printf "${BLUE}" printf "${BLUE}"
cat << EOF cat << EOF
*********************************************************************************** ***********************************************************************************
* 欢迎使用 Linux CentOS 环境运维脚本 * 欢迎使用 Linux CentOS 环境运维脚本
@ -23,67 +23,68 @@ cat << EOF
*********************************************************************************** ***********************************************************************************
EOF EOF
printf "${RESET}" printf "${RESET}"
} }
# 打印尾部信息 # 打印尾部信息
printFootInfo() { printFootInfo() {
printf "${BLUE}" printf "${BLUE}"
cat << EOF cat << EOF
*********************************************************************************** ***********************************************************************************
* 脚本执行结束,感谢使用! * 脚本执行结束,感谢使用!
*********************************************************************************** ***********************************************************************************
EOF EOF
printf "${RESET}" printf "${RESET}"
} }
# 检查操作系统环境 # 检查操作系统环境
checkOsVersion(){ checkOsVersion() {
if(($1 == 1)); then if (($1 == 1)); then
echo -e "检查操作系统环境是否兼容本套脚本" echo -e "检查操作系统环境是否兼容本套脚本"
platform=`uname -i` platform=`uname -i`
if [[ ${platform} != "x86_64" ]]; then if [[ ${platform} != "x86_64" ]]; then
echo "脚本仅支持 64 位操作系统!" echo "脚本仅支持 64 位操作系统!"
exit 1 exit 1
fi
version=`cat /etc/redhat-release | awk '{print substr($4,1,1)}'`
if [[ ${version} != 7 ]]; then
echo "脚本仅支持 CentOS 7"
exit 1
fi
echo -e "脚本可以在本环境运行!"
fi fi
version=`cat /etc/redhat-release | awk '{print substr($4,1,1)}'`
if [[ ${version} != 7 ]]; then
echo "脚本仅支持 CentOS 7"
exit 1
fi
echo -e "脚本可以在本环境运行!"
fi
} }
menus=("配置系统" "安装软件" "退出") menus=( "配置系统" "安装软件" "退出" )
main() { main() {
PS3="请输入命令编号:" PS3="请输入命令编号:"
select item in ${menus[@]} select item in ${menus[@]}
do do
case ${item} in case ${item} in
"配置系统") "配置系统")
./dunwu-sys.sh ./dunwu-sys.sh
main ;; main ;;
"安装软件") "安装软件")
./dunwu-soft.sh ./dunwu-soft.sh
main ;; main ;;
"退出") "退出")
exit 0 ;; exit 0 ;;
*) *)
printf "输入项不支持!\n" printf "输入项不支持!\n"
main ;; main ;;
esac esac
break break
done done
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
path=$(cd "$(dirname "$0")"; pwd) path=$(cd "$(dirname "$0")";
pwd)
printHeadInfo printHeadInfo
checkOsVersion 0 checkOsVersion 0
main main

View File

@ -24,37 +24,38 @@ EOF
# print menu # print menu
printf "${PURPLE}" printf "${PURPLE}"
menus=(docker fastdfs gitlab jdk8 jenkins kafka maven mongodb mysql nacos nexus nginx nodejs redis rocketmq tomcat8 menus=( docker fastdfs gitlab jdk8 jenkins kafka maven mongodb mysql nacos nexus nginx nodejs redis rocketmq tomcat8
zookeeper zsh exit) zookeeper zsh exit )
for i in "${!menus[@]}"; do for i in "${!menus[@]}"; do
index=`expr ${i} + 1` index=`expr ${i} + 1`
val=`expr ${index} % 2` val=`expr ${index} % 2`
printf "[%02d] %-20s" "${index}" "${menus[$i]}" printf "[%02d] %-20s" "${index}" "${menus[$i]}"
if [[ ${val} -eq 0 ]]; then if [[ ${val} -eq 0 ]]; then
printf "\n" printf "\n"
fi fi
done done
printf "\n${RESET}请输入需要安装的软件编号:\n" printf "\n${RESET}请输入需要安装的软件编号:\n"
# exec shell to install soft # exec shell to install soft
doInstall() { doInstall() {
read -t 30 index read -t 30 index
if [[ -n ${index} ]]; then if [[ -n ${index} ]]; then
no=`expr ${index} - 1` no=`expr ${index} - 1`
len=${#menus[*]} len=${#menus[*]}
if [[ ${index} -gt ${len} ]]; then if [[ ${index} -gt ${len} ]]; then
echo "输入项不支持!" echo "输入项不支持!"
exit -1 exit -1
fi fi
key=${menus[$no]} key=${menus[$no]}
if [[ key == 'exit' ]]; then if [[ key == 'exit' ]]; then
exit 0 exit 0
fi fi
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/${key}-install.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/${key}-install.sh | bash
doInstall doInstall
else else
echo "输入项不支持!" echo "输入项不支持!"
exit -1 exit -1
fi fi
} }
doInstall doInstall

View File

@ -9,38 +9,38 @@ cat << EOF
EOF EOF
menus=("替换yum镜像" "安装基本的命令工具" "安装常用libs" "系统配置" "全部执行" "退出") menus=( "替换yum镜像" "安装基本的命令工具" "安装常用libs" "系统配置" "全部执行" "退出" )
main() { main() {
PS3="请输入命令编号:" PS3="请输入命令编号:"
select item in ${menus[@]} select item in ${menus[@]}
do do
case ${item} in case ${item} in
"替换yum镜像") "替换yum镜像")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash
main ;; main ;;
"安装基本的命令工具") "安装基本的命令工具")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools.sh | bash
main ;; main ;;
"安装常用libs") "安装常用libs")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash
main ;; main ;;
"系统配置") "系统配置")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash
main ;; main ;;
"全部执行") "全部执行")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools | bash
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash
printf "执行完毕,退出。\n" ;; printf "执行完毕,退出。\n" ;;
"退出") "退出")
exit 0 ;; exit 0 ;;
*) *)
printf "输入项不支持!\n" printf "输入项不支持!\n"
main ;; main ;;
esac esac
break break
done done
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################

View File

@ -26,16 +26,16 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install docker begin.${RESET}\n" printf "${GREEN}>>>>>>>> install docker begin.${RESET}\n"
# uninstall old version docker # uninstall old version docker
sudo yum remove docker \ sudo yum remove docker \
docker-client \ docker-client \
docker-client-latest \ docker-client-latest \
docker-common \ docker-common \
docker-latest \ docker-latest \
docker-latest-logrotate \ docker-latest-logrotate \
docker-logrotate \ docker-logrotate \
docker-selinux \ docker-selinux \
docker-engine-selinux \ docker-engine-selinux \
docker-engine docker-engine
# install required libs # install required libs
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# add docker yum repo # add docker yum repo

View File

@ -2,50 +2,50 @@
# 检查脚本输入参数 # 检查脚本输入参数
checkInput() { checkInput() {
if [ "${app}" == "" ] || [ "${oper}" == "" ]; then if [ "${app}" == "" ] || [ "${oper}" == "" ]; then
echo "请输入脚本参数name" echo "请输入脚本参数name"
echo " app: 要启动的进程关键字必填。可选值elasticsearch|logstash|kibana|filebeat" echo " app: 要启动的进程关键字必填。可选值elasticsearch|logstash|kibana|filebeat"
echo " oper: 执行操作必填。可选值start|stop" echo " oper: 执行操作必填。可选值start|stop"
echo "例:./boot-elk.sh logstash start" echo "例:./boot-elk.sh logstash start"
exit 0 exit 0
fi fi
if [ "${app}" != "elasticsearch" ] && [ "${app}" != "logstash" ] && [ "${app}" != "kibana" ] && [ "${app}" != "filebeat" ]; then if [ "${app}" != "elasticsearch" ] && [ "${app}" != "logstash" ] && [ "${app}" != "kibana" ] && [ "${app}" != "filebeat" ]; then
echo "name 输入错误" echo "name 输入错误"
echo "可选值elasticsearch|logstash|kibana|filebeat" echo "可选值elasticsearch|logstash|kibana|filebeat"
exit 0 exit 0
fi fi
} }
# 检查文件是否存在,不存在则退出脚本 # 检查文件是否存在,不存在则退出脚本
checkFileExist() { checkFileExist() {
if [ ! -f "$1" ] if [ ! -f "$1" ]
then then
echo "关键文件 $1 找不到,脚本执行结束" echo "关键文件 $1 找不到,脚本执行结束"
exit 0 exit 0
fi fi
} }
startup() { startup() {
if [ "${app}" == "elasticsearch" ]; then if [ "${app}" == "elasticsearch" ]; then
checkFileExist ${ELASTICSEARCH_BIN_PATH}/elasticsearch checkFileExist ${ELASTICSEARCH_BIN_PATH}/elasticsearch
nohup sh ${ELASTICSEARCH_BIN_PATH}/elasticsearch >>${ELASTICSEARCH_BIN_PATH}/nohup.out 2>&1 & nohup sh ${ELASTICSEARCH_BIN_PATH}/elasticsearch >> ${ELASTICSEARCH_BIN_PATH}/nohup.out 2>&1 &
elif [ "${app}" == "logstash" ]; then elif [ "${app}" == "logstash" ]; then
checkFileExist ${LOGSTASH_BIN_PATH}/logstash checkFileExist ${LOGSTASH_BIN_PATH}/logstash
nohup sh ${LOGSTASH_BIN_PATH}/logstash -f ${LOGSTASH_BIN_PATH}/logstash.conf >>${LOGSTASH_BIN_PATH}/nohup.out 2>&1 & nohup sh ${LOGSTASH_BIN_PATH}/logstash -f ${LOGSTASH_BIN_PATH}/logstash.conf >> ${LOGSTASH_BIN_PATH}/nohup.out 2>&1 &
elif [ "${app}" == "kibana" ]; then elif [ "${app}" == "kibana" ]; then
checkFileExist ${KIBANA_BIN_PATH}/kibana checkFileExist ${KIBANA_BIN_PATH}/kibana
nohup sh ${KIBANA_BIN_PATH}/kibana >> ${KIBANA_BIN_PATH}/nohup.out 2>&1 & nohup sh ${KIBANA_BIN_PATH}/kibana >> ${KIBANA_BIN_PATH}/nohup.out 2>&1 &
elif [ "${app}" == "filebeat" ]; then elif [ "${app}" == "filebeat" ]; then
checkFileExist ${FILEBEAT_PATH}/filebeat checkFileExist ${FILEBEAT_PATH}/filebeat
touch ${FILEBEAT_PATH}/nohup.out touch ${FILEBEAT_PATH}/nohup.out
nohup ${FILEBEAT_PATH}/filebeat -e -c ${FILEBEAT_PATH}/filebeat.yml -d "publish" >> ${FILEBEAT_PATH}/nohup.out 2>&1 & nohup ${FILEBEAT_PATH}/filebeat -e -c ${FILEBEAT_PATH}/filebeat.yml -d "publish" >> ${FILEBEAT_PATH}/nohup.out 2>&1 &
fi fi
} }
shutdown() { shutdown() {
pid=`ps -ef | grep java | grep ${app} | awk '{print $2}'` pid=`ps -ef | grep java | grep ${app} | awk '{print $2}'`
kill -9 ${pid} kill -9 ${pid}
} }
##############################__MAIN__######################################## ##############################__MAIN__########################################
@ -60,14 +60,14 @@ FILEBEAT_PATH=/opt/elastic/filebeat-${version}-linux-x86_64
checkInput checkInput
case ${oper} in case ${oper} in
start) start)
echo "启动 ${app}" echo "启动 ${app}"
startup startup
;; ;;
stop) stop)
echo "终止 ${app}" echo "终止 ${app}"
shutdown shutdown
;; ;;
* ) echo "${oper} is invalid oper";; *) echo "${oper} is invalid oper" ;;
esac esac

View File

@ -9,98 +9,99 @@
# 获取当前设备IP # 获取当前设备IP
ipaddr='127.0.0.1' ipaddr='127.0.0.1'
function getDeviceIp() { function getDeviceIp() {
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}') ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
} }
# 检查文件是否存在,不存在则退出脚本 # 检查文件是否存在,不存在则退出脚本
checkFileExist() { checkFileExist() {
if [ ! -f "$1" ] if [ ! -f "$1" ]
then then
echo "关键文件 $1 找不到,脚本执行结束" echo "关键文件 $1 找不到,脚本执行结束"
exit 0 exit 0
fi fi
} }
init() { init() {
mkdir -p ${ELASTIC_SOFTWARE_PATH} mkdir -p ${ELASTIC_SOFTWARE_PATH}
getDeviceIp getDeviceIp
} }
# 安装 elasticsearch # 安装 elasticsearch
installElasticsearch() { installElasticsearch() {
cd ${ELASTIC_SOFTWARE_PATH} cd ${ELASTIC_SOFTWARE_PATH}
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${version}.tar.gz wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${version}.tar.gz
tar -xzf elasticsearch-${version}.tar.gz tar -xzf elasticsearch-${version}.tar.gz
} }
installRuby() { installRuby() {
cd ${RUBY_SOFTWARE_PATH} cd ${RUBY_SOFTWARE_PATH}
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.gz wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.gz
tar -xzf ruby-2.5.0.tar.gz tar -xzf ruby-2.5.0.tar.gz
cd ruby-2.5.0 cd ruby-2.5.0
./configure ./configure
make & make install make & make install
} }
# 安装 logstash # 安装 logstash
installLogstash() { installLogstash() {
cd ${ELASTIC_SOFTWARE_PATH} cd ${ELASTIC_SOFTWARE_PATH}
wget https://artifacts.elastic.co/downloads/logstash/logstash-${version}.tar.gz wget https://artifacts.elastic.co/downloads/logstash/logstash-${version}.tar.gz
tar -xzf logstash-${version}.tar.gz tar -xzf logstash-${version}.tar.gz
} }
# 安装 kibana # 安装 kibana
installKibana() { installKibana() {
cd ${ELASTIC_SOFTWARE_PATH} cd ${ELASTIC_SOFTWARE_PATH}
wget https://artifacts.elastic.co/downloads/kibana/kibana-${version}-linux-x86_64.tar.gz wget https://artifacts.elastic.co/downloads/kibana/kibana-${version}-linux-x86_64.tar.gz
tar -xzf kibana-${version}-linux-x86_64.tar.gz tar -xzf kibana-${version}-linux-x86_64.tar.gz
} }
# 安装 filebeat # 安装 filebeat
installFilebeat() { installFilebeat() {
cd ${ELASTIC_SOFTWARE_PATH} cd ${ELASTIC_SOFTWARE_PATH}
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${version}-linux-x86_64.tar.gz wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${version}-linux-x86_64.tar.gz
tar -zxf filebeat-${version}-linux-x86_64.tar.gz tar -zxf filebeat-${version}-linux-x86_64.tar.gz
} }
# 替换 Elasticsearch 配置 # 替换 Elasticsearch 配置
# 1. 替换 192.168.0.1 为本机 IP # 1. 替换 192.168.0.1 为本机 IP
replaceElasticsearchConfig() { replaceElasticsearchConfig() {
cp ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml.bak cp ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml.bak
sed -i "s/#network.host: 192.168.0.1/network.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml sed -i "s/#network.host: 192.168.0.1/network.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/config/elasticsearch.yml
touch ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/bin/nohup.out touch ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/bin/nohup.out
} }
replaceLogstashConfig() { replaceLogstashConfig() {
cp ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml.bak cp ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml.bak
sed -i "s/# http.host: \"127.0.0.1\"/ http.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml sed -i "s/# http.host: \"127.0.0.1\"/ http.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/config/logstash.yml
touch ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin/nohup.out touch ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin/nohup.out
cd ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin cd ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin
wget "https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/elk/config/logstash.conf" wget "https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/elk/config/logstash.conf"
} }
# 替换 Kibana 配置 # 替换 Kibana 配置
# 1. 替换 localhost 为本机 IP # 1. 替换 localhost 为本机 IP
replaceKibanaConfig() { replaceKibanaConfig() {
cp ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml.bak cp ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml.bak
sed -i "s/#server.host: \"localhost\"/server.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml sed -i "s/#server.host: \"localhost\"/server.host: ${IP}/g" ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml
sed -i "s/#elasticsearch.url: \"http://localhost:9200\"/#elasticsearch.url: \"${IP}\"/g" ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml sed -i "s/#elasticsearch.url: \"http://localhost:9200\"/#elasticsearch.url: \"${IP}\"/g" ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/config/kibana.yml
touch ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/bin/nohup.out touch ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/bin/nohup.out
} }
# 替换 Filebeat 配置 # 替换 Filebeat 配置
replaceFilebeatConfig() { replaceFilebeatConfig() {
cp ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml.bak cp ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml.bak
cd ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64 cd ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64
wget https://github.com/dunwu/OS/blob/master/codes/deploy/tool/elk/config/filebeat.yml wget https://github.com/dunwu/OS/blob/master/codes/deploy/tool/elk/config/filebeat.yml
sed -i 's/127.0.0.1/'"${IP}"'/g' ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml sed -i 's/127.0.0.1/'"${IP}"'/g' ${ELASTIC_SOFTWARE_PATH}/filebeat-${version}-linux-x86_64/filebeat.yml
} }
# 为 elk.elk 用户设置权限 # 为 elk.elk 用户设置权限
setPrivilegeForUser() { setPrivilegeForUser() {
chown -R elk.elk ${ELASTIC_SOFTWARE_PATH} chown -R elk.elk ${ELASTIC_SOFTWARE_PATH}
chown -R elk.elk /var/log/ chown -R elk.elk /var/log/
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
echo -e "\n>>>>>>>>> install elk" echo -e "\n>>>>>>>>> install elk"

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# version:v1.0 # version:v1.0
# func:elasticsearch 5.4.1/6.0.1/6.3.1安装 # func:elasticsearch 5.4.1/6.0.1/6.3.1安装
@ -12,9 +13,9 @@ env_file=/etc/profile.d/elasticsearch.sh
install_log_path=/var/log/appinstall/ install_log_path=/var/log/appinstall/
install_path=/usr/local/ install_path=/usr/local/
software_config_file=${install_path}elasticsearch/config/elasticsearch.yml software_config_file=${install_path}elasticsearch/config/elasticsearch.yml
sysversion=$(rpm -q centos-release|cut -d- -f3) sysversion=$(rpm -q centos-release | cut -d- -f3)
jvm_conf="/usr/local/elasticsearch/config/jvm.options" jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` sys_mem=`free -m | grep Mem: | awk '{print $2}' | awk '{sum+=$1} END {print sum/1024}' | cut -d. -f1`
hostname=elk-server hostname=elk-server
clear clear
@ -29,23 +30,23 @@ echo "3: Install elasticsearch 6.3.1"
echo "4: EXIT" echo "4: EXIT"
# 选择安装软件版本 # 选择安装软件版本
read -p "Please input your choice:" softversion read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then if [ "${softversion}" == "1" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz"
elif [ "${softversion}" == "2" ];then elif [ "${softversion}" == "2" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz"
elif [ "${softversion}" == "3" ];then elif [ "${softversion}" == "3" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz"
elif [ "${softversion}" == "4" ];then elif [ "${softversion}" == "4" ]; then
echo "you choce channel!" echo "you choce channel!"
exit 1; exit 1;
else else
echo "input Error! Place input{1|2|3|4|5}" echo "input Error! Place input{1|2|3|4|5}"
exit 0; exit 0;
fi fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() { output_msg() {
for msg in $*;do for msg in $*; do
action $msg /bin/true action $msg /bin/true
done done
} }
@ -53,21 +54,21 @@ output_msg() {
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() { check_yum_command() {
output_msg "命令检查:$1" output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1 hash $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0
else else
yum -y install $2 >/dev/null 2>&1 yum -y install $2 > /dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi fi
} }
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 # 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() { check_dir() {
output_msg "目录检查" output_msg "目录检查"
for dirname in $*;do for dirname in $*; do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done done
} }
@ -75,13 +76,13 @@ check_dir() {
# 下载文件并解压至安装目录,传入url链接地址 # 下载文件并解压至安装目录,传入url链接地址
download_file() { download_file() {
output_msg "下载源码包" output_msg "下载源码包"
mkdir -p $download_path mkdir -p $download_path
for file in $*;do for file in $*; do
wget $file -c -P $download_path &> /dev/null wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` $file download fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
@ -89,54 +90,54 @@ download_file() {
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() { extract_file() {
output_msg "解压源码" output_msg "解压源码"
for file in $*;do for file in $*; do
if [ "${file##*.}" == "gz" ];then if [ "${file##*.}" == "gz" ]; then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then elif [ "${file##*.}" == "zip" ]; then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
# 配置环境变量,第一个参数为添加环境变量的绝对路径 # 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() { config_env() {
output_msg "环境变量配置" output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file} echo "export PATH=\$PATH:$1" > ${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name}
} }
# 配置主机名,第一个为主机名 # 配置主机名,第一个为主机名
config_hostname() { config_hostname() {
if [ ${sysversion} -eq 6 ];then if [ ${sysversion} -eq 6 ]; then
hostname $1 hostname $1
elif [ ${sysversion} -eq 7 ];then elif [ ${sysversion} -eq 7 ]; then
hostnamectl set-hostname $1 hostnamectl set-hostname $1
else else
echo "`date +%F' '%H:%M:%S` hostname $1 config fail">> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` hostname $1 config fail" >> ${install_log_path}${install_log_name}
fi fi
} }
config_limits() { config_limits() {
output_msg "配置limits" output_msg "配置limits"
cat >>/etc/security/limits.conf<<EOF cat >> /etc/security/limits.conf << EOF
* soft nofile 65536 * soft nofile 65536
* hard nofile 65536 * hard nofile 65536
* soft nproc 65536 * soft nproc 65536
* hard nproc 65536 * hard nproc 65536
EOF EOF
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf echo "vm.max_map_count = 655360" >> /etc/sysctl.conf
sysctl -p >/dev/null 2>&1 sysctl -p > /dev/null 2>&1
} }
# 添加配置文件 # 添加配置文件
add_config() { add_config() {
cat > $1 << EOF cat > $1 << EOF
cluster.name: my-application cluster.name: my-application
node.name: ${hostname} node.name: ${hostname}
path.data: /usr/local/elasticsearch/data path.data: /usr/local/elasticsearch/data
@ -148,46 +149,46 @@ EOF
} }
config_user() { config_user() {
useradd $1 >/dev/null 2>&1 useradd $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $1 user add success">> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $1 user add success" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%S` $1 user add fail">> ${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%S` $1 user add fail" >> ${install_log_path}${install_log_name} && exit 1
fi fi
chown ${1}.${1} ${install_path}elasticsearch/ -R chown ${1}.${1} ${install_path}elasticsearch/ -R
} }
config_jvm() { config_jvm() {
if [ ${sys_mem} -eq 0 ];then if [ ${sys_mem} -eq 0 ]; then
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else else
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi fi
} }
main() { main() {
check_dir $install_log_path $install_path check_dir $install_log_path $install_path
check_yum_command wget wget check_yum_command wget wget
download_file $URL download_file $URL
config_hostname $hostname config_hostname $hostname
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do for filename in `ls $download_path`; do
extract_file ${download_path}$filename extract_file ${download_path}$filename
done done
rm -fr ${download_path} rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}elasticsearch ln -s $install_path$software_name ${install_path}elasticsearch
add_config $software_config_file add_config $software_config_file
check_dir ${install_path}elasticsearch/{data,logs} check_dir ${install_path}elasticsearch/{data,logs}
config_user elasticsearch config_user elasticsearch
config_env ${install_path}elasticsearch/bin config_env ${install_path}elasticsearch/bin
config_limits config_limits
config_jvm config_jvm
echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'" echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'"
} }

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# version:v1.0 # version:v1.0
# func:filebeat 5.6.1/6.1.3/6.3.2 安装 # func:filebeat 5.6.1/6.1.3/6.3.2 安装
@ -26,23 +27,23 @@ echo "3: Install filebeat 6.3.2"
echo "4: EXIT" echo "4: EXIT"
# 选择安装软件版本 # 选择安装软件版本
read -p "Please input your choice:" softversion read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then if [ "${softversion}" == "1" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "2" ];then elif [ "${softversion}" == "2" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz"
elif [ "${softversion}" == "3" ];then elif [ "${softversion}" == "3" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz"
elif [ "${softversion}" == "4" ];then elif [ "${softversion}" == "4" ]; then
echo "you choce channel!" echo "you choce channel!"
exit 1; exit 1;
else else
echo "input Error! Place input{1|2|3|4}" echo "input Error! Place input{1|2|3|4}"
exit 0; exit 0;
fi fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() { output_msg() {
for msg in $*;do for msg in $*; do
action $msg /bin/true action $msg /bin/true
done done
} }
@ -50,21 +51,21 @@ output_msg() {
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() { check_yum_command() {
output_msg "命令检查:$1" output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1 hash $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0
else else
yum -y install $2 >/dev/null 2>&1 yum -y install $2 > /dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi fi
} }
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 # 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() { check_dir() {
output_msg "目录检查" output_msg "目录检查"
for dirname in $*;do for dirname in $*; do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done done
} }
@ -72,13 +73,13 @@ check_dir() {
# 下载文件并解压至安装目录,传入url链接地址 # 下载文件并解压至安装目录,传入url链接地址
download_file() { download_file() {
output_msg "下载源码包" output_msg "下载源码包"
mkdir -p $download_path mkdir -p $download_path
for file in $*;do for file in $*; do
wget $file -c -P $download_path &> /dev/null wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` $file download fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
@ -86,29 +87,29 @@ download_file() {
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() { extract_file() {
output_msg "解压源码" output_msg "解压源码"
for file in $*;do for file in $*; do
if [ "${file##*.}" == "gz" ];then if [ "${file##*.}" == "gz" ]; then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then elif [ "${file##*.}" == "zip" ]; then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
# 配置环境变量,第一个参数为添加环境变量的绝对路径 # 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() { config_env() {
output_msg "环境变量配置" output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file} echo "export PATH=\$PATH:$1" > ${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name}
} }
# 添加配置文件 # 添加配置文件
add_config() { add_config() {
cat> $1 <<EOF cat > $1 << EOF
filebeat.prospectors: filebeat.prospectors:
- input_type: log - input_type: log
paths: paths:
@ -119,17 +120,17 @@ EOF
} }
main() { main() {
check_dir $install_log_path $install_path check_dir $install_log_path $install_path
check_yum_command wget wget check_yum_command wget wget
download_file $URL download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do for filename in `ls $download_path`; do
extract_file ${download_path}$filename extract_file ${download_path}$filename
done done
rm -fr ${download_path} rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}filebeat ln -s $install_path$software_name ${install_path}filebeat
add_config ${software_config_file} add_config ${software_config_file}
} }
main main

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# version:v1.0 # version:v1.0
# func:kibana 6.0.1/6.2.4/6.3.1 安装 # func:kibana 6.0.1/6.2.4/6.3.1 安装
@ -26,23 +27,23 @@ echo "3: Install kibana 6.3.1"
echo "4: EXIT" echo "4: EXIT"
# 选择安装软件版本 # 选择安装软件版本
read -p "Please input your choice:" softversion read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then if [ "${softversion}" == "1" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.0.1-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.0.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "2" ];then elif [ "${softversion}" == "2" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.2.4-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.2.4-linux-x86_64.tar.gz"
elif [ "${softversion}" == "3" ];then elif [ "${softversion}" == "3" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.3.1-linux-x86_64.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.3.1-linux-x86_64.tar.gz"
elif [ "${softversion}" == "4" ];then elif [ "${softversion}" == "4" ]; then
echo "you choce channel!" echo "you choce channel!"
exit 1; exit 1;
else else
echo "input Error! Place input{1|2|3|4}" echo "input Error! Place input{1|2|3|4}"
exit 0; exit 0;
fi fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() { output_msg() {
for msg in $*;do for msg in $*; do
action $msg /bin/true action $msg /bin/true
done done
} }
@ -50,21 +51,21 @@ output_msg() {
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() { check_yum_command() {
output_msg "命令检查:$1" output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1 hash $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0
else else
yum -y install $2 >/dev/null 2>&1 yum -y install $2 > /dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi fi
} }
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 # 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() { check_dir() {
output_msg "目录检查" output_msg "目录检查"
for dirname in $*;do for dirname in $*; do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done done
} }
@ -72,13 +73,13 @@ check_dir() {
# 下载文件并解压至安装目录,传入url链接地址 # 下载文件并解压至安装目录,传入url链接地址
download_file() { download_file() {
output_msg "下载源码包" output_msg "下载源码包"
mkdir -p $download_path mkdir -p $download_path
for file in $*;do for file in $*; do
wget $file -c -P $download_path &> /dev/null wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` $file download fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
@ -86,29 +87,29 @@ download_file() {
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() { extract_file() {
output_msg "解压源码" output_msg "解压源码"
for file in $*;do for file in $*; do
if [ "${file##*.}" == "gz" ];then if [ "${file##*.}" == "gz" ]; then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then elif [ "${file##*.}" == "zip" ]; then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
# 配置环境变量,第一个参数为添加环境变量的绝对路径 # 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() { config_env() {
output_msg "环境变量配置" output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file} echo "export PATH=\$PATH:$1" > ${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name}
} }
# 添加配置文件 # 添加配置文件
add_config() { add_config() {
cat> $1 <<EOF cat > $1 << EOF
server.port: 5601 server.port: 5601
server.host: "0.0.0.0" server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200" elasticsearch.url: "http://127.0.0.1:9200"
@ -116,18 +117,18 @@ EOF
} }
main() { main() {
check_dir $install_log_path $install_path check_dir $install_log_path $install_path
check_yum_command wget wget check_yum_command wget wget
download_file $URL download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do for filename in `ls $download_path`; do
extract_file ${download_path}$filename extract_file ${download_path}$filename
done done
rm -fr ${download_path} rm -fr ${download_path}
ln -s ${install_path}$software_name ${install_path}kibana ln -s ${install_path}$software_name ${install_path}kibana
add_config ${software_config_file} add_config ${software_config_file}
config_env ${install_path}kibana/bin config_env ${install_path}kibana/bin
} }
main main

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# version:v1.0 # version:v1.0
# func:logstash 5.4/6.1/6.3 安装 # func:logstash 5.4/6.1/6.3 安装
@ -26,23 +27,23 @@ echo "3: Install logstash-6.3"
echo "4: EXIT" echo "4: EXIT"
# 选择安装软件版本 # 选择安装软件版本
read -p "Please input your choice:" softversion read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then if [ "${softversion}" == "1" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-5.4.1.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-5.4.1.tar.gz"
elif [ "${softversion}" == "2" ];then elif [ "${softversion}" == "2" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.1.3.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.1.3.tar.gz"
elif [ "${softversion}" == "3" ];then elif [ "${softversion}" == "3" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.3.2.tar.gz" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.3.2.tar.gz"
elif [ "${softversion}" == "4" ];then elif [ "${softversion}" == "4" ]; then
echo "you choce channel!" echo "you choce channel!"
exit 1; exit 1;
else else
echo "input Error! Place input{1|2|3|4}" echo "input Error! Place input{1|2|3|4}"
exit 0; exit 0;
fi fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() { output_msg() {
for msg in $*;do for msg in $*; do
action $msg /bin/true action $msg /bin/true
done done
} }
@ -50,21 +51,21 @@ output_msg() {
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() { check_yum_command() {
output_msg "命令检查:$1" output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1 hash $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0
else else
yum -y install $2 >/dev/null 2>&1 yum -y install $2 > /dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi fi
} }
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 # 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() { check_dir() {
output_msg "目录检查" output_msg "目录检查"
for dirname in $*;do for dirname in $*; do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done done
} }
@ -72,13 +73,13 @@ check_dir() {
# 下载文件并解压至安装目录,传入url链接地址 # 下载文件并解压至安装目录,传入url链接地址
download_file() { download_file() {
output_msg "下载源码包" output_msg "下载源码包"
mkdir -p $download_path mkdir -p $download_path
for file in $*;do for file in $*; do
wget $file -c -P $download_path &> /dev/null wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` $file download fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
@ -86,29 +87,29 @@ download_file() {
# 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录
extract_file() { extract_file() {
output_msg "解压源码" output_msg "解压源码"
for file in $*;do for file in $*; do
if [ "${file##*.}" == "gz" ];then if [ "${file##*.}" == "gz" ]; then
tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} tar -zxf $file -C $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
elif [ "${file##*.}" == "zip" ];then elif [ "${file##*.}" == "zip" ]; then
unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path">>${install_log_path}${install_log_name} unzip -q $file -d $install_path && echo "`date +%F' '%H:%M:%S` $file extrac success!,path is $install_path" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
# 配置环境变量,第一个参数为添加环境变量的绝对路径 # 配置环境变量,第一个参数为添加环境变量的绝对路径
config_env() { config_env() {
output_msg "环境变量配置" output_msg "环境变量配置"
echo "export PATH=\$PATH:$1" >${env_file} echo "export PATH=\$PATH:$1" > ${env_file}
source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name}
} }
# 添加配置文件 # 添加配置文件
add_config() { add_config() {
cat> $1 <<EOF cat > $1 << EOF
input { input {
beats { beats {
port => "5044" port => "5044"
@ -124,18 +125,18 @@ EOF
} }
main() { main() {
check_dir $install_log_path $install_path check_dir $install_log_path $install_path
check_yum_command wget wget check_yum_command wget wget
download_file $URL download_file $URL
software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}')
for filename in `ls $download_path`;do for filename in `ls $download_path`; do
extract_file ${download_path}$filename extract_file ${download_path}$filename
done done
rm -fr ${download_path} rm -fr ${download_path}
ln -s $install_path$software_name ${install_path}logstash ln -s $install_path$software_name ${install_path}logstash
add_config ${software_config_file} add_config ${software_config_file}
config_env ${install_path}logstash/bin config_env ${install_path}logstash/bin
} }
main main

View File

@ -28,19 +28,21 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install fastdfs begin.${RESET}\n" printf "${GREEN}>>>>>>>> install fastdfs begin.${RESET}\n"
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
command -v git >/dev/null 2>&1 || { printf "${RED}Require git but it's not installed.${RESET}\n"; exit 1; } exit 1; }
command -v git > /dev/null 2>&1 || { printf "${RED}Require git but it's not installed.${RESET}\n";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh fastdfs-install.sh [path]\n" printf "\t sh fastdfs-install.sh [path]\n"
printf "\t Example: sh fastdfs-install.sh /opt/fastdfs\n" printf "\t Example: sh fastdfs-install.sh /opt/fastdfs\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
path=/opt/fdfs path=/opt/fdfs
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
path=$1 path=$1
fi fi
nginx_version=1.16.0 nginx_version=1.16.0
@ -55,8 +57,8 @@ path=/opt/fdfs
mkdir -p ${path}/libfastcommon mkdir -p ${path}/libfastcommon
curl -o ${path}/libfastcommon.zip http://dunwu.test.upcdn.net/soft/fdfs/libfastcommon.zip curl -o ${path}/libfastcommon.zip http://dunwu.test.upcdn.net/soft/fdfs/libfastcommon.zip
if [[ ! -f ${path}/libfastcommon.zip ]]; then if [[ ! -f ${path}/libfastcommon.zip ]]; then
printf "${RED}[Error]install libfastcommon failedexit. ${RESET}\n" printf "${RED}[Error]install libfastcommon failedexit. ${RESET}\n"
exit 1 exit 1
fi fi
unzip -o ${path}/libfastcommon.zip -d ${path} unzip -o ${path}/libfastcommon.zip -d ${path}
@ -69,7 +71,7 @@ printf "${GREEN}>>>>>>>>> install fastdfs${RESET}"
mkdir -p ${path}/fastdfs mkdir -p ${path}/fastdfs
curl -o ${path}/fastdfs.zip http://dunwu.test.upcdn.net/soft/fdfs/fastdfs.zip curl -o ${path}/fastdfs.zip http://dunwu.test.upcdn.net/soft/fdfs/fastdfs.zip
if [[ ! -f ${path}/fastdfs.zip ]]; then if [[ ! -f ${path}/fastdfs.zip ]]; then
printf "${RED}>>>>>>>>> install fastdfs failedexit. ${RESET}\n" printf "${RED}>>>>>>>>> install fastdfs failedexit. ${RESET}\n"
fi fi
unzip -o ${path}/fastdfs.zip -d ${path} unzip -o ${path}/fastdfs.zip -d ${path}
cd ${path}/fastdfs cd ${path}/fastdfs
@ -80,7 +82,7 @@ printf "${GREEN}>>>>>>>>> install fastdfs-nginx-module${RESET}\n"
mkdir -p ${path}/fastdfs-nginx-module mkdir -p ${path}/fastdfs-nginx-module
curl -o ${path}/fastdfs-nginx-module.zip http://dunwu.test.upcdn.net/soft/fdfs/fastdfs-nginx-module.zip curl -o ${path}/fastdfs-nginx-module.zip http://dunwu.test.upcdn.net/soft/fdfs/fastdfs-nginx-module.zip
if [[ ! -f ${path}/fastdfs-nginx-module.zip ]]; then if [[ ! -f ${path}/fastdfs-nginx-module.zip ]]; then
printf "${RED}>>>>>>>>> install fastdfs-nginx-module failedexit. ${RESET}\n" printf "${RED}>>>>>>>>> install fastdfs-nginx-module failedexit. ${RESET}\n"
fi fi
unzip -o ${path}/fastdfs-nginx-module.zip -d ${path} unzip -o ${path}/fastdfs-nginx-module.zip -d ${path}

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# version:v1.0 # version:v1.0
# func:grafana 5.1.0/5.1.5/5.2.2 安装 # func:grafana 5.1.0/5.1.5/5.2.2 安装
@ -25,23 +26,23 @@ echo "3: Install grafana 5.2.2"
echo "4: EXIT" echo "4: EXIT"
# 选择安装软件版本 # 选择安装软件版本
read -p "Please input your choice:" softversion read -p "Please input your choice:" softversion
if [ "${softversion}" == "1" ];then if [ "${softversion}" == "1" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm"
elif [ "${softversion}" == "2" ];then elif [ "${softversion}" == "2" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm"
elif [ "${softversion}" == "3" ];then elif [ "${softversion}" == "3" ]; then
URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm" URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm"
elif [ "${softversion}" == "4" ];then elif [ "${softversion}" == "4" ]; then
echo "you choce channel!" echo "you choce channel!"
exit 1; exit 1;
else else
echo "input Error! Place input{1|2|3|4}" echo "input Error! Place input{1|2|3|4}"
exit 0; exit 0;
fi fi
# 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开
output_msg() { output_msg() {
for msg in $*;do for msg in $*; do
action $msg /bin/true action $msg /bin/true
done done
} }
@ -49,21 +50,21 @@ output_msg() {
# 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称
check_yum_command() { check_yum_command() {
output_msg "命令检查:$1" output_msg "命令检查:$1"
hash $1 >/dev/null 2>&1 hash $1 > /dev/null 2>&1
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0
else else
yum -y install $2 >/dev/null 2>&1 yum -y install $2 > /dev/null 2>&1
# hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 } # hash $Command || { echo "`date +%F' '%H:%M:%S` $2 is installed fail">>${install_log_path}${install_log_name} ; exit 1 }
fi fi
} }
# 判断目录是否存在,传入目录绝对路径,可以传入多个目录 # 判断目录是否存在,传入目录绝对路径,可以传入多个目录
check_dir() { check_dir() {
output_msg "目录检查" output_msg "目录检查"
for dirname in $*;do for dirname in $*; do
[ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1
echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name}
done done
} }
@ -71,13 +72,13 @@ check_dir() {
# 下载文件并解压至安装目录,传入url链接地址 # 下载文件并解压至安装目录,传入url链接地址
download_file() { download_file() {
output_msg "下载源码包" output_msg "下载源码包"
mkdir -p $download_path mkdir -p $download_path
for file in $*;do for file in $*; do
wget $file -c -P $download_path &> /dev/null wget $file -c -P $download_path &> /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` $file download fail!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` $file download fail!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
done done
} }
@ -86,24 +87,24 @@ download_file() {
install_grafana_plugins() { install_grafana_plugins() {
output_msg "grafana插件安装" output_msg "grafana插件安装"
check_yum_command grafana-cli check_yum_command grafana-cli
grafana-cli plugins install $* >/dev/null grafana-cli plugins install $* > /dev/null
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!">>${install_log_path}${install_log_name} echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!" >> ${install_log_path}${install_log_name}
else else
echo "`date +%F' '%H:%M:%s` grafana plugins $* install success!">>${install_log_path}${install_log_name} && exit 1 echo "`date +%F' '%H:%M:%s` grafana plugins $* install success!" >> ${install_log_path}${install_log_name} && exit 1
fi fi
} }
main() { main() {
check_dir $install_log_path $install_path check_dir $install_log_path $install_path
check_yum_command wget wget check_yum_command wget wget
download_file $URL download_file $URL
for filename in `ls $download_path`;do for filename in `ls $download_path`; do
yum -y install $download_path$filename >/dev/null 2>&1 yum -y install $download_path$filename > /dev/null 2>&1
done done
install_grafana_plugins alexanderzobnin-zabbix-app install_grafana_plugins alexanderzobnin-zabbix-app
} }
main main

View File

@ -27,7 +27,8 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install jdk8 begin.${RESET}\n" printf "${GREEN}>>>>>>>> install jdk8 begin.${RESET}\n"
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1; }
yum -y install java-1.8.0-openjdk-devel.x86_64 yum -y install java-1.8.0-openjdk-devel.x86_64
java -version java -version

View File

@ -26,23 +26,24 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install kafka begin.${RESET}\n" printf "${GREEN}>>>>>>>> install kafka begin.${RESET}\n"
command -v java >/dev/null 2>&1 || { printf "${RED}Require java but it's not installed.${RESET}\n"; exit 1; } command -v java > /dev/null 2>&1 || { printf "${RED}Require java but it's not installed.${RESET}\n";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh kafka-install.sh [version] [path]\n" printf "\t sh kafka-install.sh [version] [path]\n"
printf "\t Example: sh kafka-install.sh 2.2.0 /opt/kafka\n" printf "\t Example: sh kafka-install.sh 2.2.0 /opt/kafka\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=2.2.0 version=2.2.0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/kafka path=/opt/kafka
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -28,23 +28,24 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install maven begin.${RESET}\n" printf "${GREEN}>>>>>>>> install maven begin.${RESET}\n"
command -v java >/dev/null 2>&1 || { printf "${RED}Require java but it's not installed.${RESET}\n"; exit 1; } command -v java > /dev/null 2>&1 || { printf "${RED}Require java but it's not installed.${RESET}\n";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh maven-install.sh [version] [path]\n" printf "\t sh maven-install.sh [version] [path]\n"
printf "\t Example: sh maven-install.sh 3.6.0 /opt/maven\n" printf "\t Example: sh maven-install.sh 3.6.0 /opt/maven\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=3.6.0 version=3.6.0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/maven path=/opt/maven
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -26,21 +26,21 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install mongodb begin.${RESET}\n" printf "${GREEN}>>>>>>>> install mongodb begin.${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh mongodb-install.sh [version] [path]\n" printf "\t sh mongodb-install.sh [version] [path]\n"
printf "\t Example: sh mongodb-install.sh 4.0.9 /opt/mongodb\n" printf "\t Example: sh mongodb-install.sh 4.0.9 /opt/mongodb\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=4.0.9 version=4.0.9
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/mongodb path=/opt/mongodb
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -26,9 +26,12 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install mysql begin.${RESET}\n" printf "${GREEN}>>>>>>>> install mysql begin.${RESET}\n"
command -v wget >/dev/null 2>&1 || { printf "${RED}Require wget but it's not installed.${RESET}\n"; exit 1; } command -v wget > /dev/null 2>&1 || { printf "${RED}Require wget but it's not installed.${RESET}\n";
command -v rpm >/dev/null 2>&1 || { printf "${RED}Require rpm but it's not installed.${RESET}\n"; exit 1; } exit 1; }
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v rpm > /dev/null 2>&1 || { printf "${RED}Require rpm but it's not installed.${RESET}\n";
exit 1; }
command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1; }
# 使用 rpm 安装 mysql # 使用 rpm 安装 mysql
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

View File

@ -11,22 +11,24 @@ cat << EOF
EOF EOF
command -v java >/dev/null 2>&1 || { echo >&2 "Require java but it's not installed."; exit 1; } command -v java > /dev/null 2>&1 || { echo >&2 "Require java but it's not installed.";
command -v mvn >/dev/null 2>&1 || { echo >&2 "Require mvn but it's not installed."; exit 1; } exit 1; }
command -v mvn > /dev/null 2>&1 || { echo >&2 "Require mvn but it's not installed.";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
echo "Usage: sh nacos-install.sh [version] [path]" echo "Usage: sh nacos-install.sh [version] [path]"
printf "Example: sh nacos-install.sh 1.0.0 /opt/nacos\n" printf "Example: sh nacos-install.sh 1.0.0 /opt/nacos\n"
fi fi
version=1.0.0 version=1.0.0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
root=/opt/nacos root=/opt/nacos
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
root=$2 root=$2
fi fi
echo "Current execution: install nacos ${version} to ${root}" echo "Current execution: install nacos ${version} to ${root}"

View File

@ -27,23 +27,24 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install maven begin.${RESET}\n" printf "${GREEN}>>>>>>>> install maven begin.${RESET}\n"
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh nginx-install.sh [version] [path]\n" printf "\t sh nginx-install.sh [version] [path]\n"
printf "\t Example: sh nginx-install.sh 1.16.0 /opt/nginx\n" printf "\t Example: sh nginx-install.sh 1.16.0 /opt/nginx\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=1.16.0 version=1.16.0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/nginx path=/opt/nginx
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -27,16 +27,16 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install nodejs begin.${RESET}\n" printf "${GREEN}>>>>>>>> install nodejs begin.${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh nodejs-install.sh [version]\n" printf "\t sh nodejs-install.sh [version]\n"
printf "\t Example: sh nodejs-install.sh 10.15.2\n" printf "\t Example: sh nodejs-install.sh 10.15.2\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=10.15.2 version=10.15.2
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
# install info # install info

View File

@ -24,31 +24,32 @@ cat << EOF
EOF EOF
printf "${RESET}" printf "${RESET}"
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
exit 1; }
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]] || [[ $# -lt 3 ]] || [[ $# -lt 4 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]] || [[ $# -lt 3 ]] || [[ $# -lt 4 ]]; then
echo "Usage: sh redis-install.sh [version] [path] [port] [password]" echo "Usage: sh redis-install.sh [version] [path] [port] [password]"
echo -e "Example: sh redis-install.sh 5.0.4 /opt/redis 6379 123456\n" echo -e "Example: sh redis-install.sh 5.0.4 /opt/redis 6379 123456\n"
fi fi
version=5.0.4 version=5.0.4
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
root=/opt/redis root=/opt/redis
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
root=$2 root=$2
fi fi
port=6379 port=6379
if [[ -n $3 ]]; then if [[ -n $3 ]]; then
port=$3 port=$3
fi fi
password= password=
if [[ -n $4 ]]; then if [[ -n $4 ]]; then
password=$4 password=$4
fi fi
printf "${GREEN}>>>>>>>> install redis begin.${RESET}\n" printf "${GREEN}>>>>>>>> install redis begin.${RESET}\n"
@ -72,7 +73,7 @@ mkdir -p /etc/redis
cp ${path}/redis.conf /etc/redis/${port}.conf cp ${path}/redis.conf /etc/redis/${port}.conf
sed -i "s/^port 6379/port ${port}/g" /etc/redis/${port}.conf sed -i "s/^port 6379/port ${port}/g" /etc/redis/${port}.conf
if [[ -n ${password} ]]; then if [[ -n ${password} ]]; then
sed -i "s/^# requirepass/requirepass ${password}/g" /etc/redis/${port}.conf sed -i "s/^# requirepass/requirepass ${password}/g" /etc/redis/${port}.conf
fi fi
printf "\n${CYAN}>>>>>>>>> add firewall port${RESET}\n" printf "\n${CYAN}>>>>>>>>> add firewall port${RESET}\n"

View File

@ -26,21 +26,21 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n" printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh rocketmq-install.sh [version] [path]\n" printf "\t sh rocketmq-install.sh [version] [path]\n"
printf "\t Example: sh rocketmq-install.sh 4.5.0 /opt/rocketmq\n" printf "\t Example: sh rocketmq-install.sh 4.5.0 /opt/rocketmq\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=4.5.0 version=4.5.0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/rocketmq path=/opt/rocketmq
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -26,21 +26,21 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n" printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh tomcat8-install.sh [version] [path]\n" printf "\t sh tomcat8-install.sh [version] [path]\n"
printf "\t Example: sh tomcat8-install.sh 8.5.28 /opt/tomcat8\n" printf "\t Example: sh tomcat8-install.sh 8.5.28 /opt/tomcat8\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=8.5.28 version=8.5.28
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/tomcat path=/opt/tomcat
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -26,21 +26,21 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install zookeeper begin.${RESET}\n" printf "${GREEN}>>>>>>>> install zookeeper begin.${RESET}\n"
if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
printf "${PURPLE}[Hint]\n" printf "${PURPLE}[Hint]\n"
printf "\t sh zookeeper-install.sh [version] [path]\n" printf "\t sh zookeeper-install.sh [version] [path]\n"
printf "\t Example: sh zookeeper-install.sh 3.4.12 /opt/zookeeper\n" printf "\t Example: sh zookeeper-install.sh 3.4.12 /opt/zookeeper\n"
printf "${RESET}\n" printf "${RESET}\n"
fi fi
version=3.4.12 version=3.4.12
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
version=$1 version=$1
fi fi
path=/opt/zookeeper path=/opt/zookeeper
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
path=$2 path=$2
fi fi
# install info # install info

View File

@ -27,8 +27,10 @@ printf "${RESET}"
printf "${GREEN}>>>>>>>> install zsh begin.${RESET}\n" printf "${GREEN}>>>>>>>> install zsh begin.${RESET}\n"
command -v yum >/dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n"; exit 1; } command -v yum > /dev/null 2>&1 || { printf "${RED}Require yum but it's not installed.${RESET}\n";
command -v git >/dev/null 2>&1 || { printf "${RED}Require git but it's not installed.${RESET}\n"; exit 1; } exit 1; }
command -v git > /dev/null 2>&1 || { printf "${RED}Require git but it's not installed.${RESET}\n";
exit 1; }
# install zsh # install zsh
yum install -y zsh yum install -y zsh

View File

@ -22,21 +22,21 @@ cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 根据发型版本选择相应 yum 镜像 # 根据发型版本选择相应 yum 镜像
if [[ ${version} == 5 ]]; then if [[ ${version} == 5 ]]; then
# Cento5 已废弃,只能使用 http://vault.CentOS.org/ 替换,但由于是国外镜像,速度较慢 # Cento5 已废弃,只能使用 http://vault.CentOS.org/ 替换,但由于是国外镜像,速度较慢
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-5.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-5.repo -O /etc/yum.repos.d/CentOS-Base.repo
# 根据实际发型版本情况替换 # 根据实际发型版本情况替换
detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'` detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'`
sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo
# 不替换下面的开关可能会出现错误Could not open/read repomd.xml # 不替换下面的开关可能会出现错误Could not open/read repomd.xml
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo
elif [[ ${version} == 6 ]]; then elif [[ ${version} == 6 ]]; then
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-6.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-6.repo -O /etc/yum.repos.d/CentOS-Base.repo
elif [[ ${version} == 7 ]]; then elif [[ ${version} == 7 ]]; then
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/yum/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
else else
echo -e "版本不支持,替换 yum repo 失败" echo -e "版本不支持,替换 yum repo 失败"
fi fi
# 更新缓存 # 更新缓存

View File

@ -7,22 +7,22 @@
################################################################################### ###################################################################################
ip='127.0.0.1' ip='127.0.0.1'
function getDeviceIp() { function getDeviceIp() {
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}') ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
} }
function setDNSServer() { function setDNSServer() {
echo -e "设置DNS服务器" echo -e "设置DNS服务器"
cat >> /etc/resolv.conf << EOF cat >> /etc/resolv.conf << EOF
nameserver 114.114.114.114 nameserver 114.114.114.114
nameserver 8.8.8.8 nameserver 8.8.8.8
EOF EOF
} }
function setHosts() { function setHosts() {
getDeviceIp getDeviceIp
host=`hostname` host=`hostname`
cat >> /etc/hosts << EOF cat >> /etc/hosts << EOF
${ip} ${host} ${ip} ${host}
EOF EOF
} }

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
printHeadInfo() { printHeadInfo() {
cat << EOF cat << EOF
################################################################################### ###################################################################################
# Linux Centos7 系统配置脚本(根据需要选择) # Linux Centos7 系统配置脚本(根据需要选择)
# @author: Zhang Peng # @author: Zhang Peng
@ -11,47 +11,47 @@ EOF
} }
setLimit() { setLimit() {
cat >> /etc/security/limits.conf << EOF cat >> /etc/security/limits.conf << EOF
* - nofile 65535 * - nofile 65535
* - nproc 65535 * - nproc 65535
EOF EOF
} }
setLang() { setLang() {
cat > /etc/sysconfig/i18n << EOF cat > /etc/sysconfig/i18n << EOF
LANG="zh_CN.UTF-8" LANG="zh_CN.UTF-8"
EOF EOF
} }
closeShutdownShortkey() { closeShutdownShortkey() {
echo "关闭 Ctrl+Alt+Del 快捷键防止重新启动" echo "关闭 Ctrl+Alt+Del 快捷键防止重新启动"
sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf
} }
closeSelinux() { closeSelinux() {
echo "关闭 selinux" echo "关闭 selinux"
# see http://blog.51cto.com/13570193/2093299 # see http://blog.51cto.com/13570193/2093299
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
} }
setBootMode() { setBootMode() {
# 1. 停机(记得不要把 initdefault 配置为 0因为这样会使 Linux 不能启动) # 1. 停机(记得不要把 initdefault 配置为 0因为这样会使 Linux 不能启动)
# 2. 单用户模式,就像 Win9X 下的安全模式 # 2. 单用户模式,就像 Win9X 下的安全模式
# 3. 多用户,但是没有 NFS # 3. 多用户,但是没有 NFS
# 4. 完全多用户模式,准则的运行级 # 4. 完全多用户模式,准则的运行级
# 5. 通常不用,在一些特殊情况下可以用它来做一些事情 # 5. 通常不用,在一些特殊情况下可以用它来做一些事情
# 6. X11即进到 X-Window 系统 # 6. X11即进到 X-Window 系统
# 7. 重新启动 (记得不要把 initdefault 配置为 6因为这样会使 Linux 不断地重新启动) # 7. 重新启动 (记得不要把 initdefault 配置为 6因为这样会使 Linux 不断地重新启动)
echo "设置 Linux 启动模式" echo "设置 Linux 启动模式"
sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab
} }
# 配置 IPv4 # 配置 IPv4
configIpv4(){ configIpv4() {
echo "配置 ipv4" echo "配置 ipv4"
cat >> /etc/sysctl.conf << EOF cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_fin_timeout = 2
@ -78,56 +78,57 @@ EOF
# 关闭 IPv6 # 关闭 IPv6
closeIpv6() { closeIpv6() {
echo "关闭 ipv6" echo "关闭 ipv6"
cat > /etc/modprobe.d/ipv6.conf << EOF cat > /etc/modprobe.d/ipv6.conf << EOF
alias net-pf-10 off alias net-pf-10 off
options ipv6 disable=1 options ipv6 disable=1
EOF EOF
echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
} }
# 入口函数 # 入口函数
main() { main() {
PS3="请选择要执行的操作:" PS3="请选择要执行的操作:"
select ITEM in "设置 DNS" "设置 NTP" "关闭防火墙" "配置 IPv4" "关闭 IPv6" "全部执行" select ITEM in "设置 DNS" "设置 NTP" "关闭防火墙" "配置 IPv4" "关闭 IPv6" "全部执行"
do do
case ${ITEM} in case ${ITEM} in
"设置 DNS") "设置 DNS")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash
;; ;;
"设置 NTP") "设置 NTP")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-ntp.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-ntp.sh | bash
;; ;;
"关闭防火墙") "关闭防火墙")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash
;; ;;
"配置 IPv4") "配置 IPv4")
configIpv4 configIpv4
;; ;;
"关闭 IPv6") "关闭 IPv6")
closeIpv6 closeIpv6
;; ;;
"全部执行") "全部执行")
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-ntp.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-ntp.sh | bash
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash
configIpv4 configIpv4
closeIpv6 closeIpv6
;; ;;
*) *)
echo -e "输入项不支持!" echo -e "输入项不支持!"
main main
;; ;;
esac esac
break break
done done
} }
######################################## MAIN ######################################## ######################################## MAIN ########################################
filepath=$(cd "$(dirname "$0")"; pwd) filepath=$(cd "$(dirname "$0")";
pwd)
printHeadInfo printHeadInfo
main main

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#mail:xuel@anchnet.com #mail:xuel@anchnet.com
#data:2017/9/7 #data:2017/9/7
#AutoInstall ELK scripts #AutoInstall ELK scripts
@ -9,65 +10,66 @@ echo "# Auto Install ELK. ##"
echo "# Press Ctrl + C to cancel ##" echo "# Press Ctrl + C to cancel ##"
echo "# Any key to continue ##" echo "# Any key to continue ##"
echo "##########################################" echo "##########################################"
read -p read -p
software_dir="/usr/local/software" software_dir="/usr/local/software"
elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz" elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz"
kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz" kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz"
logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz" logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz"
filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz" filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz"
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1` sys_version=`cat /etc/redhat-release | awk '{print $4}' | cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1` IP=`ip addr | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options" jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1` sys_mem=`free -m | grep Mem: | awk '{print $2}' | awk '{sum+=$1} END {print sum/1024}' | cut -d. -f1`
#wget software #wget software
wget_fun() { wget_fun() {
if [ ! -d ${software_dir} ];then if [ ! -d ${software_dir} ]; then
mkdir -p ${software_dir} && cd ${software_dir} mkdir -p ${software_dir} && cd ${software_dir}
else else
cd ${software_dir} cd ${software_dir}
fi fi
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
do do
wget -c $software wget -c $software
done done
clear clear
} }
#initial system:install java wget;set hostname;disable firewalld #initial system:install java wget;set hostname;disable firewalld
init_sys() { init_sys() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions [ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1 [ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1 [ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0 setenforce 0
yum install -y java-1.8.0-openjdk wget net-tools yum install -y java-1.8.0-openjdk wget net-tools
hostnamectl set-hostname elk-server hostnamectl set-hostname elk-server
systemctl stop firewalld systemctl stop firewalld
cat >>/etc/security/limits.conf<<EOF cat >> /etc/security/limits.conf << EOF
* soft nofile 65536 * soft nofile 65536
* hard nofile 65536 * hard nofile 65536
* soft nGproc 65536 * soft nGproc 65536
* hard nproc 65536 * hard nproc 65536
EOF EOF
} }
#install elasticsearch #install elasticsearch
install_elasticsearch() { install_elasticsearch() {
cd $software_dir cd $software_dir
tar zxf elasticsearch-5.4.1.tar.gz tar zxf elasticsearch-5.4.1.tar.gz
mv elasticsearch-5.4.1 /usr/local/elasticsearch mv elasticsearch-5.4.1 /usr/local/elasticsearch
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
useradd elasticsearch useradd elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p echo "vm.max_map_count = 655360" >> /etc/sysctl.conf && sysctl -p
if [ ${sys_mem} -eq 0 ];then if [ ${sys_mem} -eq 0 ]; then
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
else else
sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf} sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf} sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
fi fi
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF cat >> /usr/local/elasticsearch/config/elasticsearch.yml << EOF
cluster.name: my-application cluster.name: my-application
node.name: elk-server node.name: elk-server
path.data: /usr/local/elasticsearch/data path.data: /usr/local/elasticsearch/data
@ -76,15 +78,15 @@ network.host: 127.0.0.1
http.port: 9200 http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-server"] discovery.zen.ping.unicast.hosts: ["elk-server"]
EOF EOF
su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &" su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &"
} }
#install logstash #install logstash
install_logstash() { install_logstash() {
cd $software_dir cd $software_dir
tar -zxf logstash-5.4.1.tar.gz tar -zxf logstash-5.4.1.tar.gz
mv logstash-5.4.1 /usr/local/logstash mv logstash-5.4.1 /usr/local/logstash
cat>/usr/local/logstash/config/01-syslog.conf<<EOF cat > /usr/local/logstash/config/01-syslog.conf << EOF
input { input {
beats { beats {
port => "5044" port => "5044"
@ -97,15 +99,15 @@ output {
stdout { codec => rubydebug } stdout { codec => rubydebug }
} }
EOF EOF
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & > /dev/null
} }
#install filebeat #install filebeat
install_filebeat() { install_filebeat() {
cd $software_dir cd $software_dir
tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz
mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF cat > /usr/local/filebeat/filebeat.yml << EOF
filebeat.prospectors: filebeat.prospectors:
- input_type: log - input_type: log
paths: paths:
@ -113,52 +115,53 @@ filebeat.prospectors:
output.logstash: output.logstash:
hosts: ["127.0.0.1:5044"] hosts: ["127.0.0.1:5044"]
EOF EOF
cd /usr/local/filebeat/ cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null nohup /usr/local/filebeat/filebeat & > /dev/null
} }
#install kibana #install kibana
install_kibana() { install_kibana() {
cd $software_dir cd $software_dir
tar -zxf kibana-5.4.1-linux-x86_64.tar.gz tar -zxf kibana-5.4.1-linux-x86_64.tar.gz
mv kibana-5.4.1-linux-x86_64 /usr/local/kibana mv kibana-5.4.1-linux-x86_64 /usr/local/kibana
cat >> /usr/local/kibana/config/kibana.yml <<EOF cat >> /usr/local/kibana/config/kibana.yml << EOF
server.port: 5601 server.port: 5601
server.host: "0.0.0.0" server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200" elasticsearch.url: "http://127.0.0.1:9200"
EOF EOF
nohup /usr/local/kibana/bin/kibana & >/dev/null nohup /usr/local/kibana/bin/kibana & > /dev/null
} }
check() { check() {
port=$1 port=$1
program=$2 program=$2
check_port=`netstat -lntup|grep ${port}|wc -l` check_port=`netstat -lntup | grep ${port} | wc -l`
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l` check_program=`ps -ef | grep ${program} | grep -v grep | wc -l`
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then if [ $check_port -gt 0 ] && [ $check_program -gt 0 ]; then
action "${program} run is ok!" /bin/true action "${program} run is ok!" /bin/true
else else
action "${program} run is error!" /bin/false action "${program} run is error!" /bin/false
fi fi
} }
main() { main() {
init_sys init_sys
wget_fun wget_fun
install_elasticsearch install_elasticsearch
install_filebeat install_filebeat
install_logstash install_logstash
install_kibana install_kibana
echo -e "\033[32m Checking Elasticsearch...\033[0m" echo -e "\033[32m Checking Elasticsearch...\033[0m"
sleep 20 sleep 20
check :9200 "elasticsearch" check :9200 "elasticsearch"
echo -e "\033[32m Checking Logstash...\033[0m" echo -e "\033[32m Checking Logstash...\033[0m"
sleep 2 sleep 2
check ":9600" "logstash" check ":9600" "logstash"
echo -e "\033[32m Checking Kibana...\033[0m" echo -e "\033[32m Checking Kibana...\033[0m"
sleep 2 sleep 2
check ":5601" "kibana" check ":5601" "kibana"
action "ELK install is success!" /bin/true action "ELK install is success!" /bin/true
echo "url:http://$IP:5601" echo "url:http://$IP:5601"
} }
main main

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# auth:kaliarch # auth:kaliarch
# func:sys info check # func:sys info check
# version:v1.0 # version:v1.0
@ -16,38 +17,38 @@ LIMIT_CPU=85
LOG_DIR=/var/log/cpulimit/ LOG_DIR=/var/log/cpulimit/
# 超过阀值进程pid # 超过阀值进程pid
PIDARG=$(ps -aux |awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}') PIDARG=$(ps -aux | awk -v CPU=${PEC_CPU} '{if($3 > CPU) print $2}')
CPULIMITCMD=$(which cpulimit) CPULIMITCMD=$(which cpulimit)
install_cpulimit() { install_cpulimit() {
[ ! -d /tmp ] && mkdir /tmp || cd /tmp [ ! -d /tmp ] && mkdir /tmp || cd /tmp
wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz
tar -zxf v0.2.tar.gz tar -zxf v0.2.tar.gz
cd cpulimit-0.2 && make cd cpulimit-0.2 && make
[ $? -eq 0 ] && cp src/cpulimit /usr/bin/ [ $? -eq 0 ] && cp src/cpulimit /usr/bin/
} }
do_cpulimit() { do_cpulimit() {
[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR} [ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR}
for i in ${PIDARG}; for i in ${PIDARG};
do do
MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}') MSG=$(ps -aux | awk -v pid=$i '{if($2 == pid) print $0}')
echo ${MSG} echo ${MSG}
[ ! -d /tmp ] && mkdir /tmp || cd /tmp [ ! -d /tmp ] && mkdir /tmp || cd /tmp
nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} & nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} &
echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log
done done
} }
main() { main() {
hash cpulimit hash cpulimit
if [ $? -eq 0 ];then if [ $? -eq 0 ]; then
do_cpulimit do_cpulimit
else else
install_cpulimit && do_cpulimit install_cpulimit && do_cpulimit
fi fi
} }
main main

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# function:自定义rm命令每天晚上定时清理 # function:自定义rm命令每天晚上定时清理
CMD_SCRIPTS=$HOME/.rm_scripts.sh CMD_SCRIPTS=$HOME/.rm_scripts.sh
@ -7,7 +8,7 @@ CRON_FILE=/var/spool/cron/root
BASHRC=$HOME/.bashrc BASHRC=$HOME/.bashrc
[ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR} [ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR}
cat > $CMD_SCRIPTS <<EOF cat > $CMD_SCRIPTS << EOF
PARA_CNT=\$# PARA_CNT=\$#
TRASH_DIR=$TRASH_DIR TRASH_DIR=$TRASH_DIR
for i in \$*; do for i in \$*; do

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# #
# Daily_Archive - Archive designated files & directories # Daily_Archive - Archive designated files & directories
###################################################### ######################################################
@ -12,7 +13,7 @@ DATE=`date +%y%m%d`
FILE=archive$DATE.tar.gz FILE=archive$DATE.tar.gz
# #
# Set Configuration and Destination File # Set Configuration and Destination File
# #
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
DESTINATION=/home/tiandi/archive/$FILE DESTINATION=/home/tiandi/archive/$FILE
# #
@ -20,46 +21,46 @@ DESTINATION=/home/tiandi/archive/$FILE
# #
# Check Backup Config file exists # Check Backup Config file exists
# #
if [ -f $CONFIG_FILE ] #Make sure the config file still exists if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then then
echo echo
else else
echo echo
echo "$CONFIG_FILE does not exist." echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file" echo "Backup not completed due to missing Configuration file"
echo echo
exit exit
fi fi
# #
# Build the names of all the files to backup # Build the names of all the files to backup
# #
FILE_NO=1 # Start on Line 1 of Config file. FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File exec < $CONFIG_FILE # Redirect Std Input to name of Config File
# #
read FILE_NAME # Read 1st record read FILE_NAME # Read 1st record
# #
while [ $? -eq 0 ] while [ $? -eq 0 ]
do do
# Make sure the file or directory exists. # Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ] if [ -f $FILE_NAME -o -d $FILE_NAME ]
then then
# If file exists, add its name to the lists # If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME" FILE_LIST="$FILE_LIST $FILE_NAME"
else else
# If file doesn't exist, issue warning # If file doesn't exist, issue warning
echo echo
echo "$FILE_NAME, does not exist." echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive." echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file." echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file." echo "Continuing to build archive file."
echo echo
fi fi
# #
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record. read FILE_NAME # Read next record.
done done
########################################################### ###########################################################
# #
# Backup the files and Compress Archive # Backup the files and Compress Archive
# #
tar -czf $DESTINATION $FILE_LIST 2> /dev/null tar -czf $DESTINATION $FILE_LIST 2> /dev/null

View File

@ -1,10 +1,11 @@
#!/bin/bash #!/bin/bash
# #
# Hourly_Archive - Every hour create an archive # Hourly_Archive - Every hour create an archive
###################################################### ######################################################
# #
# Set Configuration and Destination File # Set Configuration and Destination File
# #
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
# #
# Gather Current Date,Month & Time # Gather Current Date,Month & Time
@ -27,46 +28,46 @@ DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz
# #
# Check Backup Config file exists # Check Backup Config file exists
# #
if [ -f $CONFIG_FILE ] #Make sure the config file still exists if [ -f $CONFIG_FILE ] #Make sure the config file still exists
then then
echo echo
else else
echo echo
echo "$CONFIG_FILE does not exist." echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file" echo "Backup not completed due to missing Configuration file"
echo echo
exit exit
fi fi
# #
# Build the names of all the files to backup # Build the names of all the files to backup
# #
FILE_NO=1 # Start on Line 1 of Config file. FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File exec < $CONFIG_FILE # Redirect Std Input to name of Config File
# #
read FILE_NAME # Read 1st record read FILE_NAME # Read 1st record
# #
while [ $? -eq 0 ] while [ $? -eq 0 ]
do do
# Make sure the file or directory exists. # Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ] if [ -f $FILE_NAME -o -d $FILE_NAME ]
then then
# If file exists, add its name to the lists # If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME" FILE_LIST="$FILE_LIST $FILE_NAME"
else else
# If file doesn't exist, issue warning # If file doesn't exist, issue warning
echo echo
echo "$FILE_NAME, does not exist." echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive." echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file." echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file." echo "Continuing to build archive file."
echo echo
fi fi
# #
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record. read FILE_NAME # Read next record.
done done
########################################################### ###########################################################
# #
# Backup the files and Compress Archive # Backup the files and Compress Archive
# #
tar -czf $DESTINATION $FILE_LIST 2> /dev/null tar -czf $DESTINATION $FILE_LIST 2> /dev/null

View File

@ -1,85 +1,92 @@
#!/bin/bash #!/bin/bash
# #
# Delete_User - Automates the 4 steps to remove an account # Delete_User - Automates the 4 steps to remove an account
# #
################################################################# #################################################################
# #
# Define Functions # Define Functions
# #
################################################################# #################################################################
function get_answer { function get_answer {
# #
unset ANSWER unset ANSWER
ASK_COUNT=0 ASK_COUNT=0
# #
while [ -z "$ANSWER" ] # while no answer is given, keep asking while [ -z "$ANSWER" ] # while no answer is given, keep asking
do do
ASK_COUNT=$[ $ASK_COUNT + 1 ] ASK_COUNT=$[ $ASK_COUNT + 1 ]
# #
case $ASK_COUNT in # If user gives no answer in time allowed case $ASK_COUNT in
2) # If user gives no answer in time allowed
echo 2)
echo "Please answer the question." echo
echo echo "Please answer the question."
;; echo
3) ;;
echo 3)
echo "One last try... please answer the question." echo
echo echo "One last try... please answer the question."
;; echo
4) ;;
echo 4)
echo "Since you refuse to answer the question..." echo
echo "exiting program." echo "Since you refuse to answer the question..."
echo echo "exiting program."
# echo
exit #
;; exit
esac ;;
# esac
echo #
# echo
if [ -n "$LINE2" ] #
then if [ -n "$LINE2" ]
echo $LINE1 # Print 2 lines then
echo -e $LINE2" \c" echo $LINE1 # Print 2 lines
else # Print 1 line echo -e $LINE2" \c"
echo -e $LINE1" \c" else
fi # Print 1 line
# echo -e $LINE1" \c"
# Allow 60 seconds to answer before time-out fi
read -t 60 ANSWER #
done # Allow 60 seconds to answer before time-out
# read -t 60 ANSWER
# Do a little variable clean-up done
# #
unset LINE1 # Do a little variable clean-up
unset LINE2 #
# unset LINE1
} #end of get_answer function unset LINE2
#
}
#end of get_answer function
# #
################################################################# #################################################################
function process_answer { function process_answer {
# #
case $ANSWER in case $ANSWER in
y|Y|YES|yes|yEs|yeS|YEs|yES) y | Y | YES | yes | yEs | yeS | YEs | yES)
# If user answers "yes".do nothing. # If user answers "yes".do nothing.
;; ;;
*) *)
# If user answers anything but "yes", exit script # If user answers anything but "yes", exit script
echo echo
echo $EXIT_LINE1 echo $EXIT_LINE1
echo $EXIT_LINE2 echo $EXIT_LINE2
echo echo
exit exit
;; ;;
esac esac
# #
# Do a little variable clean-up # Do a little variable clean-up
unset EXIT_LINE1 unset EXIT_LINE1
unset EXIT_LINE2 unset EXIT_LINE2
# #
} #End of process_answer function }
#End of process_answer function
# #
################################################################ ################################################################
# #
@ -108,13 +115,13 @@ get_answer
# #
USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT) USER_ACCOUNT_RECORD=$(cat /etc/passwd | grep -w $USER_ACCOUNT)
# #
if [ $? -eq 1 ] # If the account is not found, exit script if [ $? -eq 1 ] # If the account is not found, exit script
then then
echo echo
echo "Account, $USER_ACCOUNT, not found." echo "Account, $USER_ACCOUNT, not found."
echo "Leaving the script..." echo "Leaving the script..."
echo echo
exit exit
fi fi
# #
echo echo
@ -143,59 +150,59 @@ echo
echo "$USER_ACCOUNT has the following processes running: " echo "$USER_ACCOUNT has the following processes running: "
echo echo
# #
ps -u $USER_ACCOUNT #List the processes running ps -u $USER_ACCOUNT #List the processes running
# #
case $? in case $? in
1) # No processes running for this User Account 1) # No processes running for this User Account
# #
echo "There are no processes for this account currently running." echo "There are no processes for this account currently running."
echo echo
;; ;;
0) # Processes running for this User Account. 0) # Processes running for this User Account.
# Ask Script User if wants us to kill the processes. # Ask Script User if wants us to kill the processes.
# #
unset ANSWER # I think this line is not needed unset ANSWER # I think this line is not needed
LINE1="Would you like me to kill the process(es)? [y/n]:" LINE1="Would you like me to kill the process(es)? [y/n]:"
get_answer get_answer
# #
case $ANSWER in case $ANSWER in
y|Y|YES|yes|Yes|yEs|yeS|YEs|yES) # if user answer "yes", y | Y | YES | yes | Yes | yEs | yeS | YEs | yES) # if user answer "yes",
#kill User Account processes #kill User Account processes
# #
echo echo
# #
# Clean-up temp file upon signals # Clean-up temp file upon signals
# #
trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT trap "rm$USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT
# #
# List user processes running # List user processes running
ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt
# #
exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input
# #
read USER_PROCESS_REC # First record will be blank read USER_PROCESS_REC # First record will be blank
read USER_PROCESS_REC read USER_PROCESS_REC
# #
while [ $? -eq 0 ] while [ $? -eq 0 ]
do do
# obtain PID # obtain PID
USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1 ) USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1)
kill -9 $USER_PID kill -9 $USER_PID
echo "Killed process $USER_PID" echo "Killed process $USER_PID"
read USER_PROCESS_REC read USER_PROCESS_REC
done done
# #
echo echo
# #
rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report
;; ;;
*) # If user answers anything but "yes", do not kill. *) # If user answers anything but "yes", do not kill.
echo echo
echo "Will not kill the process(es)." echo "Will not kill the process(es)."
echo echo
;; ;;
esac esac
;; ;;
esac esac
################################################################################### ###################################################################################
# #
@ -216,7 +223,7 @@ echo "Please wait. This may take a while..."
REPORT_DATE=`date +%y%m%d` REPORT_DATE=`date +%y%m%d`
REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt" REPORT_FILE=$USER_ACCOUNT"_Files_"$REPORT_DATE".rpt"
# #
find / -user $USER_ACCOUNT > $REPORT_FILE 2>/dev/null find / -user $USER_ACCOUNT > $REPORT_FILE 2> /dev/null
# #
echo echo
echo "Report is complete." echo "Report is complete."
@ -240,7 +247,7 @@ EXIT_LINE1="Since you do not wish to remove the user account."
EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..." EXIT_LINE2="$USER_ACCOUNT at this time, exiting the script..."
process_answer process_answer
# #
userdel $USER_ACCOUNT # delete user account userdel $USER_ACCOUNT # delete user account
echo echo
echo "User account, $USER_ACCOUNT, has been removed" echo "User account, $USER_ACCOUNT, has been removed"
echo echo

View File

@ -9,11 +9,11 @@ echo "Top Ten Disk Space Usage"
echo "for ${DIRS} Directories" echo "for ${DIRS} Directories"
for DIR in ${DIRS} for DIR in ${DIRS}
do do
echo "" echo ""
echo "The ${DIR} Directory:" echo "The ${DIR} Directory:"
du -S ${DIR} 2>/dev/null | du -S ${DIR} 2> /dev/null |
sort -rn | sort -rn |
sed '{11,$D; =}' | sed '{11,$D; =}' |
sed 'N; s/\n/ /' | sed 'N; s/\n/ /' |
gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
done done

View File

@ -7,19 +7,19 @@
################### 读取脚本输入参数并校验 ################### ################### 读取脚本输入参数并校验 ###################
declare -a serial declare -a serial
serial=(start stop restart) serial=( start stop restart )
echo -n "请选择操作可选值start|stop|restart" echo -n "请选择操作可选值start|stop|restart"
read oper read oper
if ! echo ${serial[@]} | grep -q ${oper}; then if ! echo ${serial[@]} | grep -q ${oper}; then
echo "请选择正确操作可选值start|stop|restart" echo "请选择正确操作可选值start|stop|restart"
exit 1 exit 1
fi fi
declare -a serial2 declare -a serial2
serial2=(dev test prod) serial2=( dev test prod )
echo -n "请选择运行环境可选值dev|test|prod" echo -n "请选择运行环境可选值dev|test|prod"
read profile read profile
if ! echo ${serial2[@]} | grep -q ${profile}; then if ! echo ${serial2[@]} | grep -q ${profile}; then
echo "请选择正确运行环境可选值dev|test|prod" echo "请选择正确运行环境可选值dev|test|prod"
exit 1 exit 1
fi fi

View File

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

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 创建数组 # 创建数组
nums=([2]=2 [0]=0 [1]=1) nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 )
colors=(red yellow "dark blue") colors=( red yellow "dark blue" )
# 访问数组的单个元素 # 访问数组的单个元素
echo ${nums[1]} echo ${nums[1]}
@ -43,13 +43,13 @@ echo ${#nums[*]}
# 3 # 3
# 向数组中添加元素 # 向数组中添加元素
colors=(white "${colors[@]}" green black) colors=( white "${colors[@]}" green black )
echo ${colors[@]} echo ${colors[@]}
# Output: # Output:
# white red yellow dark blue green black # white red yellow dark blue green black
# 从数组中删除元素 # 从数组中删除元素
unset nums[0] unset nums[ 0 ]
echo ${nums[@]} echo ${nums[@]}
# Output: # Output:
# 1 2 # 1 2

View File

@ -9,7 +9,7 @@
########## 这是分割线 ########## ########## 这是分割线 ##########
:<<EOF : << EOF
echo '这是多行注释' echo '这是多行注释'
echo '这是多行注释' echo '这是多行注释'
echo '这是多行注释' echo '这是多行注释'

View File

@ -2,8 +2,8 @@
# 开启 debug # 开启 debug
set -x set -x
for (( i = 0; i < 3; i++ )); do for (( i = 0; i < 3; i ++ )); do
printf ${i} printf ${i}
done done
# 关闭 debug # 关闭 debug
set +x set +x
@ -21,6 +21,8 @@ set +x
# + (( i < 3 )) # + (( i < 3 ))
# + set +x # + set +x
for i in {1..5}; do printf ${i}; done for i in {1..5}; do
printf ${i};
done
printf "\n" printf "\n"
# Output: 12345 # Output: 12345

View File

@ -1,33 +1,34 @@
#!/usr/bin/env bash #!/usr/bin/env bash
calc(){ calc() {
PS3="choose the oper: " PS3="choose the oper: "
select oper in + - \* / # 生成操作符选择菜单 select oper in + - \* / # 生成操作符选择菜单
do do
echo -n "enter first num: " && read x # 读取输入参数 echo -n "enter first num: " && read x # 读取输入参数
echo -n "enter second num: " && read y # 读取输入参数 echo -n "enter second num: " && read y # 读取输入参数
exec exec
case ${oper} in case ${oper} in
"+") "+")
return $((${x} + ${y})) return $((${x} + ${y}))
;; ;;
"-") "-")
return $((${x} - ${y})) return $((${x} - ${y}))
;; ;;
"*") "*")
return $((${x} * ${y})) return $((${x} * ${y}))
;; ;;
"/") "/")
return $((${x} / ${y})) return $((${x} / ${y}))
;; ;;
*) *)
echo "${oper} is not support!" echo "${oper} is not support!"
return 0 return 0
;; ;;
esac esac
break break
done done
} }
calc calc
echo "the result is: $?" # $? 获取 calc 函数返回值 echo "the result is: $?" # $? 获取 calc 函数返回值
# $ ./function-demo.sh # $ ./function-demo.sh

View File

@ -2,22 +2,23 @@
x=0 x=0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
echo "第一个参数为:$1" echo "第一个参数为:$1"
x=$1 x=$1
else else
echo "第一个参数为空" echo "第一个参数为空"
fi fi
y=0 y=0
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
echo "第二个参数为:$2" echo "第二个参数为:$2"
y=$2 y=$2
else else
echo "第二个参数为空" echo "第二个参数为空"
fi fi
paramsFunction(){ paramsFunction() {
echo "函数第一个入参:$1" echo "函数第一个入参:$1"
echo "函数第二个入参:$2" echo "函数第二个入参:$2"
} }
paramsFunction ${x} ${y} paramsFunction ${x} ${y}

View File

@ -1,24 +1,25 @@
#!/usr/bin/env bash #!/usr/bin/env bash
runner() { runner() {
return 0 return 0
} }
name=zp name=zp
paramsFunction(){ paramsFunction() {
echo "函数第一个入参:$1" echo "函数第一个入参:$1"
echo "函数第二个入参:$2" echo "函数第二个入参:$2"
echo "传递到脚本的参数个数:$#" echo "传递到脚本的参数个数:$#"
echo "所有参数:" echo "所有参数:"
printf "+ %s\n" "$*" printf "+ %s\n" "$*"
echo "脚本运行的当前进程 ID 号:$$" echo "脚本运行的当前进程 ID 号:$$"
echo "后台运行的最后一个进程的 ID 号:$!" echo "后台运行的最后一个进程的 ID 号:$!"
echo "所有参数:" echo "所有参数:"
printf "+ %s\n" "$@" printf "+ %s\n" "$@"
echo "Shell 使用的当前选项:$-" echo "Shell 使用的当前选项:$-"
runner runner
echo "runner 函数的返回值:$?" echo "runner 函数的返回值:$?"
} }
paramsFunction 1 "abc" "hello, \"zp\"" paramsFunction 1 "abc" "hello, \"zp\""
# Output: # Output:
# 函数第一个入参1 # 函数第一个入参1

View File

@ -1,2 +1,3 @@
#!/bin/bash #!/bin/bash
echo "Hello, world!" echo "Hello, world!"

View File

@ -2,12 +2,12 @@
x=10 x=10
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y=20 y=20
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
echo "x=${x}, y=${y}" echo "x=${x}, y=${y}"
@ -28,10 +28,10 @@ val=`expr ${y} % ${x}`
echo "${y} % ${x} = $val" echo "${y} % ${x} = $val"
if [[ ${x} == ${y} ]]; then if [[ ${x} == ${y} ]]; then
echo "${x} = ${y}" echo "${x} = ${y}"
fi fi
if [[ ${x} != ${y} ]]; then if [[ ${x} != ${y} ]]; then
echo "${x} != ${y}" echo "${x} != ${y}"
fi fi
# Execute: ./operator-demo.sh # Execute: ./operator-demo.sh

View File

@ -2,50 +2,50 @@
x=10 x=10
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y=20 y=20
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
echo "x=${x}, y=${y}" echo "x=${x}, y=${y}"
if [[ ${x} -eq ${y} ]]; then if [[ ${x} -eq ${y} ]]; then
echo "${x} -eq ${y} : x 等于 y" echo "${x} -eq ${y} : x 等于 y"
else else
echo "${x} -eq ${y}: x 不等于 y" echo "${x} -eq ${y}: x 不等于 y"
fi fi
if [[ ${x} -ne ${y} ]]; then if [[ ${x} -ne ${y} ]]; then
echo "${x} -ne ${y}: x 不等于 y" echo "${x} -ne ${y}: x 不等于 y"
else else
echo "${x} -ne ${y}: x 等于 y" echo "${x} -ne ${y}: x 等于 y"
fi fi
if [[ ${x} -gt ${y} ]]; then if [[ ${x} -gt ${y} ]]; then
echo "${x} -gt ${y}: x 大于 y" echo "${x} -gt ${y}: x 大于 y"
else else
echo "${x} -gt ${y}: x 不大于 y" echo "${x} -gt ${y}: x 不大于 y"
fi fi
if [[ ${x} -lt ${y} ]]; then if [[ ${x} -lt ${y} ]]; then
echo "${x} -lt ${y}: x 小于 y" echo "${x} -lt ${y}: x 小于 y"
else else
echo "${x} -lt ${y}: x 不小于 y" echo "${x} -lt ${y}: x 不小于 y"
fi fi
if [[ ${x} -ge ${y} ]]; then if [[ ${x} -ge ${y} ]]; then
echo "${x} -ge ${y}: x 大于或等于 y" echo "${x} -ge ${y}: x 大于或等于 y"
else else
echo "${x} -ge ${y}: x 小于 y" echo "${x} -ge ${y}: x 小于 y"
fi fi
if [[ ${x} -le ${y} ]]; then if [[ ${x} -le ${y} ]]; then
echo "${x} -le ${y}: x 小于或等于 y" echo "${x} -le ${y}: x 小于或等于 y"
else else
echo "${x} -le ${y}: x 大于 y" echo "${x} -le ${y}: x 大于 y"
fi fi
# Execute: ./operator-demo2.sh # Execute: ./operator-demo2.sh

View File

@ -2,38 +2,38 @@
x=10 x=10
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y=20 y=20
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
echo "x=${x}, y=${y}" echo "x=${x}, y=${y}"
if [[ ${x} != ${y} ]]; then if [[ ${x} != ${y} ]]; then
echo "${x} != ${y} : x 不等于 y" echo "${x} != ${y} : x 不等于 y"
else else
echo "${x} != ${y}: x 等于 y" echo "${x} != ${y}: x 等于 y"
fi fi
if [[ ${x} -lt 100 && ${y} -gt 15 ]]; then if [[ ${x} -lt 100 && ${y} -gt 15 ]]; then
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 true" echo "${x} 小于 100 且 ${y} 大于 15 : 返回 true"
else else
echo "${x} 小于 100 且 ${y} 大于 15 : 返回 false" echo "${x} 小于 100 且 ${y} 大于 15 : 返回 false"
fi fi
if [[ ${x} -lt 100 || ${y} -gt 100 ]]; then if [[ ${x} -lt 100 || ${y} -gt 100 ]]; then
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 true" echo "${x} 小于 100 或 ${y} 大于 100 : 返回 true"
else else
echo "${x} 小于 100 或 ${y} 大于 100 : 返回 false" echo "${x} 小于 100 或 ${y} 大于 100 : 返回 false"
fi fi
if [[ ${x} -lt 5 || ${y} -gt 100 ]]; then if [[ ${x} -lt 5 || ${y} -gt 100 ]]; then
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true" echo "${x} 小于 5 或 ${y} 大于 100 : 返回 true"
else else
echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false" echo "${x} 小于 5 或 ${y} 大于 100 : 返回 false"
fi fi
# Execute: ./operator-demo3.sh # Execute: ./operator-demo3.sh

View File

@ -2,28 +2,28 @@
x=10 x=10
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y=20 y=20
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
echo "x=${x}, y=${y}" echo "x=${x}, y=${y}"
if [[ ${x} -lt 100 && ${y} -gt 100 ]] if [[ ${x} -lt 100 && ${y} -gt 100 ]]
then then
echo "${x} -lt 100 && ${y} -gt 100 返回 true" echo "${x} -lt 100 && ${y} -gt 100 返回 true"
else else
echo "${x} -lt 100 && ${y} -gt 100 返回 false" echo "${x} -lt 100 && ${y} -gt 100 返回 false"
fi fi
if [[ ${x} -lt 100 || ${y} -gt 100 ]] if [[ ${x} -lt 100 || ${y} -gt 100 ]]
then then
echo "${x} -lt 100 || ${y} -gt 100 返回 true" echo "${x} -lt 100 || ${y} -gt 100 返回 true"
else else
echo "${x} -lt 100 || ${y} -gt 100 返回 false" echo "${x} -lt 100 || ${y} -gt 100 返回 false"
fi fi
# Execute: ./operator-demo4.sh # Execute: ./operator-demo4.sh

View File

@ -2,44 +2,44 @@
x="abc" x="abc"
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y="xyz" y="xyz"
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
echo "x=${x}, y=${y}" echo "x=${x}, y=${y}"
if [[ ${x} = ${y} ]]; then if [[ ${x} = ${y} ]]; then
echo "${x} = ${y} : x 等于 y" echo "${x} = ${y} : x 等于 y"
else else
echo "${x} = ${y}: x 不等于 y" echo "${x} = ${y}: x 不等于 y"
fi fi
if [[ ${x} != ${y} ]]; then if [[ ${x} != ${y} ]]; then
echo "${x} != ${y} : x 不等于 y" echo "${x} != ${y} : x 不等于 y"
else else
echo "${x} != ${y}: x 等于 y" echo "${x} != ${y}: x 等于 y"
fi fi
if [[ -z ${x} ]]; then if [[ -z ${x} ]]; then
echo "-z ${x} : 字符串长度为 0" echo "-z ${x} : 字符串长度为 0"
else else
echo "-z ${x} : 字符串长度不为 0" echo "-z ${x} : 字符串长度不为 0"
fi fi
if [[ -n "${x}" ]]; then if [[ -n "${x}" ]]; then
echo "-n ${x} : 字符串长度不为 0" echo "-n ${x} : 字符串长度不为 0"
else else
echo "-n ${x} : 字符串长度为 0" echo "-n ${x} : 字符串长度为 0"
fi fi
if [[ ${x} ]]; then if [[ ${x} ]]; then
echo "${x} : 字符串不为空" echo "${x} : 字符串不为空"
else else
echo "${x} : 字符串为空" echo "${x} : 字符串为空"
fi fi
# Execute: ./operator-demo5.sh # Execute: ./operator-demo5.sh

View File

@ -3,39 +3,39 @@
file="/etc/hosts" file="/etc/hosts"
if [[ -r ${file} ]]; then if [[ -r ${file} ]]; then
echo "${file} 文件可读" echo "${file} 文件可读"
else else
echo "${file} 文件不可读" echo "${file} 文件不可读"
fi fi
if [[ -w ${file} ]]; then if [[ -w ${file} ]]; then
echo "${file} 文件可写" echo "${file} 文件可写"
else else
echo "${file} 文件不可写" echo "${file} 文件不可写"
fi fi
if [[ -x ${file} ]]; then if [[ -x ${file} ]]; then
echo "${file} 文件可执行" echo "${file} 文件可执行"
else else
echo "${file} 文件不可执行" echo "${file} 文件不可执行"
fi fi
if [[ -f ${file} ]]; then if [[ -f ${file} ]]; then
echo "${file} 文件为普通文件" echo "${file} 文件为普通文件"
else else
echo "${file} 文件为特殊文件" echo "${file} 文件为特殊文件"
fi fi
if [[ -d ${file} ]]; then if [[ -d ${file} ]]; then
echo "${file} 文件是个目录" echo "${file} 文件是个目录"
else else
echo "${file} 文件不是个目录" echo "${file} 文件不是个目录"
fi fi
if [[ -s ${file} ]]; then if [[ -s ${file} ]]; then
echo "${file} 文件不为空" echo "${file} 文件不为空"
else else
echo "${file} 文件为空" echo "${file} 文件为空"
fi fi
if [[ -e ${file} ]]; then if [[ -e ${file} ]]; then
echo "${file} 文件存在" echo "${file} 文件存在"
else else
echo "${file} 文件不存在" echo "${file} 文件不存在"
fi fi
# Execute: ./operator-demo6.sh # Execute: ./operator-demo6.sh

View File

@ -3,10 +3,10 @@
# 查找 10 以内第一个能整除 2 和 3 的正整数 # 查找 10 以内第一个能整除 2 和 3 的正整数
i=1 i=1
while [[ ${i} -lt 10 ]]; do while [[ ${i} -lt 10 ]]; do
if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then
echo ${i} echo ${i}
break; break;
fi fi
i=`expr ${i} + 1` i=`expr ${i} + 1`
done done
# Output: 6 # Output: 6

View File

@ -4,38 +4,38 @@ echo "input param: " $1 $2 $3
x=0 x=0
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
x=$1 x=$1
fi fi
y=0 y=0
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
y=$2 y=$2
fi fi
oper="" oper=""
if [[ -n $3 ]]; then if [[ -n $3 ]]; then
oper=$3 oper=$3
fi fi
exec exec
case ${oper} in case ${oper} in
"+") "+")
val=`expr ${x} + ${y}` val=`expr ${x} + ${y}`
echo "${x} + ${y} = ${val}" echo "${x} + ${y} = ${val}"
;; ;;
"-") "-")
val=`expr ${x} - ${y}` val=`expr ${x} - ${y}`
echo "${x} - ${y} = ${val}" echo "${x} - ${y} = ${val}"
;; ;;
"*") "*")
val=`expr ${x} \* ${y}` val=`expr ${x} \* ${y}`
echo "${x} * ${y} = ${val}" echo "${x} * ${y} = ${val}"
;; ;;
"/") "/")
val=`expr ${x} / ${y}` val=`expr ${x} / ${y}`
echo "${x} / ${y} = ${val}" echo "${x} / ${y} = ${val}"
;; ;;
*) *)
echo "Unknown oper!" echo "Unknown oper!"
;; ;;
esac esac

View File

@ -2,10 +2,10 @@
# 打印10以内的奇数 # 打印10以内的奇数
for (( i = 0; i < 10; i ++ )); do for (( i = 0; i < 10; i ++ )); do
if [[ $((i % 2)) -eq 0 ]]; then if [[ $((i % 2)) -eq 0 ]]; then
continue; continue;
fi fi
echo ${i} echo ${i}
done done
# Output: # Output:
# 1 # 1

View File

@ -3,7 +3,7 @@
################### for 语句 ################### ################### for 语句 ###################
echo "print 0 to 9" echo "print 0 to 9"
for (( j = 0; j < 10; j ++ )); do for (( j = 0; j < 10; j ++ )); do
echo ${j} echo ${j}
done done
# Output: # Output:
# print 0 to 9 # print 0 to 9
@ -20,7 +20,9 @@ done
################### for in 语句 ################### ################### for in 语句 ###################
echo "print 1 to 5" echo "print 1 to 5"
for i in {1..5}; do echo ${i}; done for i in {1..5}; do
echo ${i};
done
# Output: # Output:
# print 1 to 5 # print 1 to 5
# 1 # 1
@ -32,6 +34,6 @@ for i in {1..5}; do echo ${i}; done
################### for in 语句遍历文件 ################### ################### for in 语句遍历文件 ###################
DIR=/home/zp DIR=/home/zp
for FILE in ${DIR}/*.sh; do for FILE in ${DIR}/*.sh; do
mv "$FILE" "${DIR}/scripts" mv "$FILE" "${DIR}/scripts"
done done
# 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts # 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts

View File

@ -2,21 +2,23 @@
################### if 语句 ################### ################### if 语句 ###################
# 写成一行 # 写成一行
if [[ 1 -eq 1 ]]; then echo "1 -eq 1 result is: true"; fi if [[ 1 -eq 1 ]]; then
echo "1 -eq 1 result is: true";
fi
# Output: 1 -eq 1 result is: true # Output: 1 -eq 1 result is: true
# 写成多行 # 写成多行
if [[ "abc" -eq "abc" ]] if [[ "abc" -eq "abc" ]]
then then
echo ""abc" -eq "abc" result is: true" echo ""abc" -eq "abc" result is: true"
fi fi
# Output: abc -eq abc result is: true # Output: abc -eq abc result is: true
################### if else 语句 ################### ################### if else 语句 ###################
if [[ 2 -ne 1 ]]; then if [[ 2 -ne 1 ]]; then
echo "true" echo "true"
else else
echo "false" echo "false"
fi fi
# Output: true # Output: true
@ -24,10 +26,10 @@ fi
x=10 x=10
y=20 y=20
if [[ ${x} > ${y} ]]; then if [[ ${x} > ${y} ]]; then
echo "${x} > ${y}" echo "${x} > ${y}"
elif [[ ${x} < ${y} ]]; then elif [[ ${x} < ${y} ]]; then
echo "${x} < ${y}" echo "${x} < ${y}"
else else
echo "${x} = ${y}" echo "${x} = ${y}"
fi fi
# Output: 10 < 20 # Output: 10 < 20

View File

@ -5,10 +5,10 @@ select ITEM in bower npm gem pip
do do
echo -n "Enter the package name: " && read PACKAGE echo -n "Enter the package name: " && read PACKAGE
case ${ITEM} in case ${ITEM} in
bower) bower install ${PACKAGE} ;; bower) bower install ${PACKAGE} ;;
npm) npm install ${PACKAGE} ;; npm) npm install ${PACKAGE} ;;
gem) gem install ${PACKAGE} ;; gem) gem install ${PACKAGE} ;;
pip) pip install ${PACKAGE} ;; pip) pip install ${PACKAGE} ;;
esac esac
break # 避免无限循环 break # 避免无限循环
done done

View File

@ -2,8 +2,8 @@
x=0 x=0
until [[ ${x} -ge 5 ]]; do until [[ ${x} -ge 5 ]]; do
echo ${x} echo ${x}
x=`expr ${x} + 1` x=`expr ${x} + 1`
done done
# Output: # Output:
# 0 # 0

View File

@ -4,8 +4,8 @@
x=0 x=0
### x小于10 ### x小于10
while [[ ${x} -lt 10 ]]; do while [[ ${x} -lt 10 ]]; do
echo $((x * x)) echo $((x * x))
x=$((x + 1)) x=$((x + 1))
done done
# Output: # Output:
# 0 # 0

View File

@ -47,8 +47,8 @@ echo "key is ${key}"
################### 判断字符串中是否包含子字符串 ################### ################### 判断字符串中是否包含子字符串 ###################
result=$(echo "${str}" | grep "feature/") result=$(echo "${str}" | grep "feature/")
if [[ "$result" != "" ]] ; then if [[ "$result" != "" ]]; then
echo "feature/ 是 ${str} 的子字符串" echo "feature/ 是 ${str} 的子字符串"
else else
echo "feature/ 不是 ${str} 的子字符串" echo "feature/ 不是 ${str} 的子字符串"
fi fi

View File

@ -13,10 +13,10 @@ readonly rword
# rword="bye" # 如果放开注释,执行时会报错 # rword="bye" # 如果放开注释,执行时会报错
################### 删除变量 ################### ################### 删除变量 ###################
dword="hello" # 声明变量 dword="hello" # 声明变量
echo ${dword} # 输出变量值 echo ${dword} # 输出变量值
# Output: hello # Output: hello
unset dword # 删除变量 unset dword # 删除变量
echo ${dword} echo ${dword}
# Output: (空) # Output: (空)

View File

@ -6,7 +6,7 @@ database='test'
for f in `ls */*.sql` for f in `ls */*.sql`
do do
echo ${f}; echo ${f};
mysql -u${user} -p${password} -f ${database} -e "source $f"; mysql -u${user} -p${password} -f ${database} -e "source $f";
done done
echo 'OK!' echo 'OK!'

View File

@ -1,21 +1,22 @@
#!/bin/bash #!/bin/bash
# send data to the the table in the MYSQL database # send data to the the table in the MYSQL database
MYSQL=`which mysql` MYSQL=`which mysql`
if [ $# -ne 2 ] if [ $# -ne 2 ]
then then
echo "Usage:mtest2 emplid lastname firstname salary" echo "Usage:mtest2 emplid lastname firstname salary"
else else
#脚本变量一定要用双引号,字符串变量使用单引号 #脚本变量一定要用双引号,字符串变量使用单引号
statement=" insert into em_admin values(NULL, '$1', $2)" statement=" insert into em_admin values(NULL, '$1', $2)"
$MYSQL emwjs -u test <<EOF $MYSQL emwjs -u test << EOF
$statement $statement
EOF EOF
if [ $? -eq 0 ] if [ $? -eq 0 ]
then then
echo Data successfully added echo Data successfully added
else else
echo Problem adding data echo Problem adding data
fi fi
fi fi

View File

@ -6,7 +6,7 @@ MYSQL=`which mysql`
dbs=`$MYSQL emwjs -u test -Bse 'show tables;'` dbs=`$MYSQL emwjs -u test -Bse 'show tables;'`
for db in $dbs for db in $dbs
do do
echo $db echo $db
done done

View File

@ -1,13 +1,12 @@
#!/bin/bash #!/bin/bash
#连接数据库 #连接数据库
mysql=`which mysql mysql=`which mysql`
`
#发送单个命令 #发送单个命令
$mysql emwjs -u test -e "show databases;" $mysql emwjs -u test -e "show databases;"
#发送多个命令 #发送多个命令
$mysql emwjs -u test <<EOF $mysql emwjs -u test << EOF
show tables; show tables;
select * from em_admin; select * from em_admin;
EOF EOF

View File

@ -13,10 +13,10 @@ readonly readonly_var
# rword="bye" # 如果放开注释,执行时会报错 # rword="bye" # 如果放开注释,执行时会报错
################### 删除变量 ################### ################### 删除变量 ###################
dword="hello" # 声明变量 dword="hello" # 声明变量
echo ${dword} # 输出变量值 echo ${dword} # 输出变量值
# Output: hello # Output: hello
unset dword # 删除变量 unset dword # 删除变量
echo ${dword} echo ${dword}
# Output: (空) # Output: (空)

View File

@ -1,8 +1,9 @@
#!/bin/bash #!/bin/bash
#退出状态码最大为255超过则进行模运算 #退出状态码最大为255超过则进行模运算
#testing the exit status #testing the exit status
var1=10 var1=10
var2=20 var2=20
var3=$[ $var1 + $var2] var3=$[ $var1 + $var2 ]
echo The answer is $var3 echo The answer is $var3
exit 5 exit 5

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#An example of using the expr command #An example of using the expr command
var1=10 var1=10

View File

@ -5,7 +5,7 @@ var2=43.67
var3=33.2 var3=33.2
var4=71 var4=71
var5=`bc <<EOF var5=`bc << EOF
scale=4 scale=4
a1 = $var1 * $var2 a1 = $var1 * $var2
b1 = $var3 * $var4 b1 = $var3 * $var4

View File

@ -3,5 +3,5 @@
var1=10 var1=10
var2=50 var2=50
var3=45 var3=45
var4=$[$var1 * ($var2 - $var3)] var4=$[ $var1 * ( $var2 - $var3 ) ]
echo 'The final result is '$var4 echo 'The final result is '$var4

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#using the backtick character 会把反引号里面当作一条命令来执行 #using the backtick character 会把反引号里面当作一条命令来执行
testing=`date` testing=`date`

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
var1=100 var1=100
var2=45 var2=45
var3=`echo "scale=4; $var1 / $var2" | bc` var3=`echo "scale=4; $var1 / $var2" | bc`

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#This script displays the date and who's logged on #This script displays the date and who's logged on
#如果想在同一行显示 #如果想在同一行显示

View File

@ -1,10 +1,11 @@
#!/bin/bash #!/bin/bash
#display user information from system #display user information from system
echo "User info fro userId:$USER" echo "User info fro userId:$USER"
echo UID:$UID echo UID:$UID
echo HOME:$HOME echo HOME:$HOME
#换行 #换行
echo -e '\n' echo -e '\n'
echo 'The cost of the item is \$15' echo 'The cost of the item is \$15'

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#copy the /usr/bin directory listing to a log file #copy the /usr/bin directory listing to a log file
today=`date +%y%m%d` today=`date +%y%m%d`

View File

@ -7,7 +7,7 @@
str="0.0.0.1" str="0.0.0.1"
OLD_IFS="$IFS" OLD_IFS="$IFS"
IFS="." IFS="."
array=(${str}) array=( ${str} )
IFS="$OLD_IFS" IFS="$OLD_IFS"
size=${#array[*]} size=${#array[*]}
lastIndex=`expr ${size} - 1` lastIndex=`expr ${size} - 1`
@ -15,5 +15,5 @@ echo "数组长度:${size}"
echo "最后一个数组元素:${array[${lastIndex}]}" echo "最后一个数组元素:${array[${lastIndex}]}"
for item in ${array[@]} for item in ${array[@]}
do do
echo "$item" echo "$item"
done done

View File

@ -48,8 +48,8 @@ echo "version is ${version}"
################### 判断字符串中是否包含子字符串 ################### ################### 判断字符串中是否包含子字符串 ###################
result=$(echo "${str}" | grep "feature/") result=$(echo "${str}" | grep "feature/")
if [[ "$result" != "" ]] ; then if [[ "$result" != "" ]]; then
echo "feature/ 是 ${str} 的子字符串" echo "feature/ 是 ${str} 的子字符串"
else else
echo "feature/ 不是 ${str} 的子字符串" echo "feature/ 不是 ${str} 的子字符串"
fi fi

View File

@ -2,7 +2,7 @@
# testing signal trapping # testing signal trapping
trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT SIGTERM trap "echo'Sorry! I have trapped Ctrl-C'" SIGINT SIGTERM
echo this is a test program echo this is a test program
@ -10,7 +10,7 @@ count=1
while [ $count -le 10 ] while [ $count -le 10 ]
do do
echo "Loop #$count" echo "Loop #$count"
sleep 5 sleep 5
count=$[ $count+1 ] count=$[ $count + 1 ]
done done

View File

@ -2,12 +2,12 @@
# trapping the script exit # trapping the script exit
trap "echo byebye" EXIT trap "echobyebye" EXIT
count=1 count=1
while [ $count -le 5 ] while [ $count -le 5 ]
do do
echo "Loop #$count" echo "Loop #$count"
sleep 3 sleep 3
count=$[ $count + 1 ] count=$[ $count + 1 ]
done done

View File

@ -2,14 +2,14 @@
# removeing a set trap # removeing a set trap
trap "echo byebye" EXIT trap "echobyebye" EXIT
count=1 count=1
while [ $count -le 5 ] while [ $count -le 5 ]
do do
echo "Loop #$count" echo "Loop #$count"
sleep 3 sleep 3
count=$[ $count + 1 ] count=$[ $count + 1 ]
done done
#移除捕捉 #移除捕捉
trap - EXIT trap - EXIT

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# 创建数组 # 创建数组
nums=([2]=2 [0]=0 [1]=1) nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 )
colors=(red yellow "dark blue") colors=( red yellow "dark blue" )
# 访问数组的单个元素 # 访问数组的单个元素
echo ${nums[1]} echo ${nums[1]}
@ -43,13 +43,13 @@ echo ${#nums[*]}
# 3 # 3
# 向数组中添加元素 # 向数组中添加元素
colors=(white "${colors[@]}" green black) colors=( white "${colors[@]}" green black )
echo ${colors[@]} echo ${colors[@]}
# Output: # Output:
# white red yellow dark blue green black # white red yellow dark blue green black
# 从数组中删除元素 # 从数组中删除元素
unset nums[0] unset nums[ 0 ]
echo ${nums[@]} echo ${nums[@]}
# Output: # Output:
# 1 2 # 1 2

View File

@ -8,7 +8,7 @@ FS=":"
} }
{ {
print $1 " " $7 print $1 " " $7
} }
END { END {

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#使用内建变量 #使用内建变量
# NF 当前记录的字段个数 # NF 当前记录的字段个数

View File

@ -1,7 +1,8 @@
#!/bin/bash #!/bin/bash
#正则表达式 #正则表达式
gawk 'BEGIN{FS=","} gawk 'BEGIN{FS=","}
/11/{print $1} /11/{print $1}
' test ' test

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#gawk 自定义函数 #gawk 自定义函数
gawk ' gawk '

View File

@ -62,5 +62,5 @@ END{ for(c in pop) printf("%15-s%6d\n", c, pop[c]) | "sort -nk 2"; close("sort -
#在同一个程序中如果你写了一个文件而待会儿想要读取它那么就需要调用close。 #在同一个程序中如果你写了一个文件而待会儿想要读取它那么就需要调用close。
#某一时刻,同时处于打开状态的文件或管道数量最大值由实现定义。 #某一时刻,同时处于打开状态的文件或管道数量最大值由实现定义。
close("sort -nk 2") close ( " sort -nk 2 " )

View File

@ -6,13 +6,13 @@ mypath=`echo $PATH | sed 's/:/ /g'`
count=0 count=0
for directory in $mypath for directory in $mypath
do do
check=`ls $directory` check=`ls $directory`
echo $check echo $check
for item in $check for item in $check
do do
count=$[ $count + 1 ] count=$[ $count + 1 ]
done done
echo "$directory - $count" echo "$directory - $count"
count=0 count=0
done done

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#sed编辑器基础 #sed编辑器基础
#替换标记 #替换标记

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# shell wrapper for sed editor script to reverse lines # shell wrapper for sed editor script to reverse lines
sed -n '{ sed -n '{

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
#输出末尾10行数据 #输出末尾10行数据
sed '{ sed '{

View File

@ -8,8 +8,8 @@ number=$1
while [ $counter -le $number ] while [ $counter -le $number ]
do do
factorial=$[ $factorial * $counter ] factorial=$[ $factorial * $counter ]
counter=$[ $counter + 1 ] counter=$[ $counter + 1 ]
done done
result=`echo $factorial | sed '{ result=`echo $factorial | sed '{

View File

@ -3,23 +3,23 @@
# returning an array value # returning an array value
function arraydblr { function arraydblr {
local origarry local origarry
local newarray local newarray
local elements local elements
local i local i
origarry=(`echo "$@"`) origarry=( `echo "$@"` )
newarray=(`echo "$@"`) newarray=( `echo "$@"` )
elements=$[ $# - 1 ] elements=$[ $# - 1 ]
for (( i=0; i<=$elements; i++ )) for (( i = 0; i <= $elements; i ++ ))
{ {
newarray[$i]=$[ ${origarry[$i]} * 2 ] newarray [ $i ] = $[ ${origarry[$i]} * 2 ]
} }
echo ${newarray[*]} echo ${newarray[*]}
} }
myarray=(1 2 3 4 5) myarray=( 1 2 3 4 5 )
echo "The original array is : ${myarray[*]}" echo "The original array is : ${myarray[*]}"
arg1=`echo ${myarray[*]}` arg1=`echo ${myarray[*]}`
result=(`arraydblr $arg1`) result=( `arraydblr $arg1` )
echo "The new array is : ${result[*]}" echo "The new array is : ${result[*]}"

Some files were not shown because too many files have changed in this diff Show More