diff --git a/.editorconfig b/.editorconfig index d9c3abd..3b69c90 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,7 @@ # EditorConfig helps developers define and maintain consistent # coding styles between different editors and IDEs # http://editorconfig.org -# 所有文件换行使用 Unix like 风格(LF),bat 文件使用 win 风格(CRLF) +# 所有文件换行以 Unix like 风格(LF),win 格式特定的除外(bat) # 缩进 java 4 个空格,其他所有文件 2 个空格 root = true @@ -11,9 +11,8 @@ root = true end_of_line = lf # Change these settings to your own preference -indent_size = 2 indent_style = space -max_line_length = 120 +indent_size = 2 # We recommend you to keep these unchanged charset = utf-8 @@ -23,9 +22,8 @@ insert_final_newline = true [*.bat] end_of_line = crlf -[*.java] +[*.{java, sh}] indent_size = 4 [*.md] -max_line_length = 0 trim_trailing_whitespace = false diff --git a/codes/linux/build/helper.sh b/codes/linux/build/helper.sh index c3e120e..2466cd6 100644 --- a/codes/linux/build/helper.sh +++ b/codes/linux/build/helper.sh @@ -2,7 +2,7 @@ # 打印UI页头信息 function printHeadInfo() { -cat << EOF + cat << EOF *********************************************************************************** * 欢迎使用项目引导式发布脚本。 * 输入任意键进入脚本操作。 @@ -12,7 +12,7 @@ EOF # 打印UI页尾信息 function printFootInfo() { -cat << EOF + cat << EOF *********************************************************************************** @@ -25,18 +25,18 @@ EOF # 检查文件是否存在,不存在则退出脚本 function checkFileExist() { - if [ ! -f "$1" ] - then - echo "关键文件 $1 找不到,脚本执行结束" - exit 1 - fi + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 1 + fi } # 检查文件夹是否存在,不存在则创建 function createFolderIfNotExist() { - if [ ! -d "$1" ];then - mkdir -p "$1" - fi + if [ ! -d "$1" ]; then + mkdir -p "$1" + fi } # 记录发布的版本信息 @@ -45,18 +45,18 @@ function createFolderIfNotExist() { # 第三个参数为代码分支 # 第四个参数为运行环境 function saveVersionInfo() { - if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then - echo "缺少参数,退出" - exit 1 - fi + if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ]; then + echo "缺少参数,退出" + exit 1 + fi - VERSION_LOG_FILE=$1/$2-version.log - rm -rf ${VERSION_LOG_FILE} - touch ${VERSION_LOG_FILE} - chmod 777 ${VERSION_LOG_FILE} + VERSION_LOG_FILE=$1/$2-version.log + rm -rf ${VERSION_LOG_FILE} + touch ${VERSION_LOG_FILE} + chmod 777 ${VERSION_LOG_FILE} - echo -e "\n=================== $2 ===================" >> ${VERSION_LOG_FILE} - echo "Branch is: $3" >> ${VERSION_LOG_FILE} - echo "Profile is: $4" >> ${VERSION_LOG_FILE} - echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} + echo -e "\n=================== $2 ===================" >> ${VERSION_LOG_FILE} + echo "Branch is: $3" >> ${VERSION_LOG_FILE} + echo "Profile is: $4" >> ${VERSION_LOG_FILE} + echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} } diff --git a/codes/linux/build/java-app-boot.sh b/codes/linux/build/java-app-boot.sh index 4879c08..85db911 100644 --- a/codes/linux/build/java-app-boot.sh +++ b/codes/linux/build/java-app-boot.sh @@ -7,22 +7,22 @@ # 检查脚本参数,如必要参数未传入,退出脚本。 function checkInput() { - if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then - echo "请输入脚本参数:app oper javaArgs classpathArgs bootstrapClass" - echo " app: 应用名。" - echo " oper: 运行环境(必填)。可选值:start|stop|restart" - echo " javaArgs: JVM 参数(必填)。" - echo " classpathArgs: classpath参数(必填)。" - echo " bootstrapClass: 启动类(必填)。" - exit 0 - fi + if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then + echo "请输入脚本参数:app oper javaArgs classpathArgs bootstrapClass" + echo " app: 应用名。" + echo " oper: 运行环境(必填)。可选值:start|stop|restart" + echo " javaArgs: JVM 参数(必填)。" + echo " classpathArgs: classpath参数(必填)。" + echo " bootstrapClass: 启动类(必填)。" + exit 0 + fi } # 检查文件夹是否存在,不存在则创建 function createFolderIfNotExist() { - if [ ! -d "$1" ];then - mkdir -p "$1" - fi + if [ ! -d "$1" ]; then + mkdir -p "$1" + fi } # 检查服务是否已经启动 @@ -37,46 +37,46 @@ function checkStarted() { } function main() { - case "${oper}" in - start ) - echo -n "starting server: " - # 检查服务是否已经启动 - if checkStarted ;then - echo "ERROR: server already started!" - echo "PID: ${pids}" - exit 1 - fi + case "${oper}" in + start) + echo -n "starting server: " + # 检查服务是否已经启动 + if checkStarted; then + echo "ERROR: server already started!" + echo "PID: ${pids}" + exit 1 + fi - args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}" - echo -e "statup params:\n ${args}" + args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}" + echo -e "statup params:\n ${args}" - #启动服务 - touch ${LOG_DIR}/${app}-startup.log - nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 & - # echo -e "执行参数:\n${args}" - echo -e "\nthe server is started..." - ;; - stop ) - echo -n "stopping server: " - #dubbo提供优雅停机, 不能使用kill -9 - if checkStarted ;then - kill ${pids} - echo -e "\nthe server is stopped..." - else - echo -e "\nno server to be stopped..." - fi - ;; - restart ) - $0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" - sleep 5 - $0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" - ;; - * ) - echo "Invalid oper: ${oper}." - exit 1 - esac + #启动服务 + touch ${LOG_DIR}/${app}-startup.log + nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 & + # echo -e "执行参数:\n${args}" + echo -e "\nthe server is started..." + ;; + stop) + echo -n "stopping server: " + #dubbo提供优雅停机, 不能使用kill -9 + if checkStarted; then + kill ${pids} + echo -e "\nthe server is stopped..." + else + echo -e "\nno server to be stopped..." + fi + ;; + restart) + $0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + sleep 5 + $0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + ;; + *) + echo "Invalid oper: ${oper}." + exit 1 + esac - exit 0 + exit 0 } ######################################## MAIN ######################################## diff --git a/codes/linux/build/java-app-release.sh b/codes/linux/build/java-app-release.sh index 45d916c..02b77c0 100644 --- a/codes/linux/build/java-app-release.sh +++ b/codes/linux/build/java-app-release.sh @@ -7,41 +7,41 @@ # 检查脚本参数,如必要参数未传入,退出脚本。 checkInput() { - if [ "${branch}" == "" ] || [ "${profile}" == "" ]; then - echo "请输入脚本参数:branch profile" - echo " branch: git分支(必填)。如 feature/1.1.16, master" - echo " profile: 运行环境(必填)。可选值:development | test" - echo "例:./java-app-release.sh feature/1.1.16 test" - exit 0 - fi + if [ "${branch}" == "" ] || [ "${profile}" == "" ]; then + echo "请输入脚本参数:branch profile" + echo " branch: git分支(必填)。如 feature/1.1.16, master" + echo " profile: 运行环境(必填)。可选值:development | test" + echo "例:./java-app-release.sh feature/1.1.16 test" + exit 0 + fi } # 检查文件是否存在,不存在则退出脚本 checkFileExist() { - if [ ! -f "$1" ] - then - echo "关键文件 $1 找不到,脚本执行结束" - exit 0 - fi + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi } # 检查文件夹是否存在,不存在则创建 createFolderIfNotExist() { - if [ ! -d "$1" ];then - mkdir -p "$1" - fi + if [ ! -d "$1" ]; then + mkdir -p "$1" + fi } # 记录发布的版本信息 saveVersionInfo() { - rm -rf ${VERSION_LOG_FILE} - touch ${VERSION_LOG_FILE} - chmod 777 ${VERSION_LOG_FILE} + rm -rf ${VERSION_LOG_FILE} + touch ${VERSION_LOG_FILE} + chmod 777 ${VERSION_LOG_FILE} - echo -e "\n=================== Version Info ===================" >> ${VERSION_LOG_FILE} - echo "Branch is: ${branch}" >> ${VERSION_LOG_FILE} - echo "Profile is: ${profile}" >> ${VERSION_LOG_FILE} - echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} + echo -e "\n=================== Version Info ===================" >> ${VERSION_LOG_FILE} + echo "Branch is: ${branch}" >> ${VERSION_LOG_FILE} + echo "Profile is: ${profile}" >> ${VERSION_LOG_FILE} + echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} } ######################################## MAIN ######################################## @@ -50,7 +50,8 @@ export LANG="zh_CN.UTF-8" # 设置全局常量 LOG_DIR=/home/zp/log/ -SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SCRIPT_DIR=$(cd "$(dirname "$0")"; +pwd) SOURCE_DIR=/home/zp/source/ APP_NAME=XXX 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} execode=$? if [ "${execode}" == "0" ]; then - echo "更新代码成功" + echo "更新代码成功" else - echo "更新代码失败" - exit 1 + echo "更新代码失败" + exit 1 fi echo ">>>>>>>>>>>>>> 3. 替换配置" @@ -87,12 +88,12 @@ cd ${SOURCE_DIR}/ck-lion mvn clean package -e -Dmaven.test.skip=true | tee ${MAVEN_LOG_FILE} eexecode=$? if [ "${execode}" == "0" ]; then - echo "构建编译成功" - echo "编译详情见:${MAVEN_LOG_FILE}" + echo "构建编译成功" + echo "编译详情见:${MAVEN_LOG_FILE}" else - echo "构建编译失败" - echo "编译详情见:${MAVEN_LOG_FILE}" - exit 1 + echo "构建编译失败" + echo "编译详情见:${MAVEN_LOG_FILE}" + exit 1 fi echo ">>>>>>>>>>>>>> 5. 启动应用" @@ -101,10 +102,10 @@ echo 3 > /proc/sys/vm/drop_caches ${SCRIPT_DIR}/java-app-run.sh ${profile} start execode=$? if [ "${execode}" == "0" ]; then - echo "启动应用成功" + echo "启动应用成功" else - echo "启动应用失败" - exit 1 + echo "启动应用失败" + exit 1 fi echo ">>>>>>>>>>>>>> 6. 记录发布的版本信息" diff --git a/codes/linux/build/java-app-run.sh b/codes/linux/build/java-app-run.sh index 88dfc4e..db81b24 100644 --- a/codes/linux/build/java-app-run.sh +++ b/codes/linux/build/java-app-run.sh @@ -7,51 +7,51 @@ # 检查脚本参数,如必要参数未传入,退出脚本。 function checkInput() { - if [ "${profile}" == "" ] || [ "${oper}" == "" ]; then - echo "请输入脚本参数:profile oper [debug]" - echo " profile: 运行环境(必填)。可选值:development|test" - echo " oper: 运行环境(必填)。可选值:start|stop|restart" - echo " debug: debug启动开关。默认不填为不启动。" - exit 0 - fi + if [ "${profile}" == "" ] || [ "${oper}" == "" ]; then + echo "请输入脚本参数:profile oper [debug]" + echo " profile: 运行环境(必填)。可选值:development|test" + echo " oper: 运行环境(必填)。可选值:start|stop|restart" + echo " debug: debug启动开关。默认不填为不启动。" + exit 0 + fi } #检查文件是否存在,不存在则退出脚本 function checkFileExist() { - if [ ! -f "$1" ] - then - echo "关键文件 $1 找不到,脚本执行结束" - exit 0 - fi + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi } # 封装启动参数,调用启动脚本 -function main(){ - APP_NAME=ck-lion +function main() { + APP_NAME=ck-lion - # 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_DEBUG_OPTS="" - if [ "$2" == "debug" ]; then - JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2236,server=y,suspend=n " - shift - fi - javaArgs=" ${JAVA_OPTS} ${JAVA_DEBUG_OPTS} " + # 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_DEBUG_OPTS="" + if [ "$2" == "debug" ]; then + JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2236,server=y,suspend=n " + shift + fi + javaArgs=" ${JAVA_OPTS} ${JAVA_DEBUG_OPTS} " - # classpath 参数 - classpathArgs="${SERVER_ROOT}/WEB-INF/classes:${SERVER_ROOT}/WEB-INF/lib/*" + # classpath 参数 + 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}" - execode=$? - if [ "${execode}" == "0" ]; then - echo "执行操作成功" - else - echo "执行操作失败" - exit 1 - fi + ${SCRIPT_DIR}/java-app-boot.sh ${APP_NAME} ${oper} "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + execode=$? + if [ "${execode}" == "0" ]; then + echo "执行操作成功" + else + echo "执行操作失败" + exit 1 + fi } ######################################## MAIN ######################################## @@ -59,7 +59,8 @@ function main(){ export LANG="zh_CN.UTF-8" # 设置全局常量 -SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SCRIPT_DIR=$(cd "$(dirname "$0")"; +pwd) SOURCE_DIR=/home/zp/source/ APP_NAME=XXX SERVER_ROOT=/home/zp/source/${APP_NAME}/target/ diff --git a/codes/linux/build/js-app-release.sh b/codes/linux/build/js-app-release.sh index ec5aab4..617d9ae 100644 --- a/codes/linux/build/js-app-release.sh +++ b/codes/linux/build/js-app-release.sh @@ -5,50 +5,50 @@ # 检查脚本参数,如必要参数未传入,退出脚本。 function checkInput() { - if [ "${branch}" == "" ]; then - echo "请输入脚本参数:branch" - echo " branch: git分支。如 feature/1.1.16, master" - exit 1 - fi + if [ "${branch}" == "" ]; then + echo "请输入脚本参数:branch" + echo " branch: git分支。如 feature/1.1.16, master" + exit 1 + fi } # 脚本主方法 function main() { - echo ">>>>>>>>>>>>>> 1. 更新代码" - ${SCRIPT_DIR}/update-code.sh ${APP} ${branch} ${SOURCE_DIR} - execode=$? - if [ "${execode}" == "0" ]; then - echo "更新代码成功" - else - echo "更新代码失败" - exit 1 - fi + echo ">>>>>>>>>>>>>> 1. 更新代码" + ${SCRIPT_DIR}/update-code.sh ${APP} ${branch} ${SOURCE_DIR} + execode=$? + if [ "${execode}" == "0" ]; then + echo "更新代码成功" + else + echo "更新代码失败" + exit 1 + fi - echo ">>>>>>>>>>>>>> 2. 替换配置" - # 有的应用此处可能需要替换配置 + echo ">>>>>>>>>>>>>> 2. 替换配置" + # 有的应用此处可能需要替换配置 - echo ">>>>>>>>>>>>>> 3. 构建编译" - cd ${SOURCE_DIR}/${APP} - source "${HOME}/.nvm/nvm.sh" - nvm use 8.9 - npm install - if [ "${profile}" == "develop" ] || [ "${profile}" == "test" ]; then - npm start - elif [ "${profile}" == "preview" ] || [ "${profile}" == "product" ]; then - npm run build - fi - execode=$? - if [ "${execode}" == "0" ]; then - echo "构建编译成功" - else - echo "构建编译失败" - exit 1 - fi + echo ">>>>>>>>>>>>>> 3. 构建编译" + cd ${SOURCE_DIR}/${APP} + source "${HOME}/.nvm/nvm.sh" + nvm use 8.9 + npm install + if [ "${profile}" == "develop" ] || [ "${profile}" == "test" ]; then + npm start + elif [ "${profile}" == "preview" ] || [ "${profile}" == "product" ]; then + npm run build + fi + execode=$? + if [ "${execode}" == "0" ]; then + echo "构建编译成功" + else + echo "构建编译失败" + exit 1 + fi - echo ">>>>>>>>>>>>>> 4. 记录发布的版本信息" - saveVersionInfo ${LOG_DIR} ${APP} ${branch} ${profile} + echo ">>>>>>>>>>>>>> 4. 记录发布的版本信息" + saveVersionInfo ${LOG_DIR} ${APP} ${branch} ${profile} - echo ">>>>>>>>>>>>>> 发布应用结束" + echo ">>>>>>>>>>>>>> 发布应用结束" } ######################################## MAIN ######################################## @@ -59,7 +59,8 @@ export LANG="zh_CN.UTF-8" APP=blog LOG_DIR=/home/zp/log SOURCE_DIR=/home/zp/source -SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SCRIPT_DIR=$(cd "$(dirname "$0")"; +pwd) # 装载函数库 . ${SCRIPT_DIR}/helper.sh diff --git a/codes/linux/build/main.sh b/codes/linux/build/main.sh index a74f73a..26d92fe 100644 --- a/codes/linux/build/main.sh +++ b/codes/linux/build/main.sh @@ -7,7 +7,7 @@ # 选择应用 function chooseAppName() { -cat << EOF + cat << EOF 请选择应用名(数字或关键字均可)。 可选值如下: [0] all (所有应用) @@ -15,28 +15,28 @@ cat << EOF [2] APP2 EOF -while read app -do - case ${app} in - 0 ) - app=all - break ;; - 1 ) - app=js-app - break ;; - 2 ) - app=APP2 - break ;; - all | js-app | APP2 ) - break ;; - * ) echo "无法识别 ${app}" ;; - esac -done + while read app + do + case ${app} in + 0) + app=all + break ;; + 1) + app=js-app + break ;; + 2) + app=APP2 + break ;; + all | js-app | APP2) + break ;; + *) echo "无法识别 ${app}" ;; + esac + done } # 选择操作 function chooseOper() { -cat << EOF + cat << EOF 请选择想要执行的操作(数字或关键字均可)。 可选值如下: [1] start @@ -44,44 +44,44 @@ cat << EOF [3] stop EOF -while read oper -do - case ${oper} in - 1 ) - oper=start - break ;; - 2 ) - oper=restart - break ;; - 3 ) - oper=stop - break ;; - start | restart | stop ) - break ;; - * ) echo "无法识别 ${oper}" ;; - esac -done + while read oper + do + case ${oper} in + 1) + oper=start + break ;; + 2) + oper=restart + break ;; + 3) + oper=stop + break ;; + start | restart | stop) + break ;; + *) echo "无法识别 ${oper}" ;; + esac + done } # 选择代码分支 function chooseBranch() { -cat << EOF + cat << EOF 请输入 git 分支。 如:develop、master、feature/xxx EOF -read branch -if [[ "${branch}" =~ ^(feature/)([^ \f\n\r\t\v]+) ]] || [ "${branch}" == "develop" ] || [ "${branch}" == "master" ]; then - echo "输入了 ${branch}" -else - echo "无法识别 ${branch}" - chooseBranch -fi + read branch + if [[ "${branch}" =~ ^ ( feature/ ) ( [^ \f\n\r\t\v]+ ) ]] || [ "${branch}" == "develop" ] || [ "${branch}" == "master" ]; then + echo "输入了 ${branch}" + else + echo "无法识别 ${branch}" + chooseBranch + fi } # 选择运行环境 function chooseProfile() { -cat << EOF + cat << EOF 请选择运行环境(数字或关键字均可)。 可选值: [1] develop (开发环境) @@ -90,32 +90,32 @@ cat << EOF [4] product (生产环境) EOF -while read profile -do - case ${profile} in - 1 ) - profile=develop - break ;; - 2 ) - profile=test - break ;; - 3 ) - profile=preview - break ;; - 4 ) - profile=product - break ;; - develop | test | preview | product ) - break ;; - * ) echo "无法识别 ${profile}" ;; - esac -done + while read profile + do + case ${profile} in + 1) + profile=develop + break ;; + 2) + profile=test + break ;; + 3) + profile=preview + break ;; + 4) + profile=product + break ;; + develop | test | preview | product) + break ;; + *) echo "无法识别 ${profile}" ;; + esac + done } # 确认选择 function confirmChoice() { -cat << EOF + cat << EOF =================================================== 请确认您的选择:Y/N app: ${app} @@ -125,66 +125,66 @@ cat << EOF =================================================== EOF - while read confirm - do - case ${confirm} in - y | Y ) - echo -e "\n\n>>>>>>>>>>>>>> 开始发布应用" - break ;; - n | N ) - echo -e "重新输入发布参数\n" - inputParams ;; - * ) - echo "无法识别 ${confirm}" ;; - esac - done + while read confirm + do + case ${confirm} in + y | Y) + echo -e "\n\n>>>>>>>>>>>>>> 开始发布应用" + break ;; + n | N) + echo -e "重新输入发布参数\n" + inputParams ;; + *) + echo "无法识别 ${confirm}" ;; + esac + done } # 引导式发布应用 function releaseApp() { - # 输入执行参数 - app="" - branch="" - profile="" - chooseAppName - chooseOper - if [ "${oper}" == "stop" ]; then - confirmChoice - if [ "${app}" == "all" ]; then - ${SCRIPT_DIR}/${app}-run.sh stop ${profile} + # 输入执行参数 + app="" + branch="" + profile="" + chooseAppName + chooseOper + if [ "${oper}" == "stop" ]; then + confirmChoice + if [ "${app}" == "all" ]; then + ${SCRIPT_DIR}/${app}-run.sh stop ${profile} + else + ${SCRIPT_DIR}/${app}-run.sh stop ${profile} + fi 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 - 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() { -printHeadInfo -while read sign -do - case ${sign} in - exit) - echo "主动退出脚本" - exit 0 ;; - * ) - releaseApp ;; - esac + printHeadInfo + while read sign + do + case ${sign} in + exit) + echo "主动退出脚本" + exit 0 ;; + *) + releaseApp ;; + esac - # 装载函数库 - printFootInfo -done + # 装载函数库 + printFootInfo + done } ######################################## MAIN ######################################## @@ -192,7 +192,8 @@ done export LANG="zh_CN.UTF-8" # 设置全局常量 -SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SCRIPT_DIR=$(cd "$(dirname "$0")"; +pwd) SOURCE_DIR=/home/zp/source/ # 装载函数库 diff --git a/codes/linux/build/update-code.sh b/codes/linux/build/update-code.sh index 5a3a0df..cadaa7e 100644 --- a/codes/linux/build/update-code.sh +++ b/codes/linux/build/update-code.sh @@ -8,78 +8,78 @@ # 检查脚本参数,如必要参数未传入,退出脚本。 checkInput() { - if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then - echo "请输入脚本参数:repository branch [source] [target]" - echo " repository: git 仓储(必填)。" - echo " branch: git 分支(必填)。如 master/develop" - echo " source: 代码存放目录。默认为/home/zp/source。" - echo " target: 代码存放目录。默认为脚本所在目录。" - exit 1 - fi + if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then + echo "请输入脚本参数:repository branch [source] [target]" + echo " repository: git 仓储(必填)。" + echo " branch: git 分支(必填)。如 master/develop" + echo " source: 代码存放目录。默认为/home/zp/source。" + echo " target: 代码存放目录。默认为脚本所在目录。" + exit 1 + fi } # 检查文件夹是否存在,不存在则创建 function createFolderIfNotExist() { - if [ ! -d "$1" ];then - mkdir -p "$1" - fi + if [ ! -d "$1" ]; then + mkdir -p "$1" + fi } # 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。 gitok=false 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} - 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 操作。 function doFetchOrClone() { - if ${gitok}; then - cd ${SOURCE_DIR}/${repository}/${target} - git reset --hard - git clean -ffdx - git fetch - echo "git fetch ${repository} remote repository 到本地成功" - else - #删除所有内容,便于重新进行git clone - rm -rf ${repository} - git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target} - echo "git clone ${repository} remote repository 到本地成功" - cd ${SOURCE_DIR}/${repository}/${target} - fi + if ${gitok}; then + cd ${SOURCE_DIR}/${repository}/${target} + git reset --hard + git clean -ffdx + git fetch + echo "git fetch ${repository} remote repository 到本地成功" + else + #删除所有内容,便于重新进行git clone + rm -rf ${repository} + git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target} + echo "git clone ${repository} remote repository 到本地成功" + cd ${SOURCE_DIR}/${repository}/${target} + fi } # 切换到 ${branch} 分支 function doCheckout() { - echo "检出 ${repository} ${branch} 分支代码" - isRemoteBranch=false - gitRemoteBranch=`git branch -r` - echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false - if ${isRemoteBranch}; then - echo "找到 ${branch} 分支。" - git checkout -f 'origin/'${branch} - else - echo "未找到 ${branch} 分支!" - exit 2 - fi - echo "更新子模块代码" - git submodule update --init --recursive --force + echo "检出 ${repository} ${branch} 分支代码" + isRemoteBranch=false + gitRemoteBranch=`git branch -r` + echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false + if ${isRemoteBranch}; then + echo "找到 ${branch} 分支。" + git checkout -f 'origin/'${branch} + else + echo "未找到 ${branch} 分支!" + exit 2 + fi + echo "更新子模块代码" + git submodule update --init --recursive --force } ######################################## MAIN ######################################## @@ -96,7 +96,7 @@ checkInput GITHUB_ACCOUNT=dunwu SOURCE_DIR=/home/xyz/source if [ "${source}" != "" ]; then - SOURCE_DIR=${source} + SOURCE_DIR=${source} fi createFolderIfNotExist ${SOURCE_DIR} diff --git a/codes/linux/download.sh b/codes/linux/download.sh index ee6d323..5300e3a 100644 --- a/codes/linux/download.sh +++ b/codes/linux/download.sh @@ -29,15 +29,17 @@ printf "${RESET}" path=/home/scripts/linux-tutorial 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 git >/dev/null 2>&1 || { printf "${YELLOW}Not detected git. Install git.${RESET}\n"; yum -y install git; } +command -v yum > /dev/null 2>&1 || { printf "${RED}Not detected yum.${RESET}"; + 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 - cd ${path} - git pull + cd ${path} + git pull else - mkdir -p ${path} - git clone https://gitee.com/turnon/linux-tutorial.git ${path} + mkdir -p ${path} + git clone https://gitee.com/turnon/linux-tutorial.git ${path} fi chmod +x -R ${path} printf "\n${GREEN}<<<<<<<< Download linux-tutorial to ${path} end.${RESET}\n" diff --git a/codes/linux/dunwu-ops.sh b/codes/linux/dunwu-ops.sh index 161554e..0422d6d 100644 --- a/codes/linux/dunwu-ops.sh +++ b/codes/linux/dunwu-ops.sh @@ -14,8 +14,8 @@ RESET="$(tput sgr0)" # 打印头部信息 printHeadInfo() { -printf "${BLUE}" -cat << EOF + printf "${BLUE}" + cat << EOF *********************************************************************************** * 欢迎使用 Linux CentOS 环境运维脚本 @@ -23,67 +23,68 @@ cat << EOF *********************************************************************************** EOF -printf "${RESET}" + printf "${RESET}" } # 打印尾部信息 printFootInfo() { -printf "${BLUE}" -cat << EOF + printf "${BLUE}" + cat << EOF *********************************************************************************** * 脚本执行结束,感谢使用! *********************************************************************************** EOF -printf "${RESET}" + printf "${RESET}" } # 检查操作系统环境 -checkOsVersion(){ - if(($1 == 1)); then - echo -e "检查操作系统环境是否兼容本套脚本" +checkOsVersion() { + if (($1 == 1)); then + echo -e "检查操作系统环境是否兼容本套脚本" - platform=`uname -i` - if [[ ${platform} != "x86_64" ]]; then - echo "脚本仅支持 64 位操作系统!" - exit 1 + platform=`uname -i` + if [[ ${platform} != "x86_64" ]]; then + echo "脚本仅支持 64 位操作系统!" + 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 - - 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() { -PS3="请输入命令编号:" -select item in ${menus[@]} -do -case ${item} in - "配置系统") - ./dunwu-sys.sh - main ;; - "安装软件") - ./dunwu-soft.sh - main ;; - "退出") - exit 0 ;; - *) - printf "输入项不支持!\n" - main ;; -esac -break -done + PS3="请输入命令编号:" + select item in ${menus[@]} + do + case ${item} in + "配置系统") + ./dunwu-sys.sh + main ;; + "安装软件") + ./dunwu-soft.sh + main ;; + "退出") + exit 0 ;; + *) + printf "输入项不支持!\n" + main ;; + esac + break + done } ######################################## MAIN ######################################## -path=$(cd "$(dirname "$0")"; pwd) +path=$(cd "$(dirname "$0")"; +pwd) printHeadInfo checkOsVersion 0 main diff --git a/codes/linux/dunwu-soft.sh b/codes/linux/dunwu-soft.sh index fdb933f..049acbf 100644 --- a/codes/linux/dunwu-soft.sh +++ b/codes/linux/dunwu-soft.sh @@ -24,37 +24,38 @@ EOF # print menu printf "${PURPLE}" -menus=(docker fastdfs gitlab jdk8 jenkins kafka maven mongodb mysql nacos nexus nginx nodejs redis rocketmq tomcat8 -zookeeper zsh exit) +menus=( docker fastdfs gitlab jdk8 jenkins kafka maven mongodb mysql nacos nexus nginx nodejs redis rocketmq tomcat8 +zookeeper zsh exit ) for i in "${!menus[@]}"; do - index=`expr ${i} + 1` - val=`expr ${index} % 2` - printf "[%02d] %-20s" "${index}" "${menus[$i]}" - if [[ ${val} -eq 0 ]]; then - printf "\n" - fi + index=`expr ${i} + 1` + val=`expr ${index} % 2` + printf "[%02d] %-20s" "${index}" "${menus[$i]}" + if [[ ${val} -eq 0 ]]; then + printf "\n" + fi done printf "\n${RESET}请输入需要安装的软件编号:\n" # exec shell to install soft doInstall() { -read -t 30 index -if [[ -n ${index} ]]; then - no=`expr ${index} - 1` - len=${#menus[*]} - if [[ ${index} -gt ${len} ]]; then - echo "输入项不支持!" - exit -1 - fi - key=${menus[$no]} - if [[ key == 'exit' ]]; then - exit 0 - fi - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/${key}-install.sh | bash - doInstall -else - echo "输入项不支持!" - exit -1 -fi + read -t 30 index + if [[ -n ${index} ]]; then + no=`expr ${index} - 1` + len=${#menus[*]} + if [[ ${index} -gt ${len} ]]; then + echo "输入项不支持!" + exit -1 + fi + key=${menus[$no]} + if [[ key == 'exit' ]]; then + exit 0 + fi + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/${key}-install.sh | bash + doInstall + else + echo "输入项不支持!" + exit -1 + fi } + doInstall diff --git a/codes/linux/dunwu-sys.sh b/codes/linux/dunwu-sys.sh index f1d8747..e493bde 100644 --- a/codes/linux/dunwu-sys.sh +++ b/codes/linux/dunwu-sys.sh @@ -9,38 +9,38 @@ cat << EOF EOF -menus=("替换yum镜像" "安装基本的命令工具" "安装常用libs" "系统配置" "全部执行" "退出") +menus=( "替换yum镜像" "安装基本的命令工具" "安装常用libs" "系统配置" "全部执行" "退出" ) main() { -PS3="请输入命令编号:" -select item in ${menus[@]} -do -case ${item} in - "替换yum镜像") - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash - main ;; - "安装基本的命令工具") - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools.sh | bash - main ;; - "安装常用libs") - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash - main ;; - "系统配置") - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash - 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/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/sys-settings.sh | bash - printf "执行完毕,退出。\n" ;; - "退出") - exit 0 ;; - *) - printf "输入项不支持!\n" - main ;; -esac -break -done + PS3="请输入命令编号:" + select item in ${menus[@]} + do + case ${item} in + "替换yum镜像") + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/change-yum-repo.sh | bash + main ;; + "安装基本的命令工具") + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-tools.sh | bash + main ;; + "安装常用libs") + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/install-libs.sh | bash + main ;; + "系统配置") + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/sys-settings.sh | bash + 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/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/sys-settings.sh | bash + printf "执行完毕,退出。\n" ;; + "退出") + exit 0 ;; + *) + printf "输入项不支持!\n" + main ;; + esac + break + done } ######################################## MAIN ######################################## diff --git a/codes/linux/soft/docker-install.sh b/codes/linux/soft/docker-install.sh index 9305697..549406a 100644 --- a/codes/linux/soft/docker-install.sh +++ b/codes/linux/soft/docker-install.sh @@ -26,16 +26,16 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install docker begin.${RESET}\n" # uninstall old version docker -sudo yum remove docker \ - docker-client \ - docker-client-latest \ - docker-common \ - docker-latest \ - docker-latest-logrotate \ - docker-logrotate \ - docker-selinux \ - docker-engine-selinux \ - docker-engine +sudo yum remove docker \ + docker-client \ + docker-client-latest \ + docker-common \ + docker-latest \ + docker-latest-logrotate \ + docker-logrotate \ + docker-selinux \ + docker-engine-selinux \ + docker-engine # install required libs sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # add docker yum repo diff --git a/codes/linux/soft/elk/boot-elk.sh b/codes/linux/soft/elk/boot-elk.sh index 31ee08f..501e36e 100644 --- a/codes/linux/soft/elk/boot-elk.sh +++ b/codes/linux/soft/elk/boot-elk.sh @@ -2,50 +2,50 @@ # 检查脚本输入参数 checkInput() { - if [ "${app}" == "" ] || [ "${oper}" == "" ]; then - echo "请输入脚本参数:name" - echo " app: 要启动的进程关键字(必填)。可选值:elasticsearch|logstash|kibana|filebeat" - echo " oper: 执行操作(必填)。可选值:start|stop" - echo "例:./boot-elk.sh logstash start" - exit 0 - fi + if [ "${app}" == "" ] || [ "${oper}" == "" ]; then + echo "请输入脚本参数:name" + echo " app: 要启动的进程关键字(必填)。可选值:elasticsearch|logstash|kibana|filebeat" + echo " oper: 执行操作(必填)。可选值:start|stop" + echo "例:./boot-elk.sh logstash start" + exit 0 + fi - if [ "${app}" != "elasticsearch" ] && [ "${app}" != "logstash" ] && [ "${app}" != "kibana" ] && [ "${app}" != "filebeat" ]; then - echo "name 输入错误" - echo "可选值:elasticsearch|logstash|kibana|filebeat" - exit 0 - fi + if [ "${app}" != "elasticsearch" ] && [ "${app}" != "logstash" ] && [ "${app}" != "kibana" ] && [ "${app}" != "filebeat" ]; then + echo "name 输入错误" + echo "可选值:elasticsearch|logstash|kibana|filebeat" + exit 0 + fi } # 检查文件是否存在,不存在则退出脚本 checkFileExist() { - if [ ! -f "$1" ] - then - echo "关键文件 $1 找不到,脚本执行结束" - exit 0 - fi + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi } startup() { - if [ "${app}" == "elasticsearch" ]; then - checkFileExist ${ELASTICSEARCH_BIN_PATH}/elasticsearch - nohup sh ${ELASTICSEARCH_BIN_PATH}/elasticsearch >>${ELASTICSEARCH_BIN_PATH}/nohup.out 2>&1 & - elif [ "${app}" == "logstash" ]; then - checkFileExist ${LOGSTASH_BIN_PATH}/logstash - nohup sh ${LOGSTASH_BIN_PATH}/logstash -f ${LOGSTASH_BIN_PATH}/logstash.conf >>${LOGSTASH_BIN_PATH}/nohup.out 2>&1 & - elif [ "${app}" == "kibana" ]; then - checkFileExist ${KIBANA_BIN_PATH}/kibana - nohup sh ${KIBANA_BIN_PATH}/kibana >> ${KIBANA_BIN_PATH}/nohup.out 2>&1 & - elif [ "${app}" == "filebeat" ]; then - checkFileExist ${FILEBEAT_PATH}/filebeat - touch ${FILEBEAT_PATH}/nohup.out - nohup ${FILEBEAT_PATH}/filebeat -e -c ${FILEBEAT_PATH}/filebeat.yml -d "publish" >> ${FILEBEAT_PATH}/nohup.out 2>&1 & - fi + if [ "${app}" == "elasticsearch" ]; then + checkFileExist ${ELASTICSEARCH_BIN_PATH}/elasticsearch + nohup sh ${ELASTICSEARCH_BIN_PATH}/elasticsearch >> ${ELASTICSEARCH_BIN_PATH}/nohup.out 2>&1 & + elif [ "${app}" == "logstash" ]; then + checkFileExist ${LOGSTASH_BIN_PATH}/logstash + nohup sh ${LOGSTASH_BIN_PATH}/logstash -f ${LOGSTASH_BIN_PATH}/logstash.conf >> ${LOGSTASH_BIN_PATH}/nohup.out 2>&1 & + elif [ "${app}" == "kibana" ]; then + checkFileExist ${KIBANA_BIN_PATH}/kibana + nohup sh ${KIBANA_BIN_PATH}/kibana >> ${KIBANA_BIN_PATH}/nohup.out 2>&1 & + elif [ "${app}" == "filebeat" ]; then + checkFileExist ${FILEBEAT_PATH}/filebeat + touch ${FILEBEAT_PATH}/nohup.out + nohup ${FILEBEAT_PATH}/filebeat -e -c ${FILEBEAT_PATH}/filebeat.yml -d "publish" >> ${FILEBEAT_PATH}/nohup.out 2>&1 & + fi } shutdown() { - pid=`ps -ef | grep java | grep ${app} | awk '{print $2}'` - kill -9 ${pid} + pid=`ps -ef | grep java | grep ${app} | awk '{print $2}'` + kill -9 ${pid} } ##############################__MAIN__######################################## @@ -60,14 +60,14 @@ FILEBEAT_PATH=/opt/elastic/filebeat-${version}-linux-x86_64 checkInput case ${oper} in - start) - echo "启动 ${app}" - startup + start) + echo "启动 ${app}" + startup ;; - stop) - echo "终止 ${app}" - shutdown + stop) + echo "终止 ${app}" + shutdown ;; - * ) echo "${oper} is invalid oper";; + *) echo "${oper} is invalid oper" ;; esac diff --git a/codes/linux/soft/elk/install-elk.sh b/codes/linux/soft/elk/install-elk.sh index 8751960..9b39fec 100644 --- a/codes/linux/soft/elk/install-elk.sh +++ b/codes/linux/soft/elk/install-elk.sh @@ -9,98 +9,99 @@ # 获取当前设备IP ipaddr='127.0.0.1' 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() { - if [ ! -f "$1" ] - then - echo "关键文件 $1 找不到,脚本执行结束" - exit 0 - fi + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi } init() { - mkdir -p ${ELASTIC_SOFTWARE_PATH} - getDeviceIp + mkdir -p ${ELASTIC_SOFTWARE_PATH} + getDeviceIp } # 安装 elasticsearch installElasticsearch() { - cd ${ELASTIC_SOFTWARE_PATH} - wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${version}.tar.gz - tar -xzf elasticsearch-${version}.tar.gz + cd ${ELASTIC_SOFTWARE_PATH} + wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${version}.tar.gz + tar -xzf elasticsearch-${version}.tar.gz } installRuby() { - cd ${RUBY_SOFTWARE_PATH} - wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.gz - tar -xzf ruby-2.5.0.tar.gz - cd ruby-2.5.0 - ./configure - make & make install + cd ${RUBY_SOFTWARE_PATH} + wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.gz + tar -xzf ruby-2.5.0.tar.gz + cd ruby-2.5.0 + ./configure + make & make install } # 安装 logstash installLogstash() { - cd ${ELASTIC_SOFTWARE_PATH} - wget https://artifacts.elastic.co/downloads/logstash/logstash-${version}.tar.gz - tar -xzf logstash-${version}.tar.gz + cd ${ELASTIC_SOFTWARE_PATH} + wget https://artifacts.elastic.co/downloads/logstash/logstash-${version}.tar.gz + tar -xzf logstash-${version}.tar.gz } # 安装 kibana installKibana() { - cd ${ELASTIC_SOFTWARE_PATH} - wget https://artifacts.elastic.co/downloads/kibana/kibana-${version}-linux-x86_64.tar.gz - tar -xzf kibana-${version}-linux-x86_64.tar.gz + cd ${ELASTIC_SOFTWARE_PATH} + wget https://artifacts.elastic.co/downloads/kibana/kibana-${version}-linux-x86_64.tar.gz + tar -xzf kibana-${version}-linux-x86_64.tar.gz } # 安装 filebeat installFilebeat() { - cd ${ELASTIC_SOFTWARE_PATH} - wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${version}-linux-x86_64.tar.gz - tar -zxf filebeat-${version}-linux-x86_64.tar.gz + cd ${ELASTIC_SOFTWARE_PATH} + wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${version}-linux-x86_64.tar.gz + tar -zxf filebeat-${version}-linux-x86_64.tar.gz } # 替换 Elasticsearch 配置 # 1. 替换 192.168.0.1 为本机 IP replaceElasticsearchConfig() { - 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 - touch ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/bin/nohup.out + 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 + touch ${ELASTIC_SOFTWARE_PATH}/elasticsearch-${version}/bin/nohup.out } replaceLogstashConfig() { - 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 - touch ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin/nohup.out - cd ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin - wget "https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/elk/config/logstash.conf" + 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 + touch ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin/nohup.out + cd ${ELASTIC_SOFTWARE_PATH}/logstash-${version}/bin + wget "https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/elk/config/logstash.conf" } # 替换 Kibana 配置 # 1. 替换 localhost 为本机 IP 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 - 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 - touch ${ELASTIC_SOFTWARE_PATH}/kibana-${version}-linux-x86_64/bin/nohup.out + 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/#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 } # 替换 Filebeat 配置 replaceFilebeatConfig() { - 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 - 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 + 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 + 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 } # 为 elk.elk 用户设置权限 setPrivilegeForUser() { - chown -R elk.elk ${ELASTIC_SOFTWARE_PATH} - chown -R elk.elk /var/log/ + chown -R elk.elk ${ELASTIC_SOFTWARE_PATH} + chown -R elk.elk /var/log/ } + ######################################## MAIN ######################################## echo -e "\n>>>>>>>>> install elk" diff --git a/codes/linux/soft/elk/install_elasticserch.sh b/codes/linux/soft/elk/install_elasticserch.sh index 9511390..479b3e3 100644 --- a/codes/linux/soft/elk/install_elasticserch.sh +++ b/codes/linux/soft/elk/install_elasticserch.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # version:v1.0 # 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_path=/usr/local/ 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" -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 clear @@ -29,23 +30,23 @@ echo "3: Install elasticsearch 6.3.1" echo "4: EXIT" # 选择安装软件版本 read -p "Please input your choice:" softversion -if [ "${softversion}" == "1" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz" -elif [ "${softversion}" == "2" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz" -elif [ "${softversion}" == "3" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz" -elif [ "${softversion}" == "4" ];then - echo "you choce channel!" - exit 1; +if [ "${softversion}" == "1" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-5.4.1.tar.gz" +elif [ "${softversion}" == "2" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.0.1.tar.gz" +elif [ "${softversion}" == "3" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/elasticsearch/elasticsearch-6.3.1.tar.gz" +elif [ "${softversion}" == "4" ]; then + echo "you choce channel!" + exit 1; else - echo "input Error! Place input{1|2|3|4|5}" - exit 0; + echo "input Error! Place input{1|2|3|4|5}" + exit 0; fi # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 output_msg() { - for msg in $*;do + for msg in $*; do action $msg /bin/true done } @@ -53,21 +54,21 @@ output_msg() { # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 check_yum_command() { - output_msg "命令检查:$1" - hash $1 >/dev/null 2>&1 - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 - else - 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 } - fi + output_msg "命令检查:$1" + hash $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0 + else + 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 } + fi } # 判断目录是否存在,传入目录绝对路径,可以传入多个目录 check_dir() { output_msg "目录检查" - for dirname in $*;do - [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + for dirname in $*; do + [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1 echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} done } @@ -75,13 +76,13 @@ check_dir() { # 下载文件并解压至安装目录,传入url链接地址 download_file() { output_msg "下载源码包" - mkdir -p $download_path - for file in $*;do + mkdir -p $download_path + for file in $*; do wget $file -c -P $download_path &> /dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name} 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 done } @@ -89,54 +90,54 @@ download_file() { # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 extract_file() { - output_msg "解压源码" - for file in $*;do - 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} - 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} - else - echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 - fi + output_msg "解压源码" + for file in $*; do + 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} + 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} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1 + fi done } # 配置环境变量,第一个参数为添加环境变量的绝对路径 config_env() { output_msg "环境变量配置" - - echo "export PATH=\$PATH:$1" >${env_file} - source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + + echo "export PATH=\$PATH:$1" > ${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name} } # 配置主机名,第一个为主机名 config_hostname() { -if [ ${sysversion} -eq 6 ];then - hostname $1 -elif [ ${sysversion} -eq 7 ];then - hostnamectl set-hostname $1 -else - echo "`date +%F' '%H:%M:%S` hostname $1 config fail">> ${install_log_path}${install_log_name} -fi + if [ ${sysversion} -eq 6 ]; then + hostname $1 + elif [ ${sysversion} -eq 7 ]; then + hostnamectl set-hostname $1 + else + echo "`date +%F' '%H:%M:%S` hostname $1 config fail" >> ${install_log_path}${install_log_name} + fi } config_limits() { output_msg "配置limits" - cat >>/etc/security/limits.conf<> /etc/security/limits.conf << EOF +* soft nofile 65536 +* hard nofile 65536 +* soft nproc 65536 * hard nproc 65536 EOF -echo "vm.max_map_count = 655360" >>/etc/sysctl.conf -sysctl -p >/dev/null 2>&1 + echo "vm.max_map_count = 655360" >> /etc/sysctl.conf + sysctl -p > /dev/null 2>&1 } # 添加配置文件 add_config() { -cat > $1 << EOF + cat > $1 << EOF cluster.name: my-application node.name: ${hostname} path.data: /usr/local/elasticsearch/data @@ -148,46 +149,46 @@ EOF } config_user() { -useradd $1 >/dev/null 2>&1 -if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $1 user add success">> ${install_log_path}${install_log_name} -else - echo "`date +%F' '%H:%M:%S` $1 user add fail">> ${install_log_path}${install_log_name} && exit 1 -fi -chown ${1}.${1} ${install_path}elasticsearch/ -R + useradd $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $1 user add success" >> ${install_log_path}${install_log_name} + else + echo "`date +%F' '%H:%M:%S` $1 user add fail" >> ${install_log_path}${install_log_name} && exit 1 + fi + chown ${1}.${1} ${install_path}elasticsearch/ -R } config_jvm() { -if [ ${sys_mem} -eq 0 ];then - sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} - sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} -else - 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} -fi + if [ ${sys_mem} -eq 0 ]; then + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} + else + 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} + fi } main() { -check_dir $install_log_path $install_path -check_yum_command wget wget -download_file $URL -config_hostname $hostname + check_dir $install_log_path $install_path + check_yum_command wget wget + download_file $URL + config_hostname $hostname -software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') -for filename in `ls $download_path`;do - extract_file ${download_path}$filename -done + software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}') + for filename in `ls $download_path`; do + extract_file ${download_path}$filename + done -rm -fr ${download_path} -ln -s $install_path$software_name ${install_path}elasticsearch -add_config $software_config_file -check_dir ${install_path}elasticsearch/{data,logs} -config_user elasticsearch -config_env ${install_path}elasticsearch/bin -config_limits -config_jvm -echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'" + rm -fr ${download_path} + ln -s $install_path$software_name ${install_path}elasticsearch + add_config $software_config_file + check_dir ${install_path}elasticsearch/{data,logs} + config_user elasticsearch + config_env ${install_path}elasticsearch/bin + config_limits + config_jvm + echo "请使用一下命令启动服务:'su - elasticsearch -c 'nohup /usr/local/elasticsearch/bin/elasticsearch &'" } diff --git a/codes/linux/soft/elk/install_filebeat.sh b/codes/linux/soft/elk/install_filebeat.sh index 6c09aec..bd8265e 100644 --- a/codes/linux/soft/elk/install_filebeat.sh +++ b/codes/linux/soft/elk/install_filebeat.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # version:v1.0 # 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" # 选择安装软件版本 read -p "Please input your choice:" softversion -if [ "${softversion}" == "1" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz" -elif [ "${softversion}" == "2" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz" -elif [ "${softversion}" == "3" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz" -elif [ "${softversion}" == "4" ];then - echo "you choce channel!" - exit 1; +if [ "${softversion}" == "1" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-5.6.1-linux-x86_64.tar.gz" +elif [ "${softversion}" == "2" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.1.3-linux-x86_64.tar.gz" +elif [ "${softversion}" == "3" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/filebeat/filebeat-6.3.2-linux-x86_64.tar.gz" +elif [ "${softversion}" == "4" ]; then + echo "you choce channel!" + exit 1; else - echo "input Error! Place input{1|2|3|4}" - exit 0; + echo "input Error! Place input{1|2|3|4}" + exit 0; fi # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 output_msg() { - for msg in $*;do + for msg in $*; do action $msg /bin/true done } @@ -50,21 +51,21 @@ output_msg() { # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 check_yum_command() { - output_msg "命令检查:$1" - hash $1 >/dev/null 2>&1 - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 - else - 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 } - fi + output_msg "命令检查:$1" + hash $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0 + else + 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 } + fi } # 判断目录是否存在,传入目录绝对路径,可以传入多个目录 check_dir() { output_msg "目录检查" - for dirname in $*;do - [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + for dirname in $*; do + [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1 echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} done } @@ -72,13 +73,13 @@ check_dir() { # 下载文件并解压至安装目录,传入url链接地址 download_file() { output_msg "下载源码包" - mkdir -p $download_path - for file in $*;do + mkdir -p $download_path + for file in $*; do wget $file -c -P $download_path &> /dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name} 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 done } @@ -86,29 +87,29 @@ download_file() { # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 extract_file() { - output_msg "解压源码" - for file in $*;do - 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} - 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} - else - echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 - fi + output_msg "解压源码" + for file in $*; do + 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} + 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} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1 + fi done } # 配置环境变量,第一个参数为添加环境变量的绝对路径 config_env() { output_msg "环境变量配置" - echo "export PATH=\$PATH:$1" >${env_file} - source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + echo "export PATH=\$PATH:$1" > ${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name} } # 添加配置文件 add_config() { -cat> $1 < $1 << EOF filebeat.prospectors: - input_type: log paths: @@ -119,17 +120,17 @@ EOF } main() { -check_dir $install_log_path $install_path -check_yum_command wget wget -download_file $URL + check_dir $install_log_path $install_path + check_yum_command wget wget + download_file $URL -software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') -for filename in `ls $download_path`;do - extract_file ${download_path}$filename -done -rm -fr ${download_path} -ln -s $install_path$software_name ${install_path}filebeat -add_config ${software_config_file} + software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}') + for filename in `ls $download_path`; do + extract_file ${download_path}$filename + done + rm -fr ${download_path} + ln -s $install_path$software_name ${install_path}filebeat + add_config ${software_config_file} } main diff --git a/codes/linux/soft/elk/install_kibana.sh b/codes/linux/soft/elk/install_kibana.sh index 5532531..7359d2d 100644 --- a/codes/linux/soft/elk/install_kibana.sh +++ b/codes/linux/soft/elk/install_kibana.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # version:v1.0 # 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" # 选择安装软件版本 read -p "Please input your choice:" softversion -if [ "${softversion}" == "1" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.0.1-linux-x86_64.tar.gz" -elif [ "${softversion}" == "2" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.2.4-linux-x86_64.tar.gz" -elif [ "${softversion}" == "3" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.3.1-linux-x86_64.tar.gz" -elif [ "${softversion}" == "4" ];then - echo "you choce channel!" - exit 1; +if [ "${softversion}" == "1" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.0.1-linux-x86_64.tar.gz" +elif [ "${softversion}" == "2" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.2.4-linux-x86_64.tar.gz" +elif [ "${softversion}" == "3" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/kibana/kibana-6.3.1-linux-x86_64.tar.gz" +elif [ "${softversion}" == "4" ]; then + echo "you choce channel!" + exit 1; else - echo "input Error! Place input{1|2|3|4}" - exit 0; + echo "input Error! Place input{1|2|3|4}" + exit 0; fi # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 output_msg() { - for msg in $*;do + for msg in $*; do action $msg /bin/true done } @@ -50,21 +51,21 @@ output_msg() { # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 check_yum_command() { - output_msg "命令检查:$1" - hash $1 >/dev/null 2>&1 - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 - else - 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 } - fi + output_msg "命令检查:$1" + hash $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0 + else + 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 } + fi } # 判断目录是否存在,传入目录绝对路径,可以传入多个目录 check_dir() { output_msg "目录检查" - for dirname in $*;do - [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + for dirname in $*; do + [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1 echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} done } @@ -72,13 +73,13 @@ check_dir() { # 下载文件并解压至安装目录,传入url链接地址 download_file() { output_msg "下载源码包" - mkdir -p $download_path - for file in $*;do + mkdir -p $download_path + for file in $*; do wget $file -c -P $download_path &> /dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name} 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 done } @@ -86,29 +87,29 @@ download_file() { # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 extract_file() { - output_msg "解压源码" - for file in $*;do - 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} - 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} - else - echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 - fi + output_msg "解压源码" + for file in $*; do + 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} + 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} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1 + fi done } # 配置环境变量,第一个参数为添加环境变量的绝对路径 config_env() { output_msg "环境变量配置" - echo "export PATH=\$PATH:$1" >${env_file} - source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + echo "export PATH=\$PATH:$1" > ${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name} } # 添加配置文件 add_config() { -cat> $1 < $1 << EOF server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://127.0.0.1:9200" @@ -116,18 +117,18 @@ EOF } main() { -check_dir $install_log_path $install_path -check_yum_command wget wget -download_file $URL + check_dir $install_log_path $install_path + check_yum_command wget wget + download_file $URL -software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') -for filename in `ls $download_path`;do - extract_file ${download_path}$filename -done -rm -fr ${download_path} -ln -s ${install_path}$software_name ${install_path}kibana -add_config ${software_config_file} -config_env ${install_path}kibana/bin + software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}') + for filename in `ls $download_path`; do + extract_file ${download_path}$filename + done + rm -fr ${download_path} + ln -s ${install_path}$software_name ${install_path}kibana + add_config ${software_config_file} + config_env ${install_path}kibana/bin } main diff --git a/codes/linux/soft/elk/install_logstash.sh b/codes/linux/soft/elk/install_logstash.sh index 673e52d..7300bff 100644 --- a/codes/linux/soft/elk/install_logstash.sh +++ b/codes/linux/soft/elk/install_logstash.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # version:v1.0 # func:logstash 5.4/6.1/6.3 安装 @@ -26,23 +27,23 @@ echo "3: Install logstash-6.3" echo "4: EXIT" # 选择安装软件版本 read -p "Please input your choice:" softversion -if [ "${softversion}" == "1" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-5.4.1.tar.gz" -elif [ "${softversion}" == "2" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.1.3.tar.gz" -elif [ "${softversion}" == "3" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.3.2.tar.gz" -elif [ "${softversion}" == "4" ];then - echo "you choce channel!" - exit 1; +if [ "${softversion}" == "1" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-5.4.1.tar.gz" +elif [ "${softversion}" == "2" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.1.3.tar.gz" +elif [ "${softversion}" == "3" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/logstash/logstash-6.3.2.tar.gz" +elif [ "${softversion}" == "4" ]; then + echo "you choce channel!" + exit 1; else - echo "input Error! Place input{1|2|3|4}" - exit 0; + echo "input Error! Place input{1|2|3|4}" + exit 0; fi # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 output_msg() { - for msg in $*;do + for msg in $*; do action $msg /bin/true done } @@ -50,21 +51,21 @@ output_msg() { # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 check_yum_command() { - output_msg "命令检查:$1" - hash $1 >/dev/null 2>&1 - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 - else - 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 } - fi + output_msg "命令检查:$1" + hash $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0 + else + 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 } + fi } # 判断目录是否存在,传入目录绝对路径,可以传入多个目录 check_dir() { output_msg "目录检查" - for dirname in $*;do - [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + for dirname in $*; do + [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1 echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} done } @@ -72,13 +73,13 @@ check_dir() { # 下载文件并解压至安装目录,传入url链接地址 download_file() { output_msg "下载源码包" - mkdir -p $download_path - for file in $*;do + mkdir -p $download_path + for file in $*; do wget $file -c -P $download_path &> /dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name} 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 done } @@ -86,29 +87,29 @@ download_file() { # 解压文件,可以传入多个压缩文件绝对路径,用空格隔开,解压至安装目录 extract_file() { - output_msg "解压源码" - for file in $*;do - 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} - 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} - else - echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!">>${install_log_path}${install_log_name} && exit 1 - fi + output_msg "解压源码" + for file in $*; do + 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} + 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} + else + echo "`date +%F' '%H:%M:%S` $file type error, extrac fail!" >> ${install_log_path}${install_log_name} && exit 1 + fi done } # 配置环境变量,第一个参数为添加环境变量的绝对路径 config_env() { output_msg "环境变量配置" - echo "export PATH=\$PATH:$1" >${env_file} - source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!">> ${install_log_path}${install_log_name} + echo "export PATH=\$PATH:$1" > ${env_file} + source ${env_file} && echo "`date +%F' '%H:%M:%S` 软件安装完成!" >> ${install_log_path}${install_log_name} } # 添加配置文件 add_config() { -cat> $1 < $1 << EOF input { beats { port => "5044" @@ -124,18 +125,18 @@ EOF } main() { -check_dir $install_log_path $install_path -check_yum_command wget wget -download_file $URL + check_dir $install_log_path $install_path + check_yum_command wget wget + download_file $URL -software_name=$(echo $URL|awk -F'/' '{print $NF}'|awk -F'.tar.gz' '{print $1}') -for filename in `ls $download_path`;do - extract_file ${download_path}$filename -done -rm -fr ${download_path} -ln -s $install_path$software_name ${install_path}logstash -add_config ${software_config_file} -config_env ${install_path}logstash/bin + software_name=$(echo $URL | awk -F'/' '{print $NF}' | awk -F'.tar.gz' '{print $1}') + for filename in `ls $download_path`; do + extract_file ${download_path}$filename + done + rm -fr ${download_path} + ln -s $install_path$software_name ${install_path}logstash + add_config ${software_config_file} + config_env ${install_path}logstash/bin } main diff --git a/codes/linux/soft/fastdfs-install.sh b/codes/linux/soft/fastdfs-install.sh index 0ad79b4..65617f6 100644 --- a/codes/linux/soft/fastdfs-install.sh +++ b/codes/linux/soft/fastdfs-install.sh @@ -28,19 +28,21 @@ printf "${RESET}" 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 git >/dev/null 2>&1 || { printf "${RED}Require git 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; } +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 - printf "${PURPLE}[Hint]\n" - printf "\t sh fastdfs-install.sh [path]\n" - printf "\t Example: sh fastdfs-install.sh /opt/fastdfs\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh fastdfs-install.sh [path]\n" + printf "\t Example: sh fastdfs-install.sh /opt/fastdfs\n" + printf "${RESET}\n" fi path=/opt/fdfs if [[ -n $1 ]]; then - path=$1 + path=$1 fi nginx_version=1.16.0 @@ -55,8 +57,8 @@ path=/opt/fdfs mkdir -p ${path}/libfastcommon curl -o ${path}/libfastcommon.zip http://dunwu.test.upcdn.net/soft/fdfs/libfastcommon.zip if [[ ! -f ${path}/libfastcommon.zip ]]; then - printf "${RED}[Error]install libfastcommon failed,exit. ${RESET}\n" - exit 1 + printf "${RED}[Error]install libfastcommon failed,exit. ${RESET}\n" + exit 1 fi unzip -o ${path}/libfastcommon.zip -d ${path} @@ -69,7 +71,7 @@ printf "${GREEN}>>>>>>>>> install fastdfs${RESET}" mkdir -p ${path}/fastdfs curl -o ${path}/fastdfs.zip http://dunwu.test.upcdn.net/soft/fdfs/fastdfs.zip if [[ ! -f ${path}/fastdfs.zip ]]; then - printf "${RED}>>>>>>>>> install fastdfs failed,exit. ${RESET}\n" + printf "${RED}>>>>>>>>> install fastdfs failed,exit. ${RESET}\n" fi unzip -o ${path}/fastdfs.zip -d ${path} cd ${path}/fastdfs @@ -80,7 +82,7 @@ printf "${GREEN}>>>>>>>>> install fastdfs-nginx-module${RESET}\n" mkdir -p ${path}/fastdfs-nginx-module 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 - printf "${RED}>>>>>>>>> install fastdfs-nginx-module failed,exit. ${RESET}\n" + printf "${RED}>>>>>>>>> install fastdfs-nginx-module failed,exit. ${RESET}\n" fi unzip -o ${path}/fastdfs-nginx-module.zip -d ${path} diff --git a/codes/linux/soft/install_grafana.sh b/codes/linux/soft/install_grafana.sh index 36ddc39..1a235db 100644 --- a/codes/linux/soft/install_grafana.sh +++ b/codes/linux/soft/install_grafana.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # version:v1.0 # 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" # 选择安装软件版本 read -p "Please input your choice:" softversion -if [ "${softversion}" == "1" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm" -elif [ "${softversion}" == "2" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm" -elif [ "${softversion}" == "3" ];then - URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm" -elif [ "${softversion}" == "4" ];then - echo "you choce channel!" - exit 1; +if [ "${softversion}" == "1" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.0-1.x86_64.rpm" +elif [ "${softversion}" == "2" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.1.5-1.x86_64.rpm" +elif [ "${softversion}" == "3" ]; then + URL="https://anchnet-script.oss-cn-shanghai.aliyuncs.com/grafana/grafana-5.2.2-1.x86_64.rpm" +elif [ "${softversion}" == "4" ]; then + echo "you choce channel!" + exit 1; else - echo "input Error! Place input{1|2|3|4}" - exit 0; + echo "input Error! Place input{1|2|3|4}" + exit 0; fi # 传入内容,格式化内容输出,可以传入多个参数,用空格隔开 output_msg() { - for msg in $*;do + for msg in $*; do action $msg /bin/true done } @@ -49,21 +50,21 @@ output_msg() { # 判断命令是否存在,第一个参数 $1 为判断的命令,第二个参数为提供该命令的yum 软件包名称 check_yum_command() { - output_msg "命令检查:$1" - hash $1 >/dev/null 2>&1 - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` check command $1 ">>${install_log_path}${install_log_name} && return 0 - else - 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 } - fi + output_msg "命令检查:$1" + hash $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` check command $1 " >> ${install_log_path}${install_log_name} && return 0 + else + 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 } + fi } # 判断目录是否存在,传入目录绝对路径,可以传入多个目录 check_dir() { output_msg "目录检查" - for dirname in $*;do - [ -d $dirname ] || mkdir -p $dirname >/dev/null 2>&1 + for dirname in $*; do + [ -d $dirname ] || mkdir -p $dirname > /dev/null 2>&1 echo "`date +%F' '%H:%M:%S` $dirname check success!" >> ${install_log_path}${install_log_name} done } @@ -71,13 +72,13 @@ check_dir() { # 下载文件并解压至安装目录,传入url链接地址 download_file() { output_msg "下载源码包" - mkdir -p $download_path - for file in $*;do + mkdir -p $download_path + for file in $*; do wget $file -c -P $download_path &> /dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` $file download success!">>${install_log_path}${install_log_name} + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` $file download success!" >> ${install_log_path}${install_log_name} 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 done } @@ -86,24 +87,24 @@ download_file() { install_grafana_plugins() { output_msg "grafana插件安装" check_yum_command grafana-cli - grafana-cli plugins install $* >/dev/null - if [ $? -eq 0 ];then - echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!">>${install_log_path}${install_log_name} + grafana-cli plugins install $* > /dev/null + if [ $? -eq 0 ]; then + echo "`date +%F' '%H:%M:%S` grafana plugins $* install success!" >> ${install_log_path}${install_log_name} 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 } main() { -check_dir $install_log_path $install_path -check_yum_command wget wget -download_file $URL -for filename in `ls $download_path`;do - yum -y install $download_path$filename >/dev/null 2>&1 -done -install_grafana_plugins alexanderzobnin-zabbix-app + check_dir $install_log_path $install_path + check_yum_command wget wget + download_file $URL + for filename in `ls $download_path`; do + yum -y install $download_path$filename > /dev/null 2>&1 + done + install_grafana_plugins alexanderzobnin-zabbix-app } main diff --git a/codes/linux/soft/jdk8-install.sh b/codes/linux/soft/jdk8-install.sh index 2461825..9ac84ea 100644 --- a/codes/linux/soft/jdk8-install.sh +++ b/codes/linux/soft/jdk8-install.sh @@ -27,7 +27,8 @@ printf "${RESET}" 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 java -version diff --git a/codes/linux/soft/kafka-install.sh b/codes/linux/soft/kafka-install.sh index d7282d3..6a31a87 100644 --- a/codes/linux/soft/kafka-install.sh +++ b/codes/linux/soft/kafka-install.sh @@ -26,23 +26,24 @@ printf "${RESET}" 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 - printf "${PURPLE}[Hint]\n" - printf "\t sh kafka-install.sh [version] [path]\n" - printf "\t Example: sh kafka-install.sh 2.2.0 /opt/kafka\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh kafka-install.sh [version] [path]\n" + printf "\t Example: sh kafka-install.sh 2.2.0 /opt/kafka\n" + printf "${RESET}\n" fi version=2.2.0 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/kafka if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/maven-install.sh b/codes/linux/soft/maven-install.sh index f698352..399e51c 100644 --- a/codes/linux/soft/maven-install.sh +++ b/codes/linux/soft/maven-install.sh @@ -28,23 +28,24 @@ printf "${RESET}" 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 - printf "${PURPLE}[Hint]\n" - printf "\t sh maven-install.sh [version] [path]\n" - printf "\t Example: sh maven-install.sh 3.6.0 /opt/maven\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh maven-install.sh [version] [path]\n" + printf "\t Example: sh maven-install.sh 3.6.0 /opt/maven\n" + printf "${RESET}\n" fi version=3.6.0 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/maven if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/mongodb-install.sh b/codes/linux/soft/mongodb-install.sh index ad17a63..f92d02e 100644 --- a/codes/linux/soft/mongodb-install.sh +++ b/codes/linux/soft/mongodb-install.sh @@ -26,21 +26,21 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install mongodb begin.${RESET}\n" -if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then - printf "${PURPLE}[Hint]\n" - printf "\t sh mongodb-install.sh [version] [path]\n" - printf "\t Example: sh mongodb-install.sh 4.0.9 /opt/mongodb\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh mongodb-install.sh [version] [path]\n" + printf "\t Example: sh mongodb-install.sh 4.0.9 /opt/mongodb\n" + printf "${RESET}\n" fi version=4.0.9 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/mongodb if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/mysql-install.sh b/codes/linux/soft/mysql-install.sh index 066e4ed..0b3971b 100644 --- a/codes/linux/soft/mysql-install.sh +++ b/codes/linux/soft/mysql-install.sh @@ -26,9 +26,12 @@ printf "${RESET}" 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 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; } +command -v wget > /dev/null 2>&1 || { printf "${RED}Require wget 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 wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm diff --git a/codes/linux/soft/nacos-install.sh b/codes/linux/soft/nacos-install.sh index 68bf834..4182859 100644 --- a/codes/linux/soft/nacos-install.sh +++ b/codes/linux/soft/nacos-install.sh @@ -11,22 +11,24 @@ cat << EOF EOF -command -v java >/dev/null 2>&1 || { echo >&2 "Require java but it's not installed."; exit 1; } -command -v mvn >/dev/null 2>&1 || { echo >&2 "Require mvn but it's not installed."; exit 1; } +command -v java > /dev/null 2>&1 || { echo >&2 "Require java but it's not installed."; + 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]" printf "Example: sh nacos-install.sh 1.0.0 /opt/nacos\n" fi version=1.0.0 if [[ -n $1 ]]; then - version=$1 + version=$1 fi root=/opt/nacos if [[ -n $2 ]]; then - root=$2 + root=$2 fi echo "Current execution: install nacos ${version} to ${root}" diff --git a/codes/linux/soft/nginx-install.sh b/codes/linux/soft/nginx-install.sh index fd14790..a44726c 100644 --- a/codes/linux/soft/nginx-install.sh +++ b/codes/linux/soft/nginx-install.sh @@ -27,23 +27,24 @@ printf "${RESET}" 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 - printf "${PURPLE}[Hint]\n" - printf "\t sh nginx-install.sh [version] [path]\n" - printf "\t Example: sh nginx-install.sh 1.16.0 /opt/nginx\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh nginx-install.sh [version] [path]\n" + printf "\t Example: sh nginx-install.sh 1.16.0 /opt/nginx\n" + printf "${RESET}\n" fi version=1.16.0 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/nginx if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/nodejs-install.sh b/codes/linux/soft/nodejs-install.sh index 9a84b9b..ca11b59 100644 --- a/codes/linux/soft/nodejs-install.sh +++ b/codes/linux/soft/nodejs-install.sh @@ -27,16 +27,16 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install nodejs begin.${RESET}\n" -if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then - printf "${PURPLE}[Hint]\n" - printf "\t sh nodejs-install.sh [version]\n" - printf "\t Example: sh nodejs-install.sh 10.15.2\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh nodejs-install.sh [version]\n" + printf "\t Example: sh nodejs-install.sh 10.15.2\n" + printf "${RESET}\n" fi version=10.15.2 if [[ -n $1 ]]; then - version=$1 + version=$1 fi # install info diff --git a/codes/linux/soft/redis-install.sh b/codes/linux/soft/redis-install.sh index f103325..ee942bf 100644 --- a/codes/linux/soft/redis-install.sh +++ b/codes/linux/soft/redis-install.sh @@ -24,31 +24,32 @@ cat << EOF EOF 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 -e "Example: sh redis-install.sh 5.0.4 /opt/redis 6379 123456\n" fi version=5.0.4 if [[ -n $1 ]]; then - version=$1 + version=$1 fi root=/opt/redis if [[ -n $2 ]]; then - root=$2 + root=$2 fi port=6379 if [[ -n $3 ]]; then - port=$3 + port=$3 fi password= if [[ -n $4 ]]; then - password=$4 + password=$4 fi printf "${GREEN}>>>>>>>> install redis begin.${RESET}\n" @@ -72,7 +73,7 @@ mkdir -p /etc/redis cp ${path}/redis.conf /etc/redis/${port}.conf sed -i "s/^port 6379/port ${port}/g" /etc/redis/${port}.conf 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 printf "\n${CYAN}>>>>>>>>> add firewall port${RESET}\n" diff --git a/codes/linux/soft/rocketmq-install.sh b/codes/linux/soft/rocketmq-install.sh index c7fe771..cf4fff8 100644 --- a/codes/linux/soft/rocketmq-install.sh +++ b/codes/linux/soft/rocketmq-install.sh @@ -26,21 +26,21 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n" -if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then - printf "${PURPLE}[Hint]\n" - printf "\t sh rocketmq-install.sh [version] [path]\n" - printf "\t Example: sh rocketmq-install.sh 4.5.0 /opt/rocketmq\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh rocketmq-install.sh [version] [path]\n" + printf "\t Example: sh rocketmq-install.sh 4.5.0 /opt/rocketmq\n" + printf "${RESET}\n" fi version=4.5.0 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/rocketmq if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/tomcat8-install.sh b/codes/linux/soft/tomcat8-install.sh index 0980b15..f967bd0 100644 --- a/codes/linux/soft/tomcat8-install.sh +++ b/codes/linux/soft/tomcat8-install.sh @@ -26,21 +26,21 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install tomcat begin.${RESET}\n" -if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then - printf "${PURPLE}[Hint]\n" - printf "\t sh tomcat8-install.sh [version] [path]\n" - printf "\t Example: sh tomcat8-install.sh 8.5.28 /opt/tomcat8\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh tomcat8-install.sh [version] [path]\n" + printf "\t Example: sh tomcat8-install.sh 8.5.28 /opt/tomcat8\n" + printf "${RESET}\n" fi version=8.5.28 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/tomcat if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/zookeeper-install.sh b/codes/linux/soft/zookeeper-install.sh index 2a8fe8f..68cacef 100644 --- a/codes/linux/soft/zookeeper-install.sh +++ b/codes/linux/soft/zookeeper-install.sh @@ -26,21 +26,21 @@ printf "${RESET}" printf "${GREEN}>>>>>>>> install zookeeper begin.${RESET}\n" -if [[ $# -lt 1 ]] || [[ $# -lt 2 ]];then - printf "${PURPLE}[Hint]\n" - printf "\t sh zookeeper-install.sh [version] [path]\n" - printf "\t Example: sh zookeeper-install.sh 3.4.12 /opt/zookeeper\n" - printf "${RESET}\n" +if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then + printf "${PURPLE}[Hint]\n" + printf "\t sh zookeeper-install.sh [version] [path]\n" + printf "\t Example: sh zookeeper-install.sh 3.4.12 /opt/zookeeper\n" + printf "${RESET}\n" fi version=3.4.12 if [[ -n $1 ]]; then - version=$1 + version=$1 fi path=/opt/zookeeper if [[ -n $2 ]]; then - path=$2 + path=$2 fi # install info diff --git a/codes/linux/soft/zsh-install.sh b/codes/linux/soft/zsh-install.sh index 0831750..ce7a0ec 100644 --- a/codes/linux/soft/zsh-install.sh +++ b/codes/linux/soft/zsh-install.sh @@ -27,8 +27,10 @@ printf "${RESET}" 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 git >/dev/null 2>&1 || { printf "${RED}Require git 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; } +command -v git > /dev/null 2>&1 || { printf "${RED}Require git but it's not installed.${RESET}\n"; + exit 1; } # install zsh yum install -y zsh diff --git a/codes/linux/sys/change-yum-repo.sh b/codes/linux/sys/change-yum-repo.sh index 345c0c3..9ab7cb1 100644 --- a/codes/linux/sys/change-yum-repo.sh +++ b/codes/linux/sys/change-yum-repo.sh @@ -22,21 +22,21 @@ cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak # 根据发型版本选择相应 yum 镜像 if [[ ${version} == 5 ]]; then - # 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 + # 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 - # 根据实际发型版本情况替换 - detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'` - sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo + # 根据实际发型版本情况替换 + detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'` + sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo - # 不替换下面的开关,可能会出现错误:Could not open/read repomd.xml - sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo + # 不替换下面的开关,可能会出现错误:Could not open/read repomd.xml + sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo 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 - 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 - echo -e "版本不支持,替换 yum repo 失败" + echo -e "版本不支持,替换 yum repo 失败" fi # 更新缓存 diff --git a/codes/linux/sys/set-dns.sh b/codes/linux/sys/set-dns.sh index ff3c389..fe07f01 100644 --- a/codes/linux/sys/set-dns.sh +++ b/codes/linux/sys/set-dns.sh @@ -7,22 +7,22 @@ ################################################################################### ip='127.0.0.1' 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() { -echo -e "设置DNS服务器" -cat >> /etc/resolv.conf << EOF + echo -e "设置DNS服务器" + cat >> /etc/resolv.conf << EOF nameserver 114.114.114.114 nameserver 8.8.8.8 EOF } function setHosts() { -getDeviceIp -host=`hostname` -cat >> /etc/hosts << EOF -${ip} ${host} + getDeviceIp + host=`hostname` + cat >> /etc/hosts << EOF + ${ip} ${host} EOF } diff --git a/codes/linux/sys/sys-settings.sh b/codes/linux/sys/sys-settings.sh index e08c7d0..dcd5828 100644 --- a/codes/linux/sys/sys-settings.sh +++ b/codes/linux/sys/sys-settings.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash printHeadInfo() { -cat << EOF + cat << EOF ################################################################################### # Linux Centos7 系统配置脚本(根据需要选择) # @author: Zhang Peng @@ -11,47 +11,47 @@ EOF } setLimit() { -cat >> /etc/security/limits.conf << EOF + cat >> /etc/security/limits.conf << EOF * - nofile 65535 * - nproc 65535 EOF } setLang() { -cat > /etc/sysconfig/i18n << EOF + cat > /etc/sysconfig/i18n << EOF LANG="zh_CN.UTF-8" EOF } closeShutdownShortkey() { - echo "关闭 Ctrl+Alt+Del 快捷键防止重新启动" - sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf + echo "关闭 Ctrl+Alt+Del 快捷键防止重新启动" + sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf } closeSelinux() { - echo "关闭 selinux" + echo "关闭 selinux" - # see http://blog.51cto.com/13570193/2093299 - sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config + # see http://blog.51cto.com/13570193/2093299 + sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config } setBootMode() { - # 1. 停机(记得不要把 initdefault 配置为 0,因为这样会使 Linux 不能启动) - # 2. 单用户模式,就像 Win9X 下的安全模式 - # 3. 多用户,但是没有 NFS - # 4. 完全多用户模式,准则的运行级 - # 5. 通常不用,在一些特殊情况下可以用它来做一些事情 - # 6. X11,即进到 X-Window 系统 - # 7. 重新启动 (记得不要把 initdefault 配置为 6,因为这样会使 Linux 不断地重新启动) - echo "设置 Linux 启动模式" - sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab + # 1. 停机(记得不要把 initdefault 配置为 0,因为这样会使 Linux 不能启动) + # 2. 单用户模式,就像 Win9X 下的安全模式 + # 3. 多用户,但是没有 NFS + # 4. 完全多用户模式,准则的运行级 + # 5. 通常不用,在一些特殊情况下可以用它来做一些事情 + # 6. X11,即进到 X-Window 系统 + # 7. 重新启动 (记得不要把 initdefault 配置为 6,因为这样会使 Linux 不断地重新启动) + echo "设置 Linux 启动模式" + sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab } # 配置 IPv4 -configIpv4(){ -echo "配置 ipv4" +configIpv4() { + echo "配置 ipv4" -cat >> /etc/sysctl.conf << EOF + cat >> /etc/sysctl.conf << EOF net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 2 @@ -78,56 +78,57 @@ EOF # 关闭 IPv6 closeIpv6() { -echo "关闭 ipv6" + echo "关闭 ipv6" -cat > /etc/modprobe.d/ipv6.conf << EOF + cat > /etc/modprobe.d/ipv6.conf << EOF alias net-pf-10 off options ipv6 disable=1 EOF -echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network + echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network } # 入口函数 main() { -PS3="请选择要执行的操作:" -select ITEM in "设置 DNS" "设置 NTP" "关闭防火墙" "配置 IPv4" "关闭 IPv6" "全部执行" -do + PS3="请选择要执行的操作:" + select ITEM in "设置 DNS" "设置 NTP" "关闭防火墙" "配置 IPv4" "关闭 IPv6" "全部执行" + do -case ${ITEM} in - "设置 DNS") - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash - ;; - "设置 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/stop-firewall.sh | bash - ;; - "配置 IPv4") - configIpv4 - ;; - "关闭 IPv6") - 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-ntp.sh | bash - curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash - configIpv4 - closeIpv6 - ;; - *) - echo -e "输入项不支持!" - main - ;; -esac -break -done + case ${ITEM} in + "设置 DNS") + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/set-dns.sh | bash + ;; + "设置 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/stop-firewall.sh | bash + ;; + "配置 IPv4") + configIpv4 + ;; + "关闭 IPv6") + 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-ntp.sh | bash + curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/sys/stop-firewall.sh | bash + configIpv4 + closeIpv6 + ;; + *) + echo -e "输入项不支持!" + main + ;; + esac + break + done } ######################################## MAIN ######################################## -filepath=$(cd "$(dirname "$0")"; pwd) +filepath=$(cd "$(dirname "$0")"; +pwd) printHeadInfo main diff --git a/codes/linux/tool/Autoinstall_ELK_V1.3.sh b/codes/linux/tool/Autoinstall_ELK_V1.3.sh index 4d1f583..87557dd 100644 --- a/codes/linux/tool/Autoinstall_ELK_V1.3.sh +++ b/codes/linux/tool/Autoinstall_ELK_V1.3.sh @@ -1,4 +1,5 @@ #!/bin/bash + #mail:xuel@anchnet.com #data:2017/9/7 #AutoInstall ELK scripts @@ -9,65 +10,66 @@ echo "# Auto Install ELK. ##" echo "# Press Ctrl + C to cancel ##" echo "# Any key to continue ##" echo "##########################################" -read -p +read -p software_dir="/usr/local/software" 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" 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" -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` +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` 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_fun() { -if [ ! -d ${software_dir} ];then - mkdir -p ${software_dir} && cd ${software_dir} -else - cd ${software_dir} -fi -for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url -do - wget -c $software -done -clear + if [ ! -d ${software_dir} ]; then + mkdir -p ${software_dir} && cd ${software_dir} + else + cd ${software_dir} + fi + for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url + do + wget -c $software + done + clear } + #initial system:install java wget;set hostname;disable firewalld init_sys() { -[ -f /etc/init.d/functions ] && . /etc/init.d/functions -[ "${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 -sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config -setenforce 0 -yum install -y java-1.8.0-openjdk wget net-tools -hostnamectl set-hostname elk-server -systemctl stop firewalld -cat >>/etc/security/limits.conf<> /etc/security/limits.conf << EOF +* soft nofile 65536 +* hard nofile 65536 +* soft nGproc 65536 * hard nproc 65536 EOF } #install elasticsearch install_elasticsearch() { -cd $software_dir -tar zxf elasticsearch-5.4.1.tar.gz -mv elasticsearch-5.4.1 /usr/local/elasticsearch -mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs -useradd elasticsearch -chown -R elasticsearch:elasticsearch /usr/local/elasticsearch -echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p -if [ ${sys_mem} -eq 0 ];then - sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} - sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} -else - 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} -fi -cat >>/usr/local/elasticsearch/config/elasticsearch.yml<> /etc/sysctl.conf && sysctl -p + if [ ${sys_mem} -eq 0 ]; then + sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf} + sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf} + else + 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} + fi + cat >> /usr/local/elasticsearch/config/elasticsearch.yml << EOF cluster.name: my-application node.name: elk-server path.data: /usr/local/elasticsearch/data @@ -76,15 +78,15 @@ network.host: 127.0.0.1 http.port: 9200 discovery.zen.ping.unicast.hosts: ["elk-server"] EOF -su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &" + su - elasticsearch -c "nohup /usr/local/elasticsearch/bin/elasticsearch &" } #install logstash install_logstash() { -cd $software_dir -tar -zxf logstash-5.4.1.tar.gz -mv logstash-5.4.1 /usr/local/logstash -cat>/usr/local/logstash/config/01-syslog.conf< /usr/local/logstash/config/01-syslog.conf << EOF input { beats { port => "5044" @@ -97,15 +99,15 @@ output { stdout { codec => rubydebug } } 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() { -cd $software_dir -tar -zxf filebeat-5.4.1-linux-x86_64.tar.gz -mv filebeat-5.4.1-linux-x86_64 /usr/local/filebeat -cat >/usr/local/filebeat/filebeat.yml< /usr/local/filebeat/filebeat.yml << EOF filebeat.prospectors: - input_type: log paths: @@ -113,52 +115,53 @@ filebeat.prospectors: output.logstash: hosts: ["127.0.0.1:5044"] EOF -cd /usr/local/filebeat/ -nohup /usr/local/filebeat/filebeat & >/dev/null + cd /usr/local/filebeat/ + nohup /usr/local/filebeat/filebeat & > /dev/null } #install kibana install_kibana() { -cd $software_dir -tar -zxf kibana-5.4.1-linux-x86_64.tar.gz -mv kibana-5.4.1-linux-x86_64 /usr/local/kibana -cat >> /usr/local/kibana/config/kibana.yml <> /usr/local/kibana/config/kibana.yml << EOF server.port: 5601 server.host: "0.0.0.0" elasticsearch.url: "http://127.0.0.1:9200" EOF -nohup /usr/local/kibana/bin/kibana & >/dev/null + nohup /usr/local/kibana/bin/kibana & > /dev/null } check() { -port=$1 -program=$2 -check_port=`netstat -lntup|grep ${port}|wc -l` -check_program=`ps -ef|grep ${program}|grep -v grep|wc -l` -if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then + port=$1 + program=$2 + check_port=`netstat -lntup | grep ${port} | wc -l` + check_program=`ps -ef | grep ${program} | grep -v grep | wc -l` + if [ $check_port -gt 0 ] && [ $check_program -gt 0 ]; then action "${program} run is ok!" /bin/true -else + else action "${program} run is error!" /bin/false -fi + fi } main() { -init_sys -wget_fun -install_elasticsearch -install_filebeat -install_logstash -install_kibana -echo -e "\033[32m Checking Elasticsearch...\033[0m" -sleep 20 -check :9200 "elasticsearch" -echo -e "\033[32m Checking Logstash...\033[0m" -sleep 2 -check ":9600" "logstash" -echo -e "\033[32m Checking Kibana...\033[0m" -sleep 2 -check ":5601" "kibana" -action "ELK install is success!" /bin/true -echo "url:http://$IP:5601" + init_sys + wget_fun + install_elasticsearch + install_filebeat + install_logstash + install_kibana + echo -e "\033[32m Checking Elasticsearch...\033[0m" + sleep 20 + check :9200 "elasticsearch" + echo -e "\033[32m Checking Logstash...\033[0m" + sleep 2 + check ":9600" "logstash" + echo -e "\033[32m Checking Kibana...\033[0m" + sleep 2 + check ":5601" "kibana" + action "ELK install is success!" /bin/true + echo "url:http://$IP:5601" } + main diff --git a/codes/linux/tool/Cpu_Limit.sh b/codes/linux/tool/Cpu_Limit.sh index 1d959da..5e082e0 100644 --- a/codes/linux/tool/Cpu_Limit.sh +++ b/codes/linux/tool/Cpu_Limit.sh @@ -1,4 +1,5 @@ #!/bin/bash + # auth:kaliarch # func:sys info check # version:v1.0 @@ -16,38 +17,38 @@ LIMIT_CPU=85 LOG_DIR=/var/log/cpulimit/ # 超过阀值进程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) install_cpulimit() { - [ ! -d /tmp ] && mkdir /tmp || cd /tmp - wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz - tar -zxf v0.2.tar.gz - cd cpulimit-0.2 && make - [ $? -eq 0 ] && cp src/cpulimit /usr/bin/ + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + wget -c https://github.com/opsengine/cpulimit/archive/v0.2.tar.gz + tar -zxf v0.2.tar.gz + cd cpulimit-0.2 && make + [ $? -eq 0 ] && cp src/cpulimit /usr/bin/ } do_cpulimit() { -[ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR} -for i in ${PIDARG}; -do - MSG=$(ps -aux |awk -v pid=$i '{if($2 == pid) print $0}') + [ ! -d ${LOG_DIR} ] && mkdir -p ${LOG_DIR} + for i in ${PIDARG}; + do + MSG=$(ps -aux | awk -v pid=$i '{if($2 == pid) print $0}') echo ${MSG} - [ ! -d /tmp ] && mkdir /tmp || cd /tmp - nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} & + [ ! -d /tmp ] && mkdir /tmp || cd /tmp + nohup ${CPULIMITCMD} -p $i -l ${LIMIT_CPU} & echo "$(date) -- ${MSG}" >> ${LOG_DIR}$(date +%F).log -done + done } main() { - hash cpulimit - if [ $? -eq 0 ];then - do_cpulimit - else - install_cpulimit && do_cpulimit - fi + hash cpulimit + if [ $? -eq 0 ]; then + do_cpulimit + else + install_cpulimit && do_cpulimit + fi } main diff --git a/codes/linux/tool/Custom_Rm.sh b/codes/linux/tool/Custom_Rm.sh index 75a06d3..8b948ee 100644 --- a/codes/linux/tool/Custom_Rm.sh +++ b/codes/linux/tool/Custom_Rm.sh @@ -1,4 +1,5 @@ #!/bin/bash + # function:自定义rm命令,每天晚上定时清理 CMD_SCRIPTS=$HOME/.rm_scripts.sh @@ -7,7 +8,7 @@ CRON_FILE=/var/spool/cron/root BASHRC=$HOME/.bashrc [ ! -d ${TRASH_DIR} ] && mkdir -p ${TRASH_DIR} -cat > $CMD_SCRIPTS < $CMD_SCRIPTS << EOF PARA_CNT=\$# TRASH_DIR=$TRASH_DIR for i in \$*; do diff --git a/codes/linux/tool/Daily_Archive.sh b/codes/linux/tool/Daily_Archive.sh index 7e614b5..9fbaafe 100644 --- a/codes/linux/tool/Daily_Archive.sh +++ b/codes/linux/tool/Daily_Archive.sh @@ -1,4 +1,5 @@ #!/bin/bash + # # Daily_Archive - Archive designated files & directories ###################################################### @@ -12,7 +13,7 @@ DATE=`date +%y%m%d` FILE=archive$DATE.tar.gz # # Set Configuration and Destination File -# +# CONFIG_FILE=/home/tiandi/archive/Files_To_Backup DESTINATION=/home/tiandi/archive/$FILE # @@ -20,46 +21,46 @@ DESTINATION=/home/tiandi/archive/$FILE # # 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 - echo + echo else - echo - echo "$CONFIG_FILE does not exist." - echo "Backup not completed due to missing Configuration file" - echo - exit + echo + echo "$CONFIG_FILE does not exist." + echo "Backup not completed due to missing Configuration file" + echo + exit fi # # Build the names of all the files to backup # -FILE_NO=1 # Start on Line 1 of Config file. -exec < $CONFIG_FILE # Redirect Std Input to name of Config File +FILE_NO=1 # Start on Line 1 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 ] do - # Make sure the file or directory exists. - if [ -f $FILE_NAME -o -d $FILE_NAME ] - then - # If file exists, add its name to the lists - FILE_LIST="$FILE_LIST $FILE_NAME" - else - # If file doesn't exist, issue warning - echo - echo "$FILE_NAME, does not exist." - echo "Obviously, I will not include it in this archive." - echo "It is listed on line $FILE_NO of the config file." - echo "Continuing to build archive file." - echo - fi -# - FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one - read FILE_NAME # Read next record. + # Make sure the file or directory exists. + if [ -f $FILE_NAME -o -d $FILE_NAME ] + then + # If file exists, add its name to the lists + FILE_LIST="$FILE_LIST $FILE_NAME" + else + # If file doesn't exist, issue warning + echo + echo "$FILE_NAME, does not exist." + echo "Obviously, I will not include it in this archive." + echo "It is listed on line $FILE_NO of the config file." + echo "Continuing to build archive file." + echo + fi + # + FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one + read FILE_NAME # Read next record. done ########################################################### -# +# # Backup the files and Compress Archive # tar -czf $DESTINATION $FILE_LIST 2> /dev/null diff --git a/codes/linux/tool/Hourly_Archive.sh b/codes/linux/tool/Hourly_Archive.sh index e0396f4..d29a08e 100644 --- a/codes/linux/tool/Hourly_Archive.sh +++ b/codes/linux/tool/Hourly_Archive.sh @@ -1,10 +1,11 @@ #!/bin/bash + # # Hourly_Archive - Every hour create an archive ###################################################### # # Set Configuration and Destination File -# +# CONFIG_FILE=/home/tiandi/archive/Files_To_Backup # # Gather Current Date,Month & Time @@ -27,46 +28,46 @@ DESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIME.tar.gz # # 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 - echo + echo else - echo - echo "$CONFIG_FILE does not exist." - echo "Backup not completed due to missing Configuration file" - echo - exit + echo + echo "$CONFIG_FILE does not exist." + echo "Backup not completed due to missing Configuration file" + echo + exit fi # # Build the names of all the files to backup # -FILE_NO=1 # Start on Line 1 of Config file. -exec < $CONFIG_FILE # Redirect Std Input to name of Config File +FILE_NO=1 # Start on Line 1 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 ] do - # Make sure the file or directory exists. - if [ -f $FILE_NAME -o -d $FILE_NAME ] - then - # If file exists, add its name to the lists - FILE_LIST="$FILE_LIST $FILE_NAME" - else - # If file doesn't exist, issue warning - echo - echo "$FILE_NAME, does not exist." - echo "Obviously, I will not include it in this archive." - echo "It is listed on line $FILE_NO of the config file." - echo "Continuing to build archive file." - echo - fi -# - FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one - read FILE_NAME # Read next record. + # Make sure the file or directory exists. + if [ -f $FILE_NAME -o -d $FILE_NAME ] + then + # If file exists, add its name to the lists + FILE_LIST="$FILE_LIST $FILE_NAME" + else + # If file doesn't exist, issue warning + echo + echo "$FILE_NAME, does not exist." + echo "Obviously, I will not include it in this archive." + echo "It is listed on line $FILE_NO of the config file." + echo "Continuing to build archive file." + echo + fi + # + FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one + read FILE_NAME # Read next record. done ########################################################### -# +# # Backup the files and Compress Archive # tar -czf $DESTINATION $FILE_LIST 2> /dev/null diff --git a/codes/linux/tool/删除用户脚本.sh b/codes/linux/tool/删除用户脚本.sh index 724dfc0..2da94f7 100644 --- a/codes/linux/tool/删除用户脚本.sh +++ b/codes/linux/tool/删除用户脚本.sh @@ -1,85 +1,92 @@ #!/bin/bash + # # Delete_User - Automates the 4 steps to remove an account # ################################################################# -# +# # Define Functions # ################################################################# function get_answer { -# - unset ANSWER - ASK_COUNT=0 -# - while [ -z "$ANSWER" ] # while no answer is given, keep asking - do - ASK_COUNT=$[ $ASK_COUNT + 1 ] -# - case $ASK_COUNT in # If user gives no answer in time allowed - 2) - echo - echo "Please answer the question." - echo - ;; - 3) - echo - echo "One last try... please answer the question." - echo - ;; - 4) - echo - echo "Since you refuse to answer the question..." - echo "exiting program." - echo - # - exit - ;; - esac -# - echo -# - if [ -n "$LINE2" ] - then - echo $LINE1 # Print 2 lines - echo -e $LINE2" \c" - else # Print 1 line - echo -e $LINE1" \c" - fi -# -# Allow 60 seconds to answer before time-out - read -t 60 ANSWER - done -# -# Do a little variable clean-up -# - unset LINE1 - unset LINE2 -# -} #end of get_answer function + # + unset ANSWER + ASK_COUNT=0 + # + while [ -z "$ANSWER" ] # while no answer is given, keep asking + do + ASK_COUNT=$[ $ASK_COUNT + 1 ] + # + case $ASK_COUNT in + # If user gives no answer in time allowed + 2) + echo + echo "Please answer the question." + echo + ;; + 3) + echo + echo "One last try... please answer the question." + echo + ;; + 4) + echo + echo "Since you refuse to answer the question..." + echo "exiting program." + echo + # + exit + ;; + esac + # + echo + # + if [ -n "$LINE2" ] + then + echo $LINE1 # Print 2 lines + echo -e $LINE2" \c" + else + # Print 1 line + echo -e $LINE1" \c" + fi + # + # Allow 60 seconds to answer before time-out + read -t 60 ANSWER + done + # + # Do a little variable clean-up + # + unset LINE1 + unset LINE2 + # +} + +#end of get_answer function # ################################################################# function process_answer { -# - case $ANSWER in - y|Y|YES|yes|yEs|yeS|YEs|yES) - # If user answers "yes".do nothing. - ;; - *) - # If user answers anything but "yes", exit script - echo - echo $EXIT_LINE1 - echo $EXIT_LINE2 - echo - exit - ;; - esac - # - # Do a little variable clean-up - unset EXIT_LINE1 - unset EXIT_LINE2 -# -} #End of process_answer function + # + case $ANSWER in + y | Y | YES | yes | yEs | yeS | YEs | yES) + # If user answers "yes".do nothing. + ;; + *) + # If user answers anything but "yes", exit script + echo + echo $EXIT_LINE1 + echo $EXIT_LINE2 + echo + exit + ;; + esac + # + # Do a little variable clean-up + unset EXIT_LINE1 + unset EXIT_LINE2 + # +} + +#End of process_answer function # ################################################################ # @@ -108,13 +115,13 @@ get_answer # 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 - echo - echo "Account, $USER_ACCOUNT, not found." - echo "Leaving the script..." - echo - exit + echo + echo "Account, $USER_ACCOUNT, not found." + echo "Leaving the script..." + echo + exit fi # echo @@ -143,59 +150,59 @@ echo echo "$USER_ACCOUNT has the following processes running: " echo # -ps -u $USER_ACCOUNT #List the processes running +ps -u $USER_ACCOUNT #List the processes running # case $? in -1) # No processes running for this User Account - # - echo "There are no processes for this account currently running." - echo -;; -0) # Processes running for this User Account. - # Ask Script User if wants us to kill the processes. - # - unset ANSWER # I think this line is not needed - LINE1="Would you like me to kill the process(es)? [y/n]:" - get_answer - # - case $ANSWER in - y|Y|YES|yes|Yes|yEs|yeS|YEs|yES) # if user answer "yes", - #kill User Account processes - # - echo - # - # Clean-up temp file upon signals - # - trap "rm $USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT - # - # List user processes running - ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt - # - exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input - # - read USER_PROCESS_REC # First record will be blank - read USER_PROCESS_REC - # - while [ $? -eq 0 ] - do - # obtain PID - USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1 ) - kill -9 $USER_PID - echo "Killed process $USER_PID" - read USER_PROCESS_REC - done - # - echo - # - rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report - ;; - *) # If user answers anything but "yes", do not kill. - echo - echo "Will not kill the process(es)." - echo - ;; - esac -;; + 1) # No processes running for this User Account + # + echo "There are no processes for this account currently running." + echo + ;; + 0) # Processes running for this User Account. + # Ask Script User if wants us to kill the processes. + # + unset ANSWER # I think this line is not needed + LINE1="Would you like me to kill the process(es)? [y/n]:" + get_answer + # + case $ANSWER in + y | Y | YES | yes | Yes | yEs | yeS | YEs | yES) # if user answer "yes", + #kill User Account processes + # + echo + # + # Clean-up temp file upon signals + # + trap "rm$USER_ACCOUNT_Running_Process.rpt" SIGTERM SIGINT SIGQUIT + # + # List user processes running + ps -u $USER_ACCOUNT > $USER_ACCOUNT_Running_Process.rpt + # + exec < $USER_ACCOUNT_Running_Process.rpt # Make report Std Input + # + read USER_PROCESS_REC # First record will be blank + read USER_PROCESS_REC + # + while [ $? -eq 0 ] + do + # obtain PID + USER_PID=$(echo $USER_PROCESS_REC | cut -d " " -f1) + kill -9 $USER_PID + echo "Killed process $USER_PID" + read USER_PROCESS_REC + done + # + echo + # + rm $USER_ACCOUNT_Running_Process.rpt # Remove temp report + ;; + *) # If user answers anything but "yes", do not kill. + echo + echo "Will not kill the process(es)." + echo + ;; + esac + ;; esac ################################################################################### # @@ -216,7 +223,7 @@ echo "Please wait. This may take a while..." REPORT_DATE=`date +%y%m%d` 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 "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..." process_answer # -userdel $USER_ACCOUNT # delete user account +userdel $USER_ACCOUNT # delete user account echo echo "User account, $USER_ACCOUNT, has been removed" echo diff --git a/codes/linux/tool/查看指定目录磁盘使用情况.sh b/codes/linux/tool/查看指定目录磁盘使用情况.sh index 4639c44..d3c46ea 100644 --- a/codes/linux/tool/查看指定目录磁盘使用情况.sh +++ b/codes/linux/tool/查看指定目录磁盘使用情况.sh @@ -9,11 +9,11 @@ echo "Top Ten Disk Space Usage" echo "for ${DIRS} Directories" for DIR in ${DIRS} do - echo "" - echo "The ${DIR} Directory:" - du -S ${DIR} 2>/dev/null | - sort -rn | - sed '{11,$D; =}' | - sed 'N; s/\n/ /' | - gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' + echo "" + echo "The ${DIR} Directory:" + du -S ${DIR} 2> /dev/null | + sort -rn | + sed '{11,$D; =}' | + sed 'N; s/\n/ /' | + gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}' done diff --git a/codes/shell/action/oper/input.sh b/codes/shell/action/oper/input.sh index 29ad918..037aa91 100644 --- a/codes/shell/action/oper/input.sh +++ b/codes/shell/action/oper/input.sh @@ -7,19 +7,19 @@ ################### 读取脚本输入参数并校验 ################### declare -a serial -serial=(start stop restart) +serial=( start stop restart ) echo -n "请选择操作(可选值:start|stop|restart):" -read oper +read oper if ! echo ${serial[@]} | grep -q ${oper}; then - echo "请选择正确操作(可选值:start|stop|restart)" - exit 1 + echo "请选择正确操作(可选值:start|stop|restart)" + exit 1 fi declare -a serial2 -serial2=(dev test prod) +serial2=( dev test prod ) echo -n "请选择运行环境(可选值:dev|test|prod):" -read profile +read profile if ! echo ${serial2[@]} | grep -q ${profile}; then - echo "请选择正确运行环境(可选值:dev|test|prod)" - exit 1 + echo "请选择正确运行环境(可选值:dev|test|prod)" + exit 1 fi diff --git a/codes/shell/action/system/dir.sh b/codes/shell/action/system/dir.sh index b9499d0..8a3b2b7 100644 --- a/codes/shell/action/system/dir.sh +++ b/codes/shell/action/system/dir.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash -current_dir=$(cd `dirname $0`; pwd) +current_dir=$(cd `dirname $0`; +pwd) echo "当前目录是:${current_dir}" parent_dir=$(dirname $(pwd)) diff --git a/codes/shell/demos/array-demo.sh b/codes/shell/demos/array-demo.sh index 65ff2ef..5c412e2 100644 --- a/codes/shell/demos/array-demo.sh +++ b/codes/shell/demos/array-demo.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # 创建数组 -nums=([2]=2 [0]=0 [1]=1) -colors=(red yellow "dark blue") +nums=( [ 2 ] = 2 [ 0 ] = 0 [ 1 ] = 1 ) +colors=( red yellow "dark blue" ) # 访问数组的单个元素 echo ${nums[1]} @@ -43,13 +43,13 @@ echo ${#nums[*]} # 3 # 向数组中添加元素 -colors=(white "${colors[@]}" green black) +colors=( white "${colors[@]}" green black ) echo ${colors[@]} # Output: # white red yellow dark blue green black # 从数组中删除元素 -unset nums[0] +unset nums[ 0 ] echo ${nums[@]} # Output: # 1 2 diff --git a/codes/shell/demos/comment-demo.sh b/codes/shell/demos/comment-demo.sh index 0a5e421..098e1bc 100644 --- a/codes/shell/demos/comment-demo.sh +++ b/codes/shell/demos/comment-demo.sh @@ -9,7 +9,7 @@ ########## 这是分割线 ########## -:< ${y} ]]; then - echo "${x} > ${y}" + echo "${x} > ${y}" elif [[ ${x} < ${y} ]]; then - echo "${x} < ${y}" + echo "${x} < ${y}" else - echo "${x} = ${y}" + echo "${x} = ${y}" fi # Output: 10 < 20 diff --git a/codes/shell/demos/statement/select-demo.sh b/codes/shell/demos/statement/select-demo.sh index 956ac8f..4e59b5a 100644 --- a/codes/shell/demos/statement/select-demo.sh +++ b/codes/shell/demos/statement/select-demo.sh @@ -5,10 +5,10 @@ select ITEM in bower npm gem pip do echo -n "Enter the package name: " && read PACKAGE case ${ITEM} in - bower) bower install ${PACKAGE} ;; - npm) npm install ${PACKAGE} ;; - gem) gem install ${PACKAGE} ;; - pip) pip install ${PACKAGE} ;; + bower) bower install ${PACKAGE} ;; + npm) npm install ${PACKAGE} ;; + gem) gem install ${PACKAGE} ;; + pip) pip install ${PACKAGE} ;; esac break # 避免无限循环 done diff --git a/codes/shell/demos/statement/until-demo.sh b/codes/shell/demos/statement/until-demo.sh index 320b590..58a0fce 100644 --- a/codes/shell/demos/statement/until-demo.sh +++ b/codes/shell/demos/statement/until-demo.sh @@ -2,8 +2,8 @@ x=0 until [[ ${x} -ge 5 ]]; do - echo ${x} - x=`expr ${x} + 1` + echo ${x} + x=`expr ${x} + 1` done # Output: # 0 diff --git a/codes/shell/demos/statement/while-demo.sh b/codes/shell/demos/statement/while-demo.sh index 375bed9..9492b0a 100644 --- a/codes/shell/demos/statement/while-demo.sh +++ b/codes/shell/demos/statement/while-demo.sh @@ -4,8 +4,8 @@ x=0 ### x小于10 while [[ ${x} -lt 10 ]]; do - echo $((x * x)) - x=$((x + 1)) + echo $((x * x)) + x=$((x + 1)) done # Output: # 0 diff --git a/codes/shell/demos/string-demo.sh b/codes/shell/demos/string-demo.sh index 652c57a..0594f6b 100644 --- a/codes/shell/demos/string-demo.sh +++ b/codes/shell/demos/string-demo.sh @@ -47,8 +47,8 @@ echo "key is ${key}" ################### 判断字符串中是否包含子字符串 ################### result=$(echo "${str}" | grep "feature/") -if [[ "$result" != "" ]] ; then - echo "feature/ 是 ${str} 的子字符串" +if [[ "$result" != "" ]]; then + echo "feature/ 是 ${str} 的子字符串" else - echo "feature/ 不是 ${str} 的子字符串" + echo "feature/ 不是 ${str} 的子字符串" fi diff --git a/codes/shell/demos/variable-demo.sh b/codes/shell/demos/variable-demo.sh index 3cb6fdf..3305ce6 100644 --- a/codes/shell/demos/variable-demo.sh +++ b/codes/shell/demos/variable-demo.sh @@ -13,10 +13,10 @@ readonly rword # rword="bye" # 如果放开注释,执行时会报错 ################### 删除变量 ################### -dword="hello" # 声明变量 -echo ${dword} # 输出变量值 +dword="hello" # 声明变量 +echo ${dword} # 输出变量值 # Output: hello -unset dword # 删除变量 +unset dword # 删除变量 echo ${dword} # Output: (空) diff --git a/codes/shell/示例脚本/mysql/SQL批处理执行脚本.sh b/codes/shell/示例脚本/mysql/SQL批处理执行脚本.sh index e74299d..39d40f4 100644 --- a/codes/shell/示例脚本/mysql/SQL批处理执行脚本.sh +++ b/codes/shell/示例脚本/mysql/SQL批处理执行脚本.sh @@ -6,7 +6,7 @@ database='test' for f in `ls */*.sql` do -echo ${f}; -mysql -u${user} -p${password} -f ${database} -e "source $f"; + echo ${f}; + mysql -u${user} -p${password} -f ${database} -e "source $f"; done echo 'OK!' diff --git a/codes/shell/示例脚本/mysql/向数据库中插入数据.sh b/codes/shell/示例脚本/mysql/向数据库中插入数据.sh index c2eaef4..73c73d1 100644 --- a/codes/shell/示例脚本/mysql/向数据库中插入数据.sh +++ b/codes/shell/示例脚本/mysql/向数据库中插入数据.sh @@ -1,21 +1,22 @@ #!/bin/bash + # send data to the the table in the MYSQL database MYSQL=`which mysql` if [ $# -ne 2 ] then - echo "Usage:mtest2 emplid lastname firstname salary" + echo "Usage:mtest2 emplid lastname firstname salary" else - #脚本变量一定要用双引号,字符串变量使用单引号 - statement=" insert into em_admin values(NULL, '$1', $2)" - $MYSQL emwjs -u test < $temp - dialog --textbox $temp 20 60 + df -k > $temp + dialog --textbox $temp 20 60 } function whoseon { - who > $temp - dialog --textbox $temp 20 50 + who > $temp + dialog --textbox $temp 20 50 } function menusage { - cat /proc/meminfo > $temp - dialog --textbox $temp 20 50 + cat /proc/meminfo > $temp + dialog --textbox $temp 20 50 } while [ 1 ] do - dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2> $temp2 - if [ $? -eq 1 ] - then - break - fi + dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2> $temp2 + if [ $? -eq 1 ] + then + break + fi - selection=`cat $temp2` + selection=`cat $temp2` - case $selection in - 1) - diskspace;; - 2) - whoseon;; - 3) - menusage;; - 0) - break;; - *) - dialog --msgbox "Sorry,invalid selection" 10 30 - esac + case $selection in + 1) + diskspace ;; + 2) + whoseon ;; + 3) + menusage ;; + 0) + break ;; + *) + dialog --msgbox "Sorry,invalid selection" 10 30 + esac done rm -f $temp 2> /dev/null rm -f $temp2 2> /dev/null diff --git a/codes/shell/示例脚本/输入和输出/临时重定向.sh b/codes/shell/示例脚本/输入和输出/临时重定向.sh index 4681abe..fce0459 100644 --- a/codes/shell/示例脚本/输入和输出/临时重定向.sh +++ b/codes/shell/示例脚本/输入和输出/临时重定向.sh @@ -1,4 +1,5 @@ #!/bin/bash + # testing STDERR messages echo "This is an error " >&2 diff --git a/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh b/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh index 015c852..2115cc7 100644 --- a/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh +++ b/codes/shell/示例脚本/输入和输出/从文件中读取数据.sh @@ -1,11 +1,12 @@ #!/bin/bash + # reading data from a file count=1 cat test | while read line do - echo "Line $count: $line" - count=$[ $count + 1 ] + echo "Line $count: $line" + count=$[ $count + 1 ] done echo "Finished processing the file" diff --git a/codes/shell/示例脚本/输入和输出/使用getopts.sh b/codes/shell/示例脚本/输入和输出/使用getopts.sh index 6338939..1a48671 100644 --- a/codes/shell/示例脚本/输入和输出/使用getopts.sh +++ b/codes/shell/示例脚本/输入和输出/使用getopts.sh @@ -1,12 +1,13 @@ #!/bin/bash + # simple demonstration of the getopts command while getopts :ab:c opt do - case "$opt" in - a) echo "Found the -a option";; - b) echo "Found the -b option, with value $OPTARG";; - c) echo "Found the -c option";; - *) echo "Unknown option:$opt";; - esac + case "$opt" in + a) echo "Found the -a option" ;; + b) echo "Found the -b option, with value $OPTARG" ;; + c) echo "Found the -c option" ;; + *) echo "Unknown option:$opt" ;; + esac done diff --git a/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh b/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh index 732ac69..8bcb583 100644 --- a/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh +++ b/codes/shell/示例脚本/输入和输出/使用getopts处理选项和参数.sh @@ -1,20 +1,21 @@ #!/bin/bash + # processing options and parameters with getopts while getopts :ab:cd opt do - case "$opt" in - a) echo "Found the -a option";; - b) echo "Found the -b option,with value $OPTARG";; - c) echo "Found the -c option";; - d) echo "Found the -d option";; - *) echo "Unknown option: $opt";; - esac + case "$opt" in + a) echo "Found the -a option" ;; + b) echo "Found the -b option,with value $OPTARG" ;; + c) echo "Found the -c option" ;; + d) echo "Found the -d option" ;; + *) echo "Unknown option: $opt" ;; + esac done shift $[ $OPTIND - 1 ] count=1 for param in "$@" do - echo "Parameter $count: $param" - count=$[ $count + 1 ] + echo "Parameter $count: $param" + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/使用getopt命令.sh b/codes/shell/示例脚本/输入和输出/使用getopt命令.sh index 55c9867..999b235 100644 --- a/codes/shell/示例脚本/输入和输出/使用getopt命令.sh +++ b/codes/shell/示例脚本/输入和输出/使用getopt命令.sh @@ -1,25 +1,26 @@ #!/bin/bash + #extracting command line options and values with getopt # getopt command is not goot at dealing with space,we can use getopts set -- `getopt -q ab:c "$@"` while [ -n "$1" ] do - case "$1" in - -a) echo "Found the -a option";; - -b) param="$2" - echo "Found the -b option,with parameter value $param" - shift;; - -c) echo "Found the -c option";; - --) shift - break;; - *) echo "$1 is not an option";; - esac - shift + case "$1" in + -a) echo "Found the -a option" ;; + -b) param="$2" + echo "Found the -b option,with parameter value $param" + shift ;; + -c) echo "Found the -c option" ;; + --) shift + break ;; + *) echo "$1 is not an option" ;; + esac + shift done count=1 for param in "$@" do - echo "Parameter #$count: $param" - count=$[ $count+1 ] + echo "Parameter #$count: $param" + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/使用shift命令.sh b/codes/shell/示例脚本/输入和输出/使用shift命令.sh index 11877cf..0f249e4 100644 --- a/codes/shell/示例脚本/输入和输出/使用shift命令.sh +++ b/codes/shell/示例脚本/输入和输出/使用shift命令.sh @@ -1,12 +1,13 @@ #!/bin/bash + # shift n 移动变量 count=1 while [ -n "$1" ] do - echo "Parameter #$count = $1" - count=$[ $count+1 ] - shift + echo "Parameter #$count = $1" + count=$[ $count + 1 ] + shift done echo -e "\n" diff --git a/codes/shell/示例脚本/输入和输出/分离参数和选项.sh b/codes/shell/示例脚本/输入和输出/分离参数和选项.sh index ee3da94..1e20cf7 100644 --- a/codes/shell/示例脚本/输入和输出/分离参数和选项.sh +++ b/codes/shell/示例脚本/输入和输出/分离参数和选项.sh @@ -4,20 +4,20 @@ while [ -n "$1" ] do - case "$1" in - -a) echo "Found the -a option";; - -b) echo "Found the -b option";; - -c) echo "Found the -c option";; - --) shift - break;; - *) echo "$1 is not an option";; - esac - shift + case "$1" in + -a) echo "Found the -a option" ;; + -b) echo "Found the -b option" ;; + -c) echo "Found the -c option" ;; + --) shift + break ;; + *) echo "$1 is not an option" ;; + esac + shift done count=1 for param in $@ do - echo "Parameter #$count: $param" - count=$[ $count + 1 ] + echo "Parameter #$count: $param" + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh b/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh index 417bfa4..7df0bfd 100644 --- a/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh +++ b/codes/shell/示例脚本/输入和输出/列出当前脚本打开的文件描述符.sh @@ -2,9 +2,9 @@ # testing lsof with file descriptors -exec 3>test -exec 6>test -exec 7 test +exec 6> test +exec 7< test lsof -a -p $$ -d0,1,2,3,6,7 diff --git a/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh b/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh index 6d15a9d..8659114 100644 --- a/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh +++ b/codes/shell/示例脚本/输入和输出/创建本地临时文件.sh @@ -4,7 +4,7 @@ tempfile=`mktemp test.XXXXXX` -exec 3>$tempfile +exec 3> $tempfile echo "This script writes to temp file $tempfile" @@ -18,5 +18,5 @@ echo "Done creating temp file. The contents are:" cat $tempfile -rm -f $tempfile 2>/dev/null +rm -f $tempfile 2> /dev/null diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh index 0face23..ba8cf18 100644 --- a/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/从以重定向的文件描述符中恢复.sh @@ -3,7 +3,7 @@ #storing STDOUT, then coming back to it exec 3>&1 -exec 1>test +exec 1> test echo "This should store in output file" echo "along with this line" diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh index 154e2bf..4d091cf 100644 --- a/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/关闭文件描述符.sh @@ -1,7 +1,8 @@ #!/bin/bash + # testing closing file descriptors -exec 3>test +exec 3> test echo "This is a test line of data" >&3 # closing file descriptor @@ -12,5 +13,5 @@ echo "This won't work" >&3 cat test #覆盖前一个test文件 -exec 3>test +exec 3> test echo "This'll be bad" >&3 diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh index 53b7645..c8e25d1 100644 --- a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建读写文件描述符.sh @@ -1,4 +1,5 @@ #!/bin/bash + # testing inpiut/output file descriptor exec 3<> test diff --git a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh index c47be1b..df248e9 100644 --- a/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh +++ b/codes/shell/示例脚本/输入和输出/创建自己的重定向/创建输入文件描述符.sh @@ -1,22 +1,23 @@ #!/bin/bash + # redirecting input file descriptors exec 3>&1 echo "This is the 3 file descriptor" >&3 exec 6>&0 -exec 0test +exec 3> test echo "This should display on the monitor" echo "and this should be stored in the file" >&3 diff --git a/codes/shell/示例脚本/输入和输出/参数计数.sh b/codes/shell/示例脚本/输入和输出/参数计数.sh index 65673e8..1ed6071 100644 --- a/codes/shell/示例脚本/输入和输出/参数计数.sh +++ b/codes/shell/示例脚本/输入和输出/参数计数.sh @@ -1,4 +1,5 @@ #!/bin/bash + # getting the number of parameters echo There were $# parameters supplied diff --git a/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh b/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh index a4497a1..90ebad0 100644 --- a/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh +++ b/codes/shell/示例脚本/输入和输出/在tmp目录创建临时文件.sh @@ -7,6 +7,6 @@ tempfile=`mktemp -t tmp.XXXXXX` echo "This is a test file" > $tempfile echo "This is the second line of the test" >> $tempfile -echo ”The temp is locate at : $tempfile“ +echo ” The temp is locate at : $tempfile “ cat $tempfile rm -f $tempfile diff --git a/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh b/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh index c3105fe..d95a368 100644 --- a/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh +++ b/codes/shell/示例脚本/输入和输出/在脚本中使用重定向输入.sh @@ -1,4 +1,5 @@ #!/bin/bash + # redirecting the inpiut # 从test中读取数据,而不是从STDIN中读取数据 @@ -6,7 +7,7 @@ exec 0< test count=1 while read line do - echo "Line #$count : $line " - count=$[ $count +1 ] + echo "Line #$count : $line " + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/处理带值的选项.sh b/codes/shell/示例脚本/输入和输出/处理带值的选项.sh index b8cc631..63163ed 100644 --- a/codes/shell/示例脚本/输入和输出/处理带值的选项.sh +++ b/codes/shell/示例脚本/输入和输出/处理带值的选项.sh @@ -3,23 +3,23 @@ # extracting command line options and values while [ -n "$1" ] -do - case "$1" in - -a) echo "Found the -a option";; - -b) param="$2" - echo "Found the -b option, with parameter value $param" - shift;; - -c) echo "Found the -c option";; - --) shift - break;; - *) echo "$1 is not an option";; - esac - shift +do + case "$1" in + -a) echo "Found the -a option" ;; + -b) param="$2" + echo "Found the -b option, with parameter value $param" + shift ;; + -c) echo "Found the -c option" ;; + --) shift + break ;; + *) echo "$1 is not an option" ;; + esac + shift done count=1 for param in "$@" do - echo "Parameter #$count : $param" - count=$[ $count + 1 ] + echo "Parameter #$count : $param" + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/处理简单选项.sh b/codes/shell/示例脚本/输入和输出/处理简单选项.sh index 60dd404..9e67995 100644 --- a/codes/shell/示例脚本/输入和输出/处理简单选项.sh +++ b/codes/shell/示例脚本/输入和输出/处理简单选项.sh @@ -1,13 +1,14 @@ #!/bin/bash + # extracting command line options as parameters while [ -n "$1" ] do - case "$1" in - -a) echo "Found the -a option";; - -b) echo "Found the -b optins";; - -c) echo "Found the -c optins";; - *) echo "$1 is not a valid options";; - esac - shift + case "$1" in + -a) echo "Found the -a option" ;; + -b) echo "Found the -b optins" ;; + -c) echo "Found the -c optins" ;; + *) echo "$1 is not a valid options" ;; + esac + shift done diff --git a/codes/shell/示例脚本/输入和输出/抓取所有数据.sh b/codes/shell/示例脚本/输入和输出/抓取所有数据.sh index a93ccf0..3807526 100644 --- a/codes/shell/示例脚本/输入和输出/抓取所有数据.sh +++ b/codes/shell/示例脚本/输入和输出/抓取所有数据.sh @@ -1,16 +1,17 @@ #!/bin/bash + # testing $* and $@ count=1 for param in "$*" do - echo "\$* Parameter #$count = $param" - count=$[ $count+1 ] + echo "\$* Parameter #$count = $param" + count=$[ $count + 1 ] done count=1 for param in "$@" do - echo "\$@ Paramenter #$count = $param" - count=$[ $count+1 ] + echo "\$@ Paramenter #$count = $param" + count=$[ $count + 1 ] done diff --git a/codes/shell/示例脚本/输入和输出/永久重定向.sh b/codes/shell/示例脚本/输入和输出/永久重定向.sh index d03a3bd..ee7a5a0 100644 --- a/codes/shell/示例脚本/输入和输出/永久重定向.sh +++ b/codes/shell/示例脚本/输入和输出/永久重定向.sh @@ -1,14 +1,15 @@ #!/bin/bash + # testing STDERR messages # redirecting all to a file # 脚本执行期间,用exec命令告诉shell重定向某个特定文件描述符 -exec 2>test +exec 2> test ls badtest echo "This is test of redirecting all output" echo "from a script to another file" -exec 1>test1 +exec 1> test1 echo "This is the end of the script" echo "but this should go to the testerror file" >&2 diff --git a/codes/shell/示例脚本/输入和输出/获取用户输入.sh b/codes/shell/示例脚本/输入和输出/获取用户输入.sh index 4e36186..c8e33c2 100644 --- a/codes/shell/示例脚本/输入和输出/获取用户输入.sh +++ b/codes/shell/示例脚本/输入和输出/获取用户输入.sh @@ -17,9 +17,9 @@ echo "Checking data for $last. $first..." #如果不指定变量,read命令就会把它收到的任何数据都放到特殊环境变量REPLY中 read -p "Enter a number:" factorial=1 -for (( count=1; count<=$REPLY; count++)) +for (( count = 1; count <= $REPLY; count ++ )) do - factorial=$[ $factorial * $count ] + factorial=$[ $factorial * $count ] done echo "The factorial of $REPLY is $factorial" diff --git a/codes/shell/示例脚本/输入和输出/读取参数.sh b/codes/shell/示例脚本/输入和输出/读取参数.sh index 1a37a7c..c5f89f5 100644 --- a/codes/shell/示例脚本/输入和输出/读取参数.sh +++ b/codes/shell/示例脚本/输入和输出/读取参数.sh @@ -3,8 +3,8 @@ # using one command line parameter factorial=1 -for (( number = 1; number <= $1; number++)) +for (( number = 1; number <= $1; number ++ )) do - factorial=$[ $factorial * $number ] + factorial=$[ $factorial * $number ] done echo The factor of $1 is $factorial diff --git a/codes/shell/示例脚本/输入和输出/读取程序名.sh b/codes/shell/示例脚本/输入和输出/读取程序名.sh index f148da0..0637a45 100644 --- a/codes/shell/示例脚本/输入和输出/读取程序名.sh +++ b/codes/shell/示例脚本/输入和输出/读取程序名.sh @@ -1,4 +1,5 @@ #!/bin/bash + # testing the $0 parameter echo The command entered is $0 diff --git a/codes/shell/示例脚本/输入和输出/超时和输入计数.sh b/codes/shell/示例脚本/输入和输出/超时和输入计数.sh index 7dc8387..5879db3 100644 --- a/codes/shell/示例脚本/输入和输出/超时和输入计数.sh +++ b/codes/shell/示例脚本/输入和输出/超时和输入计数.sh @@ -1,22 +1,23 @@ #!/bin/bash + # timing the data entry if read -t 5 -p "Please enter your name:" name then - echo "Hello, $name, welcome to my script" + echo "Hello, $name, welcome to my script" else - #起到换行的作用 - echo - #输入计数 -n1 - read -n1 -p "Do you want to continue [Y/N]?" answer - case $answer in - Y | y) echo - echo "Fine, continue on...";; - N | n) echo - echo "OK,goodbye";; - *) echo - echo "OK, wrong, goodbye" - esac - echo "Sorry, this is the end of the script" + #起到换行的作用 + echo + #输入计数 -n1 + read -n1 -p "Do you want to continue [Y/N]?" answer + case $answer in + Y | y) echo + echo "Fine, continue on..." ;; + N | n) echo + echo "OK,goodbye" ;; + *) echo + echo "OK, wrong, goodbye" + esac + echo "Sorry, this is the end of the script" fi diff --git a/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh b/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh index 1299be2..ba9b2c5 100644 --- a/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh +++ b/codes/shell/示例脚本/输入和输出/隐藏方式读取数据.sh @@ -1,8 +1,9 @@ #!/bin/bash + # hiding input data from monitor read -s -p "Please enter your password: " pass #添加了-s选项之后,不会自动换行,不添加-s 会自动换行 -echo +echo echo "Is your password really $pass?" diff --git a/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh b/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh index 8505945..dcd43a1 100644 --- a/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh +++ b/codes/shell/示例脚本/进阶脚本/创建捕捉脚本.sh @@ -1,4 +1,5 @@ #!/bin/bash + # # Capture_Stats - Gather System Performance Statistics # diff --git a/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh b/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh index 07ba5ba..ebc0eab 100644 --- a/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh +++ b/codes/shell/示例脚本/进阶脚本/查看uptime获取在线用户数.sh @@ -1,3 +1,4 @@ #!/bin/bash + # uptime | sed 's/user.*$//' | gawk '{print $NF}' diff --git a/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh b/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh index 35fce81..7b3d646 100644 --- a/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh +++ b/codes/shell/示例脚本/进阶脚本/生成报告脚本-基于创建捕捉脚本.sh @@ -1,9 +1,10 @@ #!/bin/bash + # # Report_Stats - Generates Rpt from Captured Perf Stats # ############################################################ -# +# # Set Script Variables # REPORT_FILE=/home/tiandi/Documents/capstats.csv diff --git a/codes/shell/示例脚本/进阶脚本/系统快照报告.sh b/codes/shell/示例脚本/进阶脚本/系统快照报告.sh index 8614a1c..5fa363d 100644 --- a/codes/shell/示例脚本/进阶脚本/系统快照报告.sh +++ b/codes/shell/示例脚本/进阶脚本/系统快照报告.sh @@ -1,4 +1,5 @@ #!/bin/bash + # # Snapshot_Stats - produces a report for system stats # @@ -16,9 +17,9 @@ REPORT=/home/tiandi/Documents/Snapshot_Stats_$DATE.rpt # # Create Report File # -exec 3>&1 # Save file descriptor +exec 3>&1 # Save file descriptor # -exec 1> $REPORT # direct output to rpt file +exec 1> $REPORT # direct output to rpt file # ################################################### # @@ -44,10 +45,10 @@ uptime | sed -n '/,/s/,/ /gp' | gawk '{if($4 == "days" || $4 == "day") {print $2 #2) Gather Disk Usage Statistics # echo -for DISK in $DISK_TO_MONITOR # loop to check disk space +for DISK in $DISK_TO_MONITOR # loop to check disk space do - echo -e "$DISK usage: \c" - df -h $DISK | sed -n '/% \//p' | gawk '{ print $5 }' + echo -e "$DISK usage: \c" + df -h $DISK | sed -n '/% \//p' | gawk '{ print $5 }' done # ################################################################## @@ -68,10 +69,10 @@ ZOMBIE_CHECK=`ps -al | gawk '{print $2,$4}' | grep Z` # if [ "$ZOMBIE_CHECK" = "" ] then - echo "No Zombie Process on System at this Time" + echo "No Zombie Process on System at this Time" else - echo "Current System Zombie Processes" - ps -al | gawk '{print $2,$4}' | grep Z + echo "Current System Zombie Processes" + ps -al | gawk '{print $2,$4}' | grep Z fi echo # @@ -79,7 +80,7 @@ echo # # Restore File Descriptor & Mail Report # -exec 1>&3 # Restore output to STDOUT +exec 1>&3 # Restore output to STDOUT # #$MAIL -a $REPORT -s "System Sstatistics Report for $DATE" #-- $MAIL_TO < /dev/null diff --git a/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh b/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh index a935ad1..99196b3 100644 --- a/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh +++ b/codes/shell/示例脚本/进阶脚本/问题跟踪数据库/Update_Problem.sh @@ -1,4 +1,5 @@ #!/bin/bash + # # Update_Problem - updates problem record in database # @@ -12,28 +13,29 @@ MYSQL=`which mysql`" Problem_Trek -u root" # # Obtain Record Id # -if [ $# -eq 0 ] # Check if id number was passed -then # If not passed ask for it -# -# Check if any unfinished records exist. - RECORDS_EXIST=`$MYSQL -Bse 'SELECT id_number FROM problem_logger where fixed_date="0000-00-00" OR prob_solutions=""'` -# - if [ "$RECORDS_EXIST" != "" ] - then - echo - echo "The following record(s) need updating..." - $MYSQL < 90)) -then - (( var2 = $var1 ** 2)) - echo "The square of $var1 if $var2" +if (($var1 ** 2 > 90)) +then + ((var2 = $var1 ** 2)) + echo "The square of $var1 if $var2" fi diff --git a/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh b/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh index a965817..4f2b293 100644 --- a/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh +++ b/codes/shell/示例脚本/逻辑控制/使用双引号圈起用空格分隔的字符串.sh @@ -1,7 +1,8 @@ #!/bin/bash + # another example of how not to use the for command for test in Newada "New Hampshire" do - echo "Now going to $test" + echo "Now going to $test" done diff --git a/codes/shell/示例脚本/逻辑控制/使用双方括号.sh b/codes/shell/示例脚本/逻辑控制/使用双方括号.sh index 4d76eca..3f73c78 100644 --- a/codes/shell/示例脚本/逻辑控制/使用双方括号.sh +++ b/codes/shell/示例脚本/逻辑控制/使用双方括号.sh @@ -1,9 +1,10 @@ #!/bin/bash + # using pattern matching if [[ $USER == r* ]] -then - echo "Hello $USER" +then + echo "Hello $USER" else - echo "Sorry, I do not know you" + echo "Sorry, I do not know you" fi diff --git a/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh b/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh index 5887c99..fae9a1f 100644 --- a/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh +++ b/codes/shell/示例脚本/逻辑控制/使用嵌套循环并修改IFS.sh @@ -5,11 +5,11 @@ IFS.OLD=$IFS IFS=$'\n' for entry in `cat /etc/passwd` -do - echo "Values in $entry -" - IFS=: - for value in $entry - do - echo " $value" - done +do + echo "Values in $entry -" + IFS=: + for value in $entry + do + echo " $value" + done done diff --git a/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh b/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh index 89f7a8f..c8634b4 100644 --- a/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh +++ b/codes/shell/示例脚本/逻辑控制/使用符合条件测试.sh @@ -1,9 +1,10 @@ #!/bin/bash + #testing compound comparisons if [ -d $HOME ] && [ -w $HOME/testing ] then - echo "The file exists and you can write to it" + echo "The file exists and you can write to it" else - echo "I cannot write to it" + echo "I cannot write to it" fi diff --git a/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh b/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh index 7ec74a4..9e20100 100644 --- a/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh +++ b/codes/shell/示例脚本/逻辑控制/使用管道或重定向.sh @@ -3,18 +3,18 @@ # redirecting the for output to a file for file in /home/tiandi/* do - if [ -d "$file" ] - then - echo "$file is a directory" - else - echo "$file is a file" - fi + if [ -d "$file" ] + then + echo "$file is a directory" + else + echo "$file is a file" + fi done > output.txt # piping a loop to another command for state in "North Dakota" Connecticut do - echo "$state is next place to go" + echo "$state is next place to go" done | sort echo "This completes our travels" diff --git a/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh b/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh index cc45dad..c872f2e 100644 --- a/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh +++ b/codes/shell/示例脚本/逻辑控制/使用通配符处理目录.sh @@ -1,12 +1,13 @@ #!/bin/bash + #iterate through all the files in a directory for file in /home/tiandi/test/* do - if [ -d "$file" ] - then - echo "$file is a directory" - elif [ -f "$file" ] - then - echo "$file is a file" - fi + if [ -d "$file" ] + then + echo "$file is a directory" + elif [ -f "$file" ] + then + echo "$file is a file" + fi done diff --git a/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh b/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh index 13114cc..829594a 100644 --- a/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh +++ b/codes/shell/示例脚本/逻辑控制/判断字符串长度是否为零.sh @@ -1,4 +1,5 @@ #!/bin/bash + # testing string length #-n 判断长度是否非零 @@ -8,22 +9,22 @@ val1=testing val2='' if [ -n "$val1" ] -then - echo "The string $val1 is not empty" +then + echo "The string $val1 is not empty" else - echo "The string $val1 is empty" + echo "The string $val1 is empty" fi if [ -z "$val2" ] -then - echo "The string $val2 is empty" +then + echo "The string $val2 is empty" else - echo "The string $val2 is not empty" + echo "The string $val2 is not empty" fi if [ -z "$val3" ] -then - echo "The string $val3 is empty" +then + echo "The string $val3 is empty" else - echo "The string $val3 is not empty" + echo "The string $val3 is not empty" fi diff --git a/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh b/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh index 48d4118..a1420a1 100644 --- a/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh +++ b/codes/shell/示例脚本/逻辑控制/在for语句中使用多个变量.sh @@ -1,7 +1,8 @@ #!/bin/bash + # multiple variables -for (( a=1, b=10; a<=10; a++,b-- )) +for (( a = 1 , b = 10; a <= 10; a ++ , b -- )) do - echo "$a - $b" + echo "$a - $b" done diff --git a/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh b/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh index a4d439c..3392e33 100644 --- a/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh +++ b/codes/shell/示例脚本/逻辑控制/在then块中使用多条命令.sh @@ -1,8 +1,9 @@ #!/bin/bash + #testing multiple commands in the then section testuser=tiandi if grep $testuser /etc/passwd then - echo The bash files from user $testuser are: - ls -a /home/$testuser/.b* + echo The bash files from user $testuser are: + ls -a /home/$testuser/.b* fi diff --git a/codes/shell/示例脚本/逻辑控制/字符串比较.sh b/codes/shell/示例脚本/逻辑控制/字符串比较.sh index 0382761..c48c994 100644 --- a/codes/shell/示例脚本/逻辑控制/字符串比较.sh +++ b/codes/shell/示例脚本/逻辑控制/字符串比较.sh @@ -1,9 +1,10 @@ #!/bin/bash + #testing string equality testuser=tiandi if [ $USER = $testuser ] then - echo "Welcome $testuser" + echo "Welcome $testuser" fi diff --git a/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh b/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh index f0380e8..0e02981 100644 --- a/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh +++ b/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh @@ -1,20 +1,21 @@ #!/bin/bash + # checking if a directory or a file exists if [ -e $HOME ] then - echo "OK on the directory.now to check the file" - #checking if a file exists - if [ -e $HOME/testing ] - then - #the file exists,append data to it - echo "Appending date to existing file" - date >> $HOME/testing - else - #the file is not exists,create a new file - echo "Creating a new file" - date > $HOME/testing - fi + echo "OK on the directory.now to check the file" + #checking if a file exists + if [ -e $HOME/testing ] + then + #the file exists,append data to it + echo "Appending date to existing file" + date >> $HOME/testing + else + #the file is not exists,create a new file + echo "Creating a new file" + date > $HOME/testing + fi else - echo 'Sorry. you do not have a $HOME directory' + echo 'Sorry. you do not have a $HOME directory' fi diff --git a/codes/shell/示例脚本/逻辑控制/检查目录.sh b/codes/shell/示例脚本/逻辑控制/检查目录.sh index 7a1fe79..4bb2cf7 100644 --- a/codes/shell/示例脚本/逻辑控制/检查目录.sh +++ b/codes/shell/示例脚本/逻辑控制/检查目录.sh @@ -1,11 +1,12 @@ #!/bin/bash + # look before you leap if [ -d $HOME ] then - echo "Your home directory exists" - cd $HOME - ls -a + echo "Your home directory exists" + cd $HOME + ls -a else - echo "There is a problem with your HOME direcotry" + echo "There is a problem with your HOME direcotry" fi diff --git a/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh b/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh index 3cc6876..eecac33 100644 --- a/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh +++ b/codes/shell/示例脚本/逻辑控制/正确使用大于小于号.sh @@ -1,12 +1,13 @@ #!/bin/bash + #正确使用大于小于号 val1=baseball val2=hocky if [ $val1 \> $val2 ] -then - echo "$val1 is greater than $val2" +then + echo "$val1 is greater than $val2" else - echo "$val1 is less than $val2" + echo "$val1 is less than $val2" fi diff --git a/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh b/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh index 96123e4..2ea2de5 100644 --- a/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh +++ b/codes/shell/示例脚本/逻辑控制/注意test大小写顺序和sort不同.sh @@ -1,4 +1,5 @@ #!/bin/bash + #test命令中,大小字母会被当成小于小写字符,而在sort中,小写字母会先出现,test使用标准的ASCII排序,sort使用本地化语言设置进行排序,对于英语,本地化设置制定了排序顺序中小写字母出现在大写字母之前 var1=Testing @@ -6,7 +7,7 @@ var2=testing if [ $val1 \> $val2 ] then - echo '$val1 is greater than $val2' + echo '$val1 is greater than $val2' else - echo '$val1 is less than $val2' + echo '$val1 is less than $val2' fi diff --git a/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh b/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh index e5e442d..45b8c74 100644 --- a/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh +++ b/codes/shell/示例脚本/逻辑控制/解决读取列表中的复杂值.sh @@ -1,6 +1,6 @@ #!/bin/bash for test in I don\'t know if "this'll" work -do - echo "word:$test" +do +echo "word:$test" done diff --git a/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh b/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh index 7063175..1c1d292 100644 --- a/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh +++ b/codes/shell/示例脚本/逻辑控制/读取列表中的值.sh @@ -3,5 +3,5 @@ # basic for command for test in Alabama Alaska Arizona do - echo The next state is $test + echo The next state is $test done diff --git a/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh b/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh index 52034e7..d8723d7 100644 --- a/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh +++ b/codes/shell/示例脚本/逻辑控制/读取里表中复杂的值.sh @@ -3,6 +3,6 @@ # another example of how not to use the for command for test in I don't know if this'll work -do - echo "word:$test" +do + echo "word:$test" done diff --git a/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh b/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh index 7bc876b..25170b0 100644 --- a/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh +++ b/codes/shell/示例脚本/逻辑控制/错误的使用大于小于号.sh @@ -1,4 +1,5 @@ #!/bin/bash + # 大于小于号必须转义,否则shell会将它们当做重定向符号而把字符串值当做文件名处理 # 大于小于号顺序和sort命令所采用的有所不同 # mis-using string comparisons @@ -8,7 +9,7 @@ val2=hockey if [ $val1 > $val2 ] then - echo "$val1 is greater than $val2" + echo "$val1 is greater than $val2" else - echo "$val1 is less than $val2" + echo "$val1 is less than $val2" fi diff --git a/docs/linux/soft/redis-ops.md b/docs/linux/soft/redis-ops.md index dec2e90..72cdeb7 100644 --- a/docs/linux/soft/redis-ops.md +++ b/docs/linux/soft/redis-ops.md @@ -18,6 +18,7 @@ - [设为守护进程](#设为守护进程) - [远程访问](#远程访问) - [设置密码](#设置密码) + - [配置参数表](#配置参数表) - [Redis 集群使用和配置](#redis-集群使用和配置) - [集群规划](#集群规划) - [部署](#部署) @@ -165,6 +166,41 @@ Redis 默认访问不需要密码,如果需要设置密码,需要如下配 - `protected-mode yes` - `requirepass <密码>` +#### 配置参数表 + +| 配置项 | 说明 | +| :-- | :-- | +| `daemonize no` | Redis 默认不是以守护进程的方式运行,可以通过该配置项修改,使用 yes 启用守护进程(Windows 不支持守护线程的配置为 no ) | +| `pidfile /var/run/redis.pid` | 当 Redis 以守护进程方式运行时,Redis 默认会把 pid 写入 /var/run/redis.pid 文件,可以通过 pidfile 指定 | +| `port 6379` | 指定 Redis 监听端口,默认端口为 6379,作者在自己的一篇博文中解释了为什么选用 6379 作为默认端口,因为 6379 在手机按键上 MERZ 对应的号码,而 MERZ 取自意大利歌女 Alessia Merz 的名字 | +| `bind 127.0.0.1` | 绑定的主机地址 | +| `timeout 300` | 当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能 | +| `loglevel notice` | 指定日志记录级别,Redis 总共支持四个级别:debug、verbose、notice、warning,默认为 notice | +| `logfile stdout` | 日志记录方式,默认为标准输出,如果配置 Redis 为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给 /dev/null | +| `databases 16` | 设置数据库的数量,默认数据库为 0,可以使用 SELECT 命令在连接上指定数据库 id | +| `save ` Redis 默认配置文件中提供了三个条件:**save 900 1**、**save 300 10**、**save 60 10000** 分别表示 900 秒(15 分钟)内有 1 个更改,300 秒(5 分钟)内有 10 个更改以及 60 秒内有 10000 个更改。 | 指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合 | +| `rdbcompression yes` | 指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大 | +| `dbfilename dump.rdb` | 指定本地数据库文件名,默认值为 dump.rdb | +| `dir ./` | 指定本地数据库存放目录 | +| `slaveof ` | 设置当本机为 slav 服务时,设置 master 服务的 IP 地址及端口,在 Redis 启动时,它会自动从 master 进行数据同步 | +| `masterauth ` | 当 master 服务设置了密码保护时,slav 服务连接 master 的密码 | +| `requirepass foobared` | 设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH 命令提供密码,默认关闭 | +| `maxclients 128` | 设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息 | +| `maxmemory ` | 指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区 | +| `appendonly no` | 指定是否在每次更新操作后进行日志记录,Redis 在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis 本身同步数据文件是按上面 save 条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为 no | +| `appendfilename appendonly.aof` | 指定更新日志文件名,默认为 appendonly.aof | +| `appendfsync everysec` | 指定更新日志条件,共有 3 个可选值:**no**:表示等操作系统进行数据缓存同步到磁盘(快)**always**:表示每次更新操作后手动调用 fsync() 将数据写到磁盘(慢,安全)**everysec**:表示每秒同步一次(折中,默认值) | +| `vm-enabled no` | 指定是否启用虚拟内存机制,默认值为 no,简单的介绍一下,VM 机制将数据分页存放,由 Redis 将访问量较少的页即冷数据 swap 到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析 Redis 的 VM 机制) | +| `vm-swap-file /tmp/redis.swap` | 虚拟内存文件路径,默认值为 /tmp/redis.swap,不可多个 Redis 实例共享 | +| `vm-max-memory 0` | 将所有大于 vm-max-memory 的数据存入虚拟内存,无论 vm-max-memory 设置多小,所有索引数据都是内存存储的(Redis 的索引数据 就是 keys),也就是说,当 vm-max-memory 设置为 0 的时候,其实是所有 value 都存在于磁盘。默认值为 0 | +| `vm-page-size 32` | Redis swap 文件分成了很多的 page,一个对象可以保存在多个 page 上面,但一个 page 上不能被多个对象共享,vm-page-size 是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page 大小最好设置为 32 或者 64bytes;如果存储很大大对象,则可以使用更大的 page,如果不确定,就使用默认值 | +| `vm-pages 134217728` | 设置 swap 文件中的 page 数量,由于页表(一种表示页面空闲或使用的 bitmap)是在放在内存中的,,在磁盘上每 8 个 pages 将消耗 1byte 的内存。 | +| `vm-max-threads 4` | 设置访问 swap 文件的线程数,最好不要超过机器的核数,如果设置为 0,那么所有对 swap 文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为 4 | +| `glueoutputbuf yes` | 设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启 | +| `hash-max-zipmap-entries 64 hash-max-zipmap-value 512` | 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法 | +| `activerehashing yes` | 指定是否激活重置哈希,默认为开启(后面在介绍 Redis 的哈希算法时具体介绍) | +| `include /path/to/local.conf` | 指定包含其它的配置文件,可以在同一主机上多个 Redis 实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件 | + ## Redis 集群使用和配置 Redis 3.0 后支持集群模式。