1. shell函数参数传递不需要在参数列表,是通过$1, $2….这样获取,如

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

function deploy_one_host() # 函数定义,小括号不是必须的
{
cd wps_pic
zip_file_name=wps_pic_`date +%Y%m%d%H%M%S`.zip
zip "$zip_file_name" ./* -q
scp $zip_file_name root@$1:/tmp # 获取第一个参数
ssh root@$1 "sh exec.sh $zip_file_name;" # ssh 在远端执行命令,多个以分号分隔
}

deploy_one_host 10.229.26.143 # 传递一个参数,多个可在后面添加,以空格分隔

2. shell 命令行参数

1
2
3
#!/bin/bash

echo $1, $2 # 获取命令行参数,第一个第二个, 如果没有就为空

执行:

./exec.sh a b

3. if 判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function check()
{
if [ ! -d "$release_name" ]; then
echo "$release_name"" not exists, check the online dir"
return
fi
if [ -z "$1" ]; then
echo "must give the dir"
return
elif [ ! -d "$1" ]; then
echo "$1" "not exists please give the dir will online"
return
else
echo "check ok"
do_online $1
fi
}

-d 判读是不是一个目录,如果是为真

-f 判断是不是一个普通文件,是为真

-z 判断字符的长度是不是为0,为0则为真

==/!= 字符串相等/不等