Jesse's home


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

Shell脚本特殊变量

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

shell脚本特殊变量

变量名 含义
$0 当前脚本的文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2……第n个参数是$n
$# 传递给脚本或函数的参数个数
$* 传递给脚本或函数的所有参数。
$@ 传递给脚本或函数的所有参数。被双引号(“ “)包含时,与 $* 稍有不同,下面将会讲到。
$? 上个命令的退出状态,或函数的返回值。一般情况下,执行成功会返回 0,失败返回 值是个大于1的整数
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID

阅读全文 »

Shell的脚本参数

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

Shell的脚本参数

本章主要介绍2个命令

  • shift
  • getopts

一.shift

shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理.

在使用shift时,默认情况他会将参数变量向左移动一个位置,所以变量$3会移动到$2,$2移动到$1,$1会被删除.($0是脚本名)

举个例子:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]$vim shift.sh

#!/bin/bash

count=1

while [ -n "$1" ];do
echo "Parameter #$count = $1 "
let count=$count+1
shift
done

加入一些参数去执行这个脚本:

1
2
3
4
5
6
[root@localhost ~]$bash shift.sh jesse jerry jack tom tony
Parameter #1 = jesse
Parameter #2 = jerry
Parameter #3 = jack
Parameter #4 = tom
Parameter #5 = tony

可以看到每次echo一个参数后,shift就移掉最左边的一个参数,直到$1为空,退出循环

当然也可以一次移动多个位置.

稍微改动一下刚才的脚本

阅读全文 »

shell脚本模板

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

shell脚本模板

介绍

为了保持良好的脚本编写习惯,在编制一个脚本时,需要有以下参数:

  • 脚本用途描述
  • 脚本作者
  • 脚本版本
  • 脚本更新记录
  • 脚本编写时间

下面是一个常见的脚本头部内容:

1
2
3
4
5
6
#!/bin/bash
#Description: this is the standard format for the head part of scripts
#Author: Jesse
#Version:0.1
#Datetime:2016-10-31
#Name:script.sh

阅读全文 »

shell脚本导入外部文件

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

shell脚本有时需要读取文件内容.下面介绍了从外部读取文件,引入循环语句处理该文件的几种常见方法

1.文本重定向

1
2
3
4
5
定义一个文件内容:
[root@localhost ~]$cat users.cvs
tom
jesse
tony

shell脚本从该文件读取文件的每行内容,且创建相关账户

阅读全文 »

Bash数组

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

Bash数组

Bash中的数组介绍

数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组)。

与大部分编程语言类似,数组元素的下标由0开始。


阅读全文 »

Rsync差异同步目录结构

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

Rsync同步本地和远程主机有差异的目录结构

需求

将BETA服务器的代码和配置文件数据迁移到IDC环境,IDC的BETA服务器已经存在一份数据,不能将本机所有的数据全部同步过去。一来耗时,占用带宽。二来会覆盖IDC BETA上正在使用的数据

所以,

1.需要判断如果对方(IDC BETA)如果不存在某个文件或者目录,则同步过去。

2.不能将目录上所有数据传递过去,因为代码目录包含很多不需要的旧版本releases的发布记录,全部同步过去耗费带宽和时间。

脚本内容

我直接在命令行操作的,没有写shell脚本。但是命令差不多

1.本地/data/apps/下的代码目录,如果对方服务器并不存在,则同步结构过去。

1
2
3
cd /data/apps

for dir in $(ls);do if ! `ssh work@172.16.20.1 "ls -d /data/apps/$dir" > /dev/null 2>&1`;then rsync -avz $dir --include '*/' --exclude '*' work@172.16.20.1:/data/logs/;fi;done

以下是2个知识点:

1
ssh work@iP COMMAND   #可以在本地远程执行shell命令

以下命令只同步目录结构(包括所有子目录),但是不同步文件

1
rsync -avz LOCAL_PATH --include '*/' --exclude '*' work@IP:REMOTE_PATH

那么同样,同步nginx配置文件.这个时候因为是同步文件,所以使用scp命令.

1
2
3
cd /etc/nginx/conf.d/

for dir in $(ls);do if ! `ssh work@172.16.20.1 "ls /data/conf/nginx/conf.d/$dir" > /dev/null 2>&1`;then scp $dir work@172.16.20.1:/data/conf/nginx/conf.d/;fi;done

修改配置文件V3.0

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

修改配置文件V3.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
#description: this script is to modify the arguments of database in all PHP config files
#author:huangyong
#date:2018-10-23
#该脚本在之前的脚本上在2个地方进行了优化:
#1.不再仅限于在具体某个目录下去寻找配置文件.而是在current.shared目录中去寻找
#2.find忽略了releases目录,这样不会去修改老版本的配置文件



p_dir=/data/apps/ #Parent dir


#找出父目录下的所有PHP业务目录
files=$(ls $p_dir)

for file in $files;do
cd $p_dir$file
app_conf=$(find current/ shared/ -name "application.ini" ) #在current shared目录查找配置文件.指定目录是避免查找release目录.

#find的命令可以忽略某个目录: find . ! -path "./xxx/*" ! -path "./xxxxx/*"

if [ ! -z "$app_conf" ];then #如果存在application配置文件,就替换redis配置项.前面2个是好食期的redis配置..后面两个是觅食蜂项目的
for conf in $app_conf;do
sed -ri '/redis.config.isauth/s#(=\s*).+$#\1true#' $conf
sed -ri '/redis.config.auth/s#(=\s*).+$#\1"Iamyourdaddy"#' $conf
sed -ri '/redis.default.config.isauth/s#(=\s*).+$#\1true#' $conf
sed -ri '/redis.default.config.auth/s#(=\s*).+$#\1"Iamyourdaddy"#' $conf

done
fi

par_conf=$(find current/ shared/ -name "parameters.yml") #查找parameters配置文件,并进行替换

if [ ! -z "$par_conf" ];then
for par in $par_conf;do
sed -ri '/redis_dsn/s#(:\s*).+$#\1"redis://Iamyourdaddy@127.0.0.1:6379"#' $par
done

fi

done

使用inotify监控新创建的文件

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

使用inotify监控新创建的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#!/bin/bash
#description:监测某个路径下的是否有新创建的文件.如果有新创建的文件,则判断是否以某个固定的前缀开头.
# 如果是需要监测的文件,则发邮件通知给用户,文件已经创建,且通告文件名,文件创建日期等信息.
# 此脚本只监测一台服务器,一个目标文件夹,一个create的属性...........
#date:2018-03-29

#v2.0 多了个数组,有多个文件前缀.只要匹配其中任何一个就是监测的文件目标

src=/tmp
file_start_with=("SBD_sbdBalFinancing_LDAYSTMTDTL","INCOME_INFO_SBD_227","PRODUCT_INFO_SBD_227","TRANS_INFO_SBD")
email_user="mailhuangyong@163.com"
log_file="/tmp/monitorfile.log"

if ! `which inotifywait > /dev/null 2>&1` ;then
echo "sorry,inotify is not installed "> $log_file
exit 1
fi

if ! `which mail > /dev/null 2>&1`;then
echo "sorry,mail is not installed"> $log_file
fi

#判断文件名的前缀是否符合监测目标数组函数
function check_file () {
file_start=$(echo $1 | sed -r 's/(.*)_[0-9]*\.zip/\1/')
if [ ${file_start_with[@]/$file_start/} != ${file_start_with[@]} ];then
return 0
else
return 1
fi

}


#发送邮件
function send_mail () {
echo -e "Filename is : $1\nFile create time is : $2" |mail -s "receive file succeed!" $email_user

}


#inotify监测目录下文件是否有改动.主要监测:文件名或者目录创建(这里我没有监测文件删除,修改,移动,权限等).
#将监测到的文件重定向到while循环.
inotifywait -rm --format '%Xe %f' -e create $src | while read file;do

#获取文件名
mon_file=$(echo $file | awk '{print $2}')
#判断文件名是否符合固定前缀
check_file $mon_file

#接收check_file函数返回结果,如果是监测的文件已经被创建,就发送邮件
if [ $? -eq 0 ];then
file_time=$(stat $src/$mon_file | awk -F '[" ".]' '/Change/ {print $2,$3}')
send_mail $mon_file "$file_time"
[[ $? -ne 0 ]] && echo "send email failed" > $log_file
fi

done

监控某个进程端口,邮件告警

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

监控某个进程端口,邮件告警

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#despcrition:Our MQ server encountered some troubles which sometimes 5672 port was unreachable from this server.So run a shell script to detect the connection
#author:huang yong
#date: 2018-03-12

#define variables
port=5672
host=10.47.115.13
email_user="huangyong@doweidu.com"
#email_title="rabbitmq connection lost"
email_send=0

#send email
function send_email () {
echo "$(nmap -p $port $host)" | mail -s $1 $email_user

}

while true;do
nmap -p $port $host | grep "${port}/tcp open" > /dev/null
if [ $? -ne 0 ];then
if [ $email_send -eq 0 ];then
send_email "rabbitmq connection lost"
email_send=1
fi
else
[ $email_send -eq 1 ] && send_email "rabbitmq connection succeed" && email_send=0
fi

sleep 5

done

所有目录重命名

发表于 2018-06-12 | 分类于 Linux-Basic , shell&shell脚本 |

所有目录重命名

需求

将下列目录名批量重命名..

如果目录名包含beta,则将beta改为testing.

例如:beta,beta1,beta2,等改成testing,testing1,testing2

如果目录名不包含beta.则就直接在现有目录名后面追加testing

脚本内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
#description:修改app目录下的业务名称.如果目录名没有beta就直接重命名成testing.如果有beta就把beta改成testing

dir=/data/apps
files=$(ls $dir)

for file in $files;do
if `echo $file | grep "beta" > /dev/null`;then #如果当前目录名包含beta字段,或者beta1,beta2......
new_file=$(echo $file | sed -re 's#(.*)beta(\s*)+#\1testing\2#') #将beta,beta1,beta2更改为testing,testing1,testing2
mv $dir/$file $dir/$new_file #重命名目录名为testing

else #如果目录名不包含beta,则直接在后面追加testing
mv $dir/$file $dir/$file-testing
fi
done
1…192021
Jesse

Jesse

求知若饥,虚心若愚.

209 日志
44 分类
41 标签
RSS
© Tue Jun 12 2018 08:00:00 GMT+0800 (GMT+08:00) — 2021 Jesse