table of Contents
Shell introduction
Shell tutorial
Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language.
Shell refers to an application program that provides an interface through which users access the services of the operating system kernel.
Ken Thompson's sh is the first Unix Shell, and Windows Explorer is a typical graphical shell.
Shell script
Shell script (shell script) is a script program written for the shell.
The shell in the industry usually refers to shell script, but readers should know that shell and shell script are two different concepts.
For reasons of habit and for the sake of brevity, "shell programming" in this article refers to shell script programming, not the development of the shell itself.
Shell knowledge points
variable
Equal sign set variable
变量名=变量内容
$ Read, $ variable name in single quotes, $ variable name in double quotes, $ {variable name} are all available
$ 变量名、$ {
变量名}
Pass parameters
$ n, n represents the number of the number
$ 0 is the file name to be executed, and the rest represent the nth parameter
$ n
Operator
Arithmetic Operator
+ 加
- 减
* 乘
/ 初
= 赋值
== 相等
!= 不相等
Relational operator
-eq 相等返回 true
-ne 不相等返回 true
-gt >返回 true
-lt <返回 true
-ge >=返回 true
-le <=返回 true
Boolean operator
! 非运算
-o 或运算
-a 与运算
Logical Operators
&& AND
|| OR
String operator
= 两个字符串相等返回 true [ $a = $b ]
!= 不相等返回 true
-z 字符串长度为0返回 true
-n 字符串长度不为 0 返回 true
$ 字符串s不为空返回 true。
command
echo, output string
echo string
Process control
if else-if else
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
for loop
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
while statement
while condition
do
command
done
switch case
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
function
[ function ] funname [()]
{
action;
[return int;]
}
parameter
$# 传递到脚本或函数的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
Input/output redirection
command > file 将输出重定向到 file。
command < file 将输入重定向到 file。
command >> file 将输出以追加的方式重定向到 file。
Shell script
The shell file compiled under windows, the end of each line is \n\r, and the end of the file under linux is \n. After the
file is placed in linux, use the following command to solve
$ sed -i 's/\r$//' 文件名
Annotated version
# export:用于设置或显示环境变量
# export [-fnp][变量名称]=[变量设置值]
export JAVA_HOME=/usr/java/jdk1.8.0_11
export JRE_HOME=$JAVA_HOME/jre
# 变量赋值,反斜杠为转义
# API_NAME+JAR_NAME 包名
API_NAME=helloworld
JAR_NAME=$API_NAME\.jar
# 启动日志名
LOG_NAME=$API_NAME\.log
#PID 代表是PID文件
PID=$API_NAME\.pid
# 方法:使用说明,用来提示输入参数
formatDescription() {
# echo:输出字符串
# -e:开启转义
echo -e "===============Please use the following format==================\n"
echo -e ">>> sh service.sh [start|debug|stop|restart|status] <<<"
echo -e "\n==============================================================="
# exit 0:正常运行程序并退出程序;
# exit 1:非正常运行导致退出程序;
exit 1
}
# 方法:检查程序是否在运行
checkStatus(){
# ps -ef|grep $JAR_NAME 匹配进程中包名
# grep -v 参数 : 过滤 匹配参数
# awk '{print $2}' 匹配第二个参数
# $0 就是你写的shell脚本本身的名字
# $1 是你给你写的shell脚本传的第一个参数
# $2 是你给你写的shell脚本传的第二个参数
pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
# if then、elif then、else、fi
# [ -z STRING ] “STRING” 的长度为零则为真
# 如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
# 方法:启动方法
startService(){
# 调用shell内的方法
checkStatus
# $?:获取函数返回值或者上一个命令的退出状态
# -eq : 等于
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is already running, PID=${pid} <<<"
else
# nohup + & :后台运行
# >$LOG_NAME : 将日志信息输出到log日志中
# 0 表示stdin标准输入
# 1 表示stdout标准输出
# 2 表示stderr标准错误
# & 相当于等效于标准输出
# 2>&1 是将标准错误信息转变成标准输出,这样就可以将错误信息输出到out.log 日志里面来
# -noverify:关闭字节码验证
# java -jar XXX.jar :执行jar
nohup $JRE_HOME/bin/java -noverify -Xms512m -Xmx1024m -jar $JAR_NAME >$LOG_NAME 2>&1 &
# $! Shell最后运行的后台Process的PID
# '>' 为创建: echo “hello shell” > out.txt
# '>>' 为追加:echo “hello shell” >> out.txt
# 将进程的pid输出到.pid文件中(PID=$API_NAME\.pid)
echo $! > $PID
echo ">>> ${JAR_NAME} start successfully, PID=$! <<<"
fi
}
# 方法:debug方法
debugService(){
checkStatus
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is already running, PID=${pid} <<<"
else
nohup $JRE_HOME/bin/java -agentlib:jdwp=transport=dtSocket,server=y,suspend=n,address=5006 -Xms512m -Xmx1024m -jar $JAR_NAME >$LOG_NAME 2>&1 &
echo $! > $PID
echo ">>> ${JAR_NAME} start in debug mode successfully, PID=$! <<<"
fi
}
# 方法:停止方法
stopService(){
# cat读取PID文件
pidf=$(cat $PID)
#echo "$pidf"
echo ">>> ${JAR_NAME} begin kill, $pidf <<<"
# kill 杀进程
kill $pidf
# rm -rf : 删除文件
rm -rf $PID
# 休眠3s
sleep 3
checkStatus
if [ $? -eq "0" ]; then
echo ">>> kill failed, kill it forcefully <<<"
echo ">>> ${JAR_NAME} begin kill -9 $pid <<<"
# kill -9 强制杀进程
kill -9 $pid
sleep 3
# 无需确认,进程biss
echo ">>> ${JAR_NAME} has been killed <<<"
else
echo ">>> ${JAR_NAME} has stopped <<<"
fi
}
# 方法:重启
restartService(){
stopService
startService
}
# 方法:输出运行状态
statusService(){
checkStatus
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is running, PID=${pid} <<<"
else
echo ">>> ${JAR_NAME} is not running <<<"
fi
}
# 根据输入参数,选择执行对应方法,不输入则执行使用说明
# case expression in
# pattern 1)
# statement1
# ;;
# pattern 2)
# statement2
# ;;
# ……
# *)
# statementn
# esac
case "$1" in
"start")
startService
;;
"stop")
stopService
;;
"status")
statusService
;;
"restart")
restartService
;;
"debug")
debugService
;;
*)
formatDescription
;;
esac
exit 0
Pure Edition
export JAVA_HOME=/usr/java/jdk1.8.0_11
export JRE_HOME=$JAVA_HOME/jre
API_NAME=helloworld
JAR_NAME=$API_NAME\.jar
LOG_NAME=$API_NAME\.log
PID=$API_NAME\.pid
formatDescription() {
echo -e "===============Please use the following format==================\n"
echo -e ">>> sh service.sh [start|debug|stop|restart|status] <<<"
echo -e "\n==============================================================="
exit 1
}
checkStatus(){
pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
startService(){
checkStatus
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is already running, PID=${pid} <<<"
else
nohup $JRE_HOME/bin/java -noverify -Xms512m -Xmx1024m -jar $JAR_NAME >$LOG_NAME 2>&1 &
echo $! > $PID
echo ">>> ${JAR_NAME} start successfully, PID=$! <<<"
fi
}
debugService(){
checkStatus
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is already running, PID=${pid} <<<"
else
nohup $JRE_HOME/bin/java -agentlib:jdwp=transport=dtSocket,server=y,suspend=n,address=5006 -Xms512m -Xmx1024m -jar $JAR_NAME >$LOG_NAME 2>&1 &
echo $! > $PID
echo ">>> ${JAR_NAME} start in debug mode successfully, PID=$! <<<"
fi
}
stopService(){
pidf=$(cat $PID)
echo ">>> ${JAR_NAME} begin kill, $pidf <<<"
kill $pidf
rm -rf $PID
sleep 3
checkStatus
if [ $? -eq "0" ]; then
echo ">>> kill failed, kill it forcefully <<<"
echo ">>> ${JAR_NAME} begin kill -9 $pid <<<"
kill -9 $pid
sleep 3
echo ">>> ${JAR_NAME} has been killed <<<"
else
echo ">>> ${JAR_NAME} has stopped <<<"
fi
}
restartService(){
stopService
startService
}
statusService(){
checkStatus
if [ $? -eq "0" ]; then
echo ">>> ${JAR_NAME} is running, PID=${pid} <<<"
else
echo ">>> ${JAR_NAME} is not running <<<"
fi
}
case "$1" in
"start")
startService
;;
"stop")
stopService
;;
"status")
statusService
;;
"restart")
restartService
;;
"debug")
debugService
;;
*)
formatDescription
;;
esac
exit 0