Shell脚本(持续更新)


# Shell 脚本是什么? Shell是一个命令解释器,它的作用是解释执行用户输入的命令及程序等,也就是说,我们用户每输入一条命令,Shell 就会相对应的执行一条命令。当命令或程序语句不在命令行下执行,而是通过一个程序文件来执行时,该程序文件就被称为Shell脚本。

1.查看自己当前系统默认的 Shell

echo $SHELL

输出:/bin/bash

2.查看系统支持的Shell

cat /etc/shells

输出:/bin/sh /bin/bash /usr/bin/sh /usr/bin/bash

怎么写Shell脚本

mkdir /usr/local/shelltest
touch test.sh
vim test.sh
#-----------------------test.sh内容开始-------------------------
#!/bin/sh
#  #! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell
echo "Hello World Shell"
#-----------------------test.sh内容结束-------------------------
# 执行脚本
./test.sh

# 注意:如果执行报错sh: ./test.sh: Permission denied 会提示这个,也就是没有授权定义
# 授权命令
chmod +x test.sh
# 然后再执行脚本就不报错了
./test.sh

Shell的流程控制

IF

if ...
then
    ...
else if ...
then
	...    
else
    ...
fi
# 例如:
if [ $1 -gt 2 ];

then
        echo "值大于2"
else
        echo "值小于2"
        exit
fi
# 结果
[root]# sh test2.sh 1
值小于2
[root]# sh test2.sh 3
值大于2

解释一下:

  • $1是我们传入的变量
  • -ge标识的是大于等于符号;
  • -le表示的是小于等于符号;
  • -gt 表示大于符号;
  • -lt 表示小于符号;
  • -eq 表示等于符号;
  • -ne 表示不等于符号;

我们给脚本传入一个值,然后比对这个值和2的大小关系,然后输出我们指定的内容。

Case

casein
匹配值1)
    command1
    command2
    
    ;;
匹配值2)
    command1
    command2
    ;;
esac
# 例如:
case $1 in
  
start)
        #输出启动命令
        echo "start已经开始"
        ;;
stop)
        #输出停止命令
        echo "stop命令执行"
        ;;
esac
exit
# 结果
[root]# sh service.sh start
start已经开始
[root]# sh service.sh stop
stop命令执行

脚本的意思是:匹配我们传入的第一个字符,和 start 还有 stop 进行比较,如果匹配上之后,输出命令,最后退出即可。

For

for i in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
# 例如:
j=$1
for ((i=1;i<=j;i++))
do
        echo $i
done
# 结果
[root]# sh fortest.sh 6
1
2
3
4
5
6

While

while condition
do
    command
done
# 例如:
a=1
b=1
while ((a <=9))
do
        while ((b<=a))
                do
                        let "c=a*b"   #声明变量c
                        echo -n "$a*$b=$c "
                        let b++
                done
                        let a++

     let b=1 #因为每个乘法表都是1开始乘,所以b要重置

             echo "" #显示到屏幕换行

     done
# 结果
[root]# sh whileTest.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

案例

jar包启动基础脚本

#!/bin/bash
# 自定义变量
SpringBoot=ics-customer-biz.jar

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未输入操作名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

if [ "$SpringBoot" = "" ];
then
    echo -e "\033[0;31m 未输入应用名 \033[0m"
    exit 1
fi

function start()
{
	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
	if [ $count != 0 ];then
		echo "$SpringBoot is running..."
	else
		echo "Start $SpringBoot success..."
# &> 将标准输出和标准错误输出都重定向到nohup.out中,>是覆盖,>>是追加
# --是强制生效的参数配置,server.port是指定端口		
                nohup java -jar $SpringBoot --server.port=6066 &>nohup.out &
	fi
}

function stop()
{
	echo "Stop $SpringBoot"
	boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

	if [ $count != 0 ];then
	    kill $boot_id
    	count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

		boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
		kill -9 $boot_id
	fi
}

function restart()
{
	stop
	sleep 2
	start
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "$SpringBoot is not running..."
    fi
}

case $1 in
	start)
	start;;
	stop)
	stop;;
	restart)
	restart;;
	status)
	status;;
	*)

	echo -e "\033[0;31m Usage: \033[0m  \033[0;34m sh  $0  {start|stop|restart|status}  {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
	  \033[0;33m sh  $0  start esmart-test.jar \033[0m"
esac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值