zoukankan      html  css  js  c++  java
  • shell脚本学习

    启动脚本 start.sh 这个脚本用于启动项目,记录日志和pid
    #!/bin/sh
    PRG="$0" #$0代表当前启动脚本
     
    # 获取文件真实路径 while [ -h "$PRG" ]; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> (.*)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done # 截取文件路径 PRGDIR=`dirname "$PRG"` [ -z "$APP_HOME" ] && APP_HOME=`cd "$PRGDIR" >/dev/null; pwd` [ -z "$APP_PID" ] && APP_PID=$APP_HOME/pid FILE_LIST=`ls ${APP_HOME}` JAR_FILE="" LOG_FILE=""
    # 填充变量值 for FILE in $FILE_LIST do if [ "${FILE##*.}" = "jar" ]; then JAR_FILE="${APP_HOME}/${FILE}" LOG_FILE="${FILE%.*}.log" fi done if [ -z $JAR_FILE ]; then echo "Error : no jar file in $APP_HOME" exit fi if [ -f "$APP_PID" ]; then PID=`cat $APP_PID` rm -rf $APP_PID fi

    # 加载配置文件,使用java -jar *.jar 启动Java项目,并输出到日志文件 source
    /etc/profile OPTS="-Dfile.encoding=UTF8 -Djasypt.encryptor.password=${JASYPT_PASSWORD} -Djava.security.egd=file:/dev/./urandom -Xmx512m -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -Xloggc:gc.log" nohup java $DEBUG -jar $OPTS ${JAR_FILE} >$APP_HOME/${LOG_FILE} 2>&1 & PID=$! echo $PID > ${APP_PID} if [ ! -f $APP_HOME/app.log ]; then echo "begin....." >>$APP_HOME/app.log fi

      

    if判断参数
    [-a file] 如果file存在则为真
    [-b file] 如果file存在且是一个块特殊文件则为真
    [-c file] 如果file存在且是一个字特殊文件则为真
    [-d file] 如果file文件存在且是一个目录则为真
    [ ! -d file ] 表示后面的那个目录不存在
    [-e file] 如果file文件存在则为真
    [-f file] 如果file存在且是一个普通文件则为真
    [-g file] 如果file存在且已经设置了SGID则为真(SUID 是 Set User ID, SGID 是 Set Group ID的意思)
    [-h file] 如果file存在且是一个符号连接则为真
    [-k file] 如果file存在且已经设置粘制位则为真
    [-p file] 如果file存在且是一个名字管道(F如果O)则为真
    管道是linux里面进程间通信的一种方式,其他的还有像信号(signal)、信号量、消息队列、共享内存、套接字(socket)等。
    [-r file] 如果file存在且是可读的则为真
    [-s file] 如果file存在且大小不为0则为真
    [-t FD] 如果文件描述符FD打开且指向一个终端则为真
    [-u file] 如果file存在且设置了SUID(set userID)则为真
    [-w file] 如果file存在且是可写的则为真
    [-x file] 如果file存在且是可执行的则为真
    [-O file] 如果file存在且属有效用户ID则为真
    [-G file] 如果file存在且属有效用户组则为真
    [-L file] 如果file存在且是一个符号连接则为真
    [-N file] 如果file存在and has been mod如果ied since it was last read则为真
    [-S file] 如果file存在且是一个套接字则为真
    [file1 –nt file2] 如果file1 has been changed more recently than file2或者file1 exists and file2 does not则为真
    [file1 –ot file2] 如果file1比file2要老,或者file2存在且file1不存在则为真
    [file1 –ef file2] 如果file1和file2指向相同的设备和节点号则为真
    [-o optionname] 如果shell选项“optionname”开启则为真
    [-z string] “string”的长度为零则为真
    [-n string] or [string] “string”的长度为非零non-zero则为真
    [sting1==string2] 如果2个字符串相同。“=”may be used instead of “==”for strict posix compliance则为真
    [string1!=string2] 如果字符串不相等则为真
    [string1<string2] 如果“string1”sorts before“string2”lexicographically in the current locale则为真
    PRGDIR=`dirname "$PRG"`
    用于截取文件的目录部分 
    [root@test aa]# dirname /data/ssth/excenter/aa
    /data/ssth/excenter
    "${FILE##*.}" = "jar"
    ${}:用于字符串截取,它和#(截取左侧),%(截取右侧)
    ${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
    ${file##*/}:删掉最后一个 /  及其左边的字符串:my.file.txt
    ${file#*.}:删掉第一个 .  及其左边的字符串:file.txt
    ${file##*.}:删掉最后一个 .  及其左边的字符串:txt
    ${file%/*}:删掉最后一个  /  及其右边的字符串:/dir1/dir2/dir3
    ${file%%/*}:删掉第一个 /  及其右边的字符串:(空值)
    ${file%.*}:删掉最后一个  .  及其右边的字符串:/dir1/dir2/dir3/my.file
    ${file%%.*}:删掉第一个  .   及其右边的字符串:/dir1/dir2/dir3/my
    nohup java $DEBUG -jar $OPTS ${JAR_FILE}  >$APP_HOME/${LOG_FILE} 2>&1 &
    这个语句干了三件事:

    nohup [command] &:表示不间断后台执行命令

    • nohup:永久执行,忽略所有挂断(SIGHUP)信号
      • 结果默认输出到nohup.out,由于缓存输出的原因,如果不指定输出内容,nohup.out大小为0;
      • 关闭session发送SIGHUP信号,程序免疫
      • 使用Ctrl+c发送SIGINT信号,程序关闭
    • &:后台执行,忽略所有前台关闭SIGINT信号
      • 结果会输出到终端
      • 关闭session发送SIGHUP信号,程序关闭
      • 使用Ctrl+c发送SIGINT信号,程序免疫
    >:是1>的简写,表示标准输出
    2>&1:将标准错误输出到标准输出通道
       
     操作系统中有三个常用的流及其代码:
      标准输入流 stdin:0
      标准输出流 stdout:1
      标准错误流 stderr:2

    例:区分标准输出和错误输出内容

    [root@test aa]# ll
    总用量 4
    -rw-r--r--. 1 root root 8 5月 24 09:50 a.txt
    [root@test aa]# ls a.txt b.txt
        ls: 无法访问b.txt: 没有那个文件或目录
        a.txt
    [root@test aa]# ls a.txt b.txt 1> output 2>err #将标准输出到output文件,将错误输出到err文件
    [root@test aa]# ll
    总用量 12
    -rw-r--r--. 1 root root  8 5月  24 09:50 a.txt
    -rw-r--r--. 1 root root 51 5月  24 09:52 err
    -rw-r--r--. 1 root root  6 5月  24 09:52 output
    [root@test aa]# cat err
    ls: 无法访问b.txt: 没有那个文件或目录
    [root@test aa]# cat output
    a.txt
    [root@test aa]# ls a.txt b.txt 1> output 2>&1 #转换为上面的内容即是,将标准输出和错误输出都放到指定文件中
    [root@test aa]# cat output
    ls: 无法访问b.txt: 没有那个文件或目录
    a.txt

       

    >/dev/null
    >是标准输出,/dev/null表示不在控制台显示输出内容
  • 相关阅读:
    备用
    Python进阶
    *args 和 **kwargs
    C语言
    【Pythno库】-Re
    【C语言】-struct
    Django By Example
    字符串
    Python库
    【Keil】Keil5-改变字的大小和颜色
  • 原文地址:https://www.cnblogs.com/chengmuyu/p/9081451.html
Copyright © 2011-2022 走看看