运维Shell脚本小试牛刀(一)
运维Shell脚本小试牛刀(二)
运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解
运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客
path = $(cd $(dirname $0); pwd)解析:
1、取当前运行脚本的所在路径: $0
2、取当前脚本所在路径的父目录: dirname
3、取返回的父目录的值: $(dirname $0)
4、cd到返回的父目录: cd “$(dirname “$0”)”
5、输出地址: $(cd $(dirname $0); pwd)
6、取输出的地址,并赋值给path: path = $(cd (dirname $0),pwd)
$0 获取运行脚本文件所在路径,在命令行种执行结果为 -bash
[root@www ~]# echo "$0"
-bash
Linux中变量#,@,0,1,2,*,$$,$?的含义
$# 是传给脚本的参数个数
$
0
是脚本本身的名字
$
1
是传递给该shell脚本的第一个参数
$
2
是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过
9
个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,
0
表示没有错误,其他表示有错误
区别:@@*
- 相同点:都是引用所有参数
- 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数(分别存储在112 3)则"3)则"*" 等价于 “112 3"(传递了一个参数);而“3"(传递了一个参数);而“@" 等价于 "1""1""2" "$3"(传递了三个参数)
小试牛刀一:
[root@www tools]# cat commandList.sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
echo "show parm list:$*"
echo "show process id:$$"
echo "show precomm stat: $?"
输出:
[root@www example]# ./commandList.sh 1 20 111 2 5 6 7 3
number:8
scname:./commandList.sh
first :1
second:20
argume:1 20 111 2 5 6 7 3
show parm list:1 20 111 2 5 6 7 3
show process id:3465
show precomm stat: 0
小试牛刀二:
[root@www example]# cat commandOutList.sh
#!/bin/sh
num=$#
name=$0
echo "number:$num"
echo "scname:$name"
echo $0
echo $1
echo $2for ((i=0; i<$num; i++))
do
echo "$i"
doneecho "argume:$@"
for key in $@
do
echo $key
done
echo "-----------------"
for key in "$@"
do
echo $key
done
echo "-----------------------------"
for key2 in $*
do
echo $key2
done
echo "-----------------"
for key2 in "$*"
do
echo $key2
doneecho "show process id:$$"
echo
echo "show precomm stat: $?"
输出内容:
[root@www example]# ./commandOutList.sh
number:0
scname:./commandOutList.sh
./commandOutList.sh
argume:
-----------------
-----------------------------
-----------------show process id:3514
show precomm stat: 0
[root@www tools]# sudo yum install rsync -y
rsync和scp区别:用rsync做文件的复制要比scp的速度快,rsync只对差异文件做更新。scp是把所有文件都复制过去。
创建分发脚本文件:
xsync 脚本内容
[root@www tools]# cat ~/bin/xsync
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器|docker0|docker1|docker2|
for host in docker0 docker1 docker2
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
小试牛刀一:
将jdk分发到docker1,docker2服务器上
[root@www bin]# ./xsync /usr/local/tools/jdk-8u371-linux-x64.tar.gz
==================== docker0 ====================
sending incremental file listsent 66 bytes received 12 bytes 156.00 bytes/sec
total size is 139,219,380 speedup is 1,784,863.85
==================== docker1 ====================
sending incremental file list
jdk-8u371-linux-x64.tar.gzsent 139,253,473 bytes received 35 bytes 39,786,716.57 bytes/sec
total size is 139,219,380 speedup is 1.00
==================== docker2 ====================
sending incremental file list
jdk-8u371-linux-x64.tar.gzsent 139,253,473 bytes received 35 bytes 39,786,716.57 bytes/sec
total size is 139,219,380 speedup is 1.00
小试牛刀二:[root@www bin]# ./xsync /usr/local/zookeeper/
将解压后的zookeeper文件全拷贝到docker1|docker2机器:
[root@www bin]# ./xsync /usr/local/zookeeper/
==================== docker0 ====================
sending incremental file listsent 61,605 bytes received 227 bytes 123,664.00 bytes/sec
total size is 390,744,347 speedup is 6,319.45
==================== docker1 ====================
sending incremental file listsent 61,613 bytes received 235 bytes 123,696.00 bytes/sec
total size is 390,744,347 speedup is 6,317.82
==================== docker2 ====================
sending incremental file listsent 61,613 bytes received 235 bytes 123,696.00 bytes/sec
total size is 390,744,347 speedup is 6,317.82