整理学习 shell 脚本编写过程中遇到的各种疑难杂症

shell 脚本编写过程中需要注意的事项

1. = 号是赋值符号, 前后不允许有空格, 不然会报错      
2. 字符串是赋予变量的值, 必须使用单引号 ''  或者双引号 ""  包围, 否则会有语义错误   
3. 变量的引用, 需要使用 ${param} 这类方式引用    
4. if[] 是标准的判断表达式, [] 与 test 命令一样, 可以判断表达式是否为真,  if[[]] 支持正则等, 表达式左右必须要有空格!!!!!!!!!!
5. -a 叫选项 option  abc 叫参数 parameter

例子代码

#!/bin/sh

echo "
 *************************************
 *          Author by gomyck         *
 * --------------------------------  *
 *|  qq:    474798383              | *
 *|  email: hao474798383@163.com   | *
 *|  blog:  https://blog.gomyck.com| *
 * --------------------------------  *
 *************************************
 "

JAVA_HOME=
RESOURCE_NAME=*.jar
LOG_PATH="/dev/null"
ACTIVE=ck

while [ -n "$1" ] # -n 参数的含义: 判断 $1 是否是 null, 长度大于 0
do
    case "$1" in
        -log | -l)  # | 是或者, 必须使用一个竖线, 不可以用 ||
			LOG_PATH="$2"   # 赋值, 左右不可以有空格
            echo "日志输出位置: $2"  
            shift ;;  # 移动游标, 使当前参数数组的初始游标向右位移一位,  比如  1 2 3 4 5 起始 $1 是 1, shift 之后  $1 就是 2
        -profile | -p) 
			ACTIVE="$2"
            echo "激活配置: $2"
            shift ;;
        --) shift
            break ;;
        *) ACTIVE="$1";;
    esac
    shift
done

if [[ ${ACTIVE} != "pro" && ${ACTIVE} != "dev" && ${ACTIVE} != "test" ]]; then
echo "Please input a model you want active -> [dev pro test]"
exit
fi
PARAM_VALUE='-Dfile.encoding=UTF-8 -Deureka.instance.ip-address= -Djasypt.encryptor.password='
if [[ ${ACTIVE} ==  "test" || ${ACTIVE} ==  "dev" ]]; then
	echo "开发测试环境, 日志默认输出位置: ./gomyck.log"
	LOG_PATH="gomyck.log"
fi
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`  # 追溯进程号, 赋值
if [ ${tpid} ]; then
echo Close Application... ${tpid}
kill -15 $tpid
fi

sleep 5

tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo Close Application... ${tpid}
kill -9 $tpid
else
echo Close Success!
echo Bootstrap Application...
fi

rm -f tpid

if [ ${JAVA_HOME} ]; then
    nohup $JAVA_HOME/bin/java -jar $PARAM_VALUE -Dspring.profiles.active=${ACTIVE} ./$RESOURCE_NAME > ${LOG_PATH} 2>&1 & 
else
    nohup java -jar $PARAM_VALUE -Dspring.profiles.active=${ACTIVE} ./$RESOURCE_NAME > ${LOG_PATH} 2>&1 & 
fi

echo $! > tpid
echo Application is Running!!!

if [[ ${ACTIVE} ==  "test" || ${ACTIVE} ==  "dev" ]]; then
	tail -f gomyck.log
fi

其他知识点

kill 命令接收的信号量

SignalNameNumberDescription
SIGHUP1Hangup (POSIX)
SIGINT2Terminal interrupt (ANSI)
SIGQUIT3Terminal quit (POSIX)
SIGILL4Illegal instruction (ANSI)
SIGTRAP5Trace trap (POSIX)
SIGIOT6IOT Trap (4.2 BSD)
SIGBUS7BUS error (4.2 BSD)
SIGFPE8Floating point exception (ANSI)
SIGKILL9Kill(can’t be caught or ignored) (POSIX)
SIGUSR110User defined signal 1 (POSIX)
SIGSEGV11Invalid memory segment access (ANSI)
SIGUSR212User defined signal 2 (POSIX)
SIGPIPE13Write on a pipe with no reader, Broken pipe (POSIX)
SIGALRM14Alarm clock (POSIX)
SIGTERM15Termination (ANSI)
SIGSTKFLT16Stack fault
SIGCHLD17Child process has stopped or exited, changed (POSIX)
SIGCONT18Continue executing, if stopped (POSIX)
SIGSTOP19Stop executing(can’t be caught or ignored) (POSIX)
SIGTSTP20Terminal stop signal (POSIX)
SIGTTIN21Background process trying to read, from TTY (POSIX)
SIGTTOU22Background process trying to write, to TTY (POSIX)
SIGURG23Urgent condition on socket (4.2 BSD)
SIGXCPU24CPU limit exceeded (4.2 BSD)
SIGXFSZ25File size limit exceeded (4.2 BSD)
SIGVTALRM26Virtual alarm clock (4.2 BSD)
SIGPROF27Profiling alarm clock (4.2 BSD)
SIGWINCH28Window size change (4.3 BSD, Sun)
SIGIO29I/O now possible (4.2 BSD)
SIGPWR30Power failure restart (System V)

常用的信号量有 2 9 15

cat » fileName «(-) EOF

创建文件, 然后根据操作提示符, 输入字符, 当系统单独捕捉到 EOF 时, 结束输入监听

« 后面加 - 之后, EOF 不需要顶行写, 兼容脚本产生的制表符



相关文章:
⤧  上一篇 一招让你 MAC idea springboot 项目启动快 10 倍 ⤧  下一篇 罗织经译卷