Linux的bash命令语法

可用点

#!/bin/bash
# 文件要以上面开始,.sh结尾的文件不需要

# 赋权文件可执行权限
chmod +x <fileName>

# 获取java jar包启动的进程id
ps -ef | grep *.jar | grep -v grep | awk '{print $2}'



shell变量

变量命令规则:

  1. 只能包含字母、数字、下划线;
  2. 不能以数字开口,可以包含数字;
  3. 不能使用Shell的关键字作为变量名
  4. 使用大写字母表示常量;
  5. 变量赋值不能使用空格,例如:PID=“pid”;

使用变量

#!/bin/bash
PID="pid"
P1=`ps -ef | grep *.jar | grep -v grep | awk '{print $2}'` # 1.使用命令执行结果作为变量
P1=`ps -ef | grep *.jar | grep -v grep | awk "{print $2}"` # 1.使用命令执行结果作为变量
P2="`ps -ef | grep *.jar | grep -v grep | awk '{print $2}'`" # 1.使用命令执行结果作为变量
PID=$(ps -ef | grep *.jar | grep -v grep | awk '{print $2}') # 2.

# 使用变量
echo $PID
echo "P1=$PID"
echo 'P2=$PID'
echo 'P3=''$PID' # 要成对
echo ${PID}

# 设置只读
only_read="Java"
readonly only_read
only_read="python" #会有提示

# 删除变量
unset only_read
echo ${only_read}

Linux的bash命令语法_第1张图片

变量类型

# 字符串变量
s1="Hello World"
s2='Hello World'

# 整数变量
declare -i i1=128

# 数组变量
array_example=(1 2 3 4 5)
declare -A accociative_array
accociative_array["name"]="zs"
associative_array["age"]=18

# 读取环境变量
echo $JAVA_HOME


流程语句

if

#!/bin/sh
if [ condition ]; then
  # ...
else
  # ...
fi

#!/bin/sh


运算符

#!/bin/sh

###
SERVICE_NAME="lgsa-portRelease-mgr"

ENVFILE="../env"

PIDFILE="pid"

checkRunning(){
    if [ -f "$PIDFILE" ]; then
       if  [ -z "`cat $PIDFILE`" ];then
        echo "ERROR: Pidfile '$PIDFILE' exists but contains no pid"
        return 2
       fi
       PID="`cat ${PIDFILE}`"
       RET="`ps -p "${PID}"|grep java`"
       if [ -n "$RET" ];then
	 echo "${RET}"
         return 1;
       else
         return 0;
       fi
    else
         return 0;
    fi
}

status(){
    if ( checkRunning );then
         PID="`cat $PIDFILE`"
         echo "'$SERVICE_NAME' is running (pid '$PID')"
         exit 0
    fi
    echo "'$SERVICE_NAME' not running"
    exit 1
}


#启动方法
start(){
    if ( checkRunning );then
      PID="`cat $PIDFILE`"
      echo "INFO: Process with pid '$PID' is already running"
      exit 0
    fi
	ENVIRONMENT="`cat ${ENVFILE}`"
    java -jar -Xms64M -Xmx512M -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5105 -Dspring.config.location=config-${SERVICE_NAME}/application-${ENVIRONMENT}.yml -Dlogging.config=config-${SERVICE_NAME}/log4j2.xml  ${SERVICE_NAME}-0.0.1-SNAPSHOT.jar --spring.profiles.active=${ENVIRONMENT} > console.log 2>&1 &
    echo $! > "${PIDFILE}";
}
#停止方法
stop(){
    if ( checkRunning ); then
       PID="`cat ${PIDFILE}`"
       echo "INFO: sending SIGKILL to pid '$PID'"
       kill -KILL $PID
       RET="$?"
       rm -f "${PIDFILE}"
       return $RET
    fi
    echo "INFO: not running, nothing to do"
    return 0
}

show_help() {
    cat << EOF
Tasks provided by the sysv init script:
    stop            - terminate instance in a drastic way by sending SIGKILL
    start           - start new instance
    restart         - stop running instance (if there is one), start new instance
    status          - check if '$SERVICE_NAME' process is running
EOF
  exit 1
}

# show help
if [ -z "$1" ];then
 show_help
fi

case "$1" in
  status)
    status
    ;;
  restart)
    if ( checkRunning );then
      $0 stop
      echo
    fi
    $0 start
    $0 status
    ;;
  start)
    start
    ;;
  stop)
    stop
    exit $?
    ;;
  *)
esac


你可能感兴趣的:(linux,bash)