shell脚本笔记--根据进程名关键字kill对应的进程

shell基本概念:

A Unix shell is a command-line interpreter or shell that provides a traditional Unix-like command line user interface.
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell

  从语言角度上来说,shell就是执行在类Unix系统上的命令行语言。而shell脚本就是有一组合法的shell命令来组成。

脚本编写基础语法:

1、流程控制:

 if判断格式:

if [ condition1 ]             # 此处为判断条件,中括号内部首尾都要有一个空格
then
    command1           # 此处为该条件下所要执行的命令。多条命令以 换行 或 ; 作为分隔
    command2
elif [ condition2 ]
then 
    command3
    command4
else
    commandN
fi                 # 注意fi结束

 for循环格式:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

while循环:

while condition
do
    command
done

调用shell脚本的方法: 

1、直接输入脚本所在路径进行调用;
  前提:

      1)脚本第一行添加    #!/bin/bash    ,来告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell

      2)使用chmod +x 文件路径  给使用者赋予执行该脚本的权限

2、直接运行解释器,后面加上 shell 脚本的文件名为参数;

     e.g.  sh test.sh

调用shell脚本时传参:

1、在脚本路径后,输入参数
2、多个参数以空格分隔
3、在脚本内部,$0表示获取该脚本名,$1表示获取调用脚本时传的第一个参数   $2表示调用脚本时传的第二个参数。。以此类推

e.g.
sh test.sh 第一个参数 第二个参数 第三个参数

实例--根据进程名关键字kill对应的进程:

#! /bin/bash
# kill进程函数 killProcess(){
echo "filename:$0"; echo "firstParameter:$1" echo "firstParameter:$2" echo "firstParameter:$3" if [ $1 = 'kill供货打桩' ]; then
     # ps和grep和awk组合过滤出进程id,作为参数传给xargs kill -9 ps -aux |grep 'uwsgi --http :8001 --chdir /root/Desktop/Suppli'|grep -v grep|awk '{print $2}'|xargs kill -9; ps -aux |grep 'uwsgi --http :8001 --chdir /root/Desktop/Suppli'; elif [ $1 = 'kill自动化定时' ]; then ps -aux | grep '/usr/sbin/CROND -n' | grep -v grep|awk '{print $2}' | xargs kill -9; ps -aux | grep 'python /root/Desktop/Automation/MainThread.py'| grep -v grep | awk '{print $2}' | xargs kill -9 ; ps -aux | grep 'python /root/Desktop/Automation/MainThread.py' fi } killProcess $1 $2 $3 # 传参调用函数

猜你喜欢

转载自www.cnblogs.com/minerrr/p/10576442.html