Linux之shell脚本编程(二)
9.命令排序
(1) ; 拼接多个命令没有逻辑关系
[[email protected] test]# ll 123;sh useradd.sh
- && 拼接多个命令,有逻辑关系 第一个命令不能执行时,第二个命令也就不能执行
[[email protected] test]# sh useradd.sh && ll 123
[[email protected] test]# ll 123 && sh useradd.sh
- || 拼接命令,有逻辑关系,
[[email protected] test]# ll 123 || sh useradd.sh
注意:
command & 后台执行
command &>/dev/null 混合重定向(标准输出1,错误输出2)
command1 && command2 命令排序,逻辑判断
10.通配符及转移
* :
[] :
[^] :
{} 集合
cp复制的三种办法。
(1)[[email protected] boot]# cp -vp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens32.bak"
/etc/sysconfig/network-scripts/ifcfg-ens32" -> "/etc/sysconfig/network-scripts/ifcfg-ens32.bak"
(2)[[email protected] boot]# cp -vp /etc/sysconfig/network-scripts/{ifcfg-ens32,ifcfg-ens32.bak1}
"/etc/sysconfig/network-scripts/ifcfg-ens32" -> "/etc/sysconfig/network-scripts/ifcfg-ens32.bak1"
(3)[[email protected] boot]# cp -vp /etc/sysconfig/network-scripts/ifcfg-ens32{,.bak2}
"/etc/sysconfig/network-scripts/ifcfg-ens32" -> "/etc/sysconfig/network-scripts/ifcfg-ens32.bak2"
[[email protected] ~]# echo -e "a\tb"
a b
[[email protected] ~]# echo -e "a\tb"
a b
[[email protected] ~]# echo -e "a\nb"
a
b
[[email protected] ~]# echo -e "anb"
anb
[[email protected] ~]# echo -e 'a\nb'
a
b
[[email protected] ~]# echo -e "a\nb"
a
b
11.位置变量
$0,
$1,$2 .... $9,${10},${11} ... # 接收脚本或者函数的位置参数。
$0 文件名,若执行脚本时带有路径,会将路径和将本名同事输出。
$# 表示参数的个数
实例:
[[email protected] test]# vim random.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 09:11:09
#Name:random.sh
#Version:V1.0
#Description:This is a test script.
function rand(){
min=$1 #40000
max=$(($2-$min+1)) #500000-400000+1 10000
num=$(($RANDOM+1000000000)) #增加一个10位的数再求余 #4012 +10000000000 = 1000004012
echo $(($num%$max+$min)) # 1000004012%100001 +400000 < 100001 +400000 < 500000
}
#read -p "please input your num of start:" sta
#read -p "please input your num of end:" sto
#rnd=$(rand $1 $2)
if [ $# -eq 2 ] #$# :用户传递的参数个数
then
rnd=$(rand $1 $2) # $1 $2 :用户传递的位置参数
echo $rnd
exit 0
else
echo "parameter is not anough,[ $0 parameter1 parameter2]" #$0 :脚本名
fi
实例:
[[email protected] test]# vim var1.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 10:36:24
#Name:var1.sh
#Version:V1.0
#Description:This is a test script.
#echo $0
#echo $1
#echo $2
#echo $3
#echo $#
# $*,[email protected] 获取所有参数。
:<<BLOCK
n=0
for i in "$*" #"$*" 表示把所有参数当成一个整体,[email protected]表示所有参数是独立的个体。
do
echo $i
let n++
done
echo $n #统计循环次数
#echo $*
BLOCK
n=0
for i in "[email protected]" # "[email protected]",[email protected] 表示所有参数是独立的个体。
do
echo $i
let n++
done
echo $n
12.dirname,basename
[[email protected] test]# dirname /root/test/var2.sh
/root/test
[[email protected] test]# basename /root/test/var2.sh
var2.sh
实例:
[[email protected] test]# vim var2.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 11:23:32
#Name:var2.sh
#Version:V1.0
#Description:This is a test script.
echo $0
echo "`dirname $0`" #dirname 打印路径
echo "`basename $0`" #basename 打印文件名
13.状态变量
$? :(1)判断上一条命令是否执行成功,成功则返回0,不成功则返回非0;
(2)获取脚本的exit的退出码
(3)获取的是函数的返回值
实例:监控web服务状态是否正常
思路:a 判断进程:
[[email protected] test]# ps -ef |grep httpd
[[email protected] test]# killall -0 httpd
b 判断端口:
[[email protected] test]# netstat -tunalp |grep 80
c 判断链接:
[[email protected] test]# curl 127.0.0.1
补充:
killall命令:用来杀死进程,终止进程
killall 进程名 #杀死进程
Killall -0 进程名 #检测进程是否运行
[[email protected] test]# vim monitor_httpd.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 11:45:41
#Name:monitor_httpd.sh
#Version:V1.0
#Description:This is a test script.
service=httpd
while true
do
killall -0 $service &>/dev/null
#curl 127.0.0.1 &>/dev/null
#`ps -ef |grep httpd |grep -v grep` &>/dev/null
#`netstat -tulanp |grep 80` &>/dev/null
if [ $? -eq 0 ]
then
echo "httpd is up ..."
else
echo "httpd is down ..."
fi
sleep 1
done
实例:
[[email protected] test]# cat state1.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 14:56:47
#Name:state1.sh
#Version:V1.0
#Description:This is a test script.
testfun(){
#i=$1
if (($1<200))
then
echo "$1"
#exit 1
return 1
fi
}
testfun $1
#echo $?
if [ $? -eq 1 ]
then
echo "输对了。。。"
else
echo "输错了。。。"
fi
$$: 获取当前脚本的进程号
实例:
[[email protected] test]# cat test_ping.sh
#!/bin/bash
#Author:axiang
#Blog:
#Time:2020-06-20 15:49:20
#Name:test_ping.sh
#Version:V1.0
#Description:This is test script.
if [ -f /tmp/pid.ping ]
then
kill `cat /tmp/pid.ping`
echo $$ >/tmp/pid.ping
else
echo $$ >/tmp/pid.ping
fi
while true
do
for i in `cat IP.txt`
do
#echo $i
sleep 5
ping -c 1 $i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i tong ..." >> ping.log
else
echo "$i not tong ..." >> ping.log
fi
done
#sleep 5
#echo $$ > /tmp/pid.ping
done
#rm -rf /tmp/pid.ping
$! :获取上一个在后台运行的进程PID
$_ :获取上一个命令的最后一个参数
$* , [email protected], $n,
14.常见的内置命令
a echo -e :解析转义字符
-n :不换行输出
rnd=$(rand $1 $2)
echo -ne "随机数是: \t$rnd"
echo -e "\t请拿好..."
exit 50
b exec :在不启动子进程的前题下运行命令,执行完后杀死当前进程。
实例:
[[email protected] test]# cat read.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-20 17:04:26
#Name:read.sh
#Version:V1.0
#Description:This is a test script.
exec < IP.txt
while read line
do
echo $line
done
echo OK
c read -p :读取用户输入的内容
d shift :移动位置参数
实例:企业垃圾桶参数偏移
[[email protected] test]# cat rm.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-20 17:27:37
#Name:rm.sh
#Version:V1.0
#Description:This is a test script.
[ -d /dev/mynull ] || mkdir /dev/mynull
shift
mv $1 /dev/mynull
e exit n (数字)
$?
15.字符串操作
定义字符串:
[[email protected] test]#a=daahufsuufsdghfgdiha
[[email protected] test]#a="cnsjv dsjf dshs" (有空格的变量,可以用””定义。和变量一样。)
[[email protected] test]#a='afnkjs$a##w*l' //缺点强引用,特殊字符不能表达其含义。
取消变量:unset a:
查看字符串:(cat是查看文件和目录的,变量用echo $a查看)
[[email protected] test]# echo $a
[[email protected] test]# echo ${a}
afhkjsfjsdfjdskfasli$alkdahkjf
获取字符串长度:
[[email protected] test]# echo ${a}
wolikelinux
[[email protected] test]# echo ${#a}
11
[[email protected] test]# expr length "$a"
11
截取子串(# / %在其中的实意):
[[email protected] test]# echo ${a}
wolikelinux
[[email protected] test]# echo ${a:2} #从第3个位置开始取子串
likelinux
[[email protected] test]# echo ${a:2:4} #从第3个位置开始取4个字符
like
[[email protected] test]# echo ${a#wo} #删匹配
likelinux
[[email protected] test]# echo ${a#w*l} #从左向右删除最短匹配
ikelinux
[[email protected] test]# echo ${a##w*l} #从左向右删除最长匹配
Inux
[[email protected] test]# echo ${a%l*x} #从右往左删除最短匹配
wolike
[[email protected] test]# echo ${a%%l*x} #从右往左删除最长匹配
wo
[[email protected] test]# echo ${a/l/5} #从左向右替换第一个匹配到的字符
wo5ikelinux
[[email protected] test]# echo ${a//l/5} #从左往右替换所有匹配到的字符
wo5ike5inux
[[email protected] test]# echo ${a/l*x/5} #最大范围匹配到的字符替换
wo5
实例:批量修改文件名
知识1:如何修改shell中的for循环以空格分割为换行
知识2:如何用shell字符串的取子串操作处理文本
[[email protected] test]# echo ${a}
[[email protected] test]# echo ${a#*l}
ike linux
[[email protected] test]# echo ${a##*l}
inux
[[email protected] test]# echo ${a%%l*}
we
[[email protected] test]# echo ${a%l*}
we like
[[email protected] test]# cat file3.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-23 15:44:39
#Name:file3.sh
#Version:V1.0
#Description:This is a test script.
#"如何修改shell中的for循环,使其不以空格分割。而是以换行"
#IFS:
IFS_old=$IFS
IFS=$'\n'
for i in `ip a |grep -E '\<inet\>'` #for 循环默认以空格分割,---""
do
#echo $IFS
#echo $i
j=${i#*t}
echo ${j%%/*}
#echo ${j%%s*o}
done
IFS=$IFS_old
16.字符串的特殊变量扩展(= - + ?)
echo ${parameter:-word} :如果parameter为空,则输出word,parameter本身没有发生变化;如果不为空输出parameter的值。
echo ${parameter:=word} :如果parameter为空,则输出word,parameter已经赋予word的值;如果不为空输出parameter的值。
echo ${parameter:?word} :如果parameter为空,则输出word,此时word将是错误输出的提示,echo $? 返回值为1;如果不为空输出parameter的值。
echo ${parameter:+word} :如果parameter为空,则输出空,如果不为空输出word,parameter本身没有发生变化。
-与=的区别:
-:变量有值,输出变量值。若变量没有值则输出后面的值。变量a最终没值。a没变化
=:变量有值,输出变量值。若变量没有值则输出后面的值。变量a最终有值。a变化
[[email protected] test]# a=linux
[[email protected] test]# echo ${a}
linux
[[email protected] test]# echo ${a:-mysql}
linux
[[email protected] test]# unset a
[[email protected] test]# echo ${a}
[[email protected] test]# echo ${a:-mysql}
mysql
[[email protected] test]# unset a
[[email protected] test]# echo ${a}
[[email protected] test]# echo ${a:-mysql}
mysql
[[email protected] test]# echo ${a}
[[email protected] test]#
[[email protected] test]# echo ${a:=mysql}
mysql
[[email protected] test]# echo ${a}
mysql
[[email protected] test]# unset
[[email protected] test]# unset a
[[email protected] test]# echo ${a}
[[email protected] test]# echo ${a:?mysql}
-bash: a: mysql
[[email protected] test]# echo $?
1
[[email protected] test]# lll
-bash: lll: 未找到命令
[[email protected] test]# echo ${a:?"参数未定义"}
-bash: a: 参数未定义
[[email protected] test]# a=123
[[email protected] test]# echo ${a:?"参数未定义"}
123
[[email protected] test]# echo ${a}
123
[[email protected] test]#
[[email protected] test]#
[[email protected] test]# unset a #unset 删除变量,字符串
[[email protected] test]#
[[email protected] test]# echo ${a}
[[email protected] test]#
[[email protected] test]# echo ${a:+mysql}
[[email protected] test]# a=linux
[[email protected] test]# echo ${a}
linux
[[email protected] test]# echo ${a:+mysql}
mysql
[[email protected] test]# echo ${a}
linux
实例:Shell实现程序日志文件分割
日志文件过大:(1)资源浪费
(2)读写不方便,甚至后导致日志的输入延时而导致程序故障。
滚动切分:要求:(1)日志文件以当前日期命名:每小时生成一个新文件(以小时命名,并在存放在以天命令的文件夹中)
(2)每天凌晨时将前一天的日志文件归档压缩。20200623---> 00:00 20200623.tar.gz
(3)保留7天之内的日志,7天之外的删除。 20200623.tar.gz,20200624.tar.gz,20200625.tar.gz,20200626.tar.gz,
20200627.tar.gz, 20200628.tar.gz, 20200629.tar.gz, 20200630.tar.gz.
补充:定时计划任务坑:
坑1:[[email protected] ~]# vim /var/spool/cron/root 这样做,权限不对,正确的方法是crontab -e;.vimrc.bak;
-rw------- 1 root root 9 6月 23 18:35 root
坑2:计划任务在执行时,是在自己的家目录下执行的,所以脚本中一定要写绝对路径。
[[email protected] test]# crontab -e
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#
0 */1 * * * . /etc/profile;/root/test/ping_log.sh
#
0 0 */1 * * . /etc/profile;/root/test/ping_log1.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-23 17:51:43
#Name:ping_log.sh
#Version:V1.0
#Description:This is a test script.
DirName=`date "+%Y%m%d"`
FileName=`date "+%H"`
[ -d /root/test/$DirName ] || mkdir /root/test/$DirName
mv /root/test/ping.log /root/test/$DirName/$FileName.log
[[email protected] test]# cat ping_log1.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-23 17:51:43
#Name:ping_log.sh
#Version:V1.0
#Description:This is a test script.
DataPath=/root/test
#DataPath=
SourceFile=`date -d "-1 day" "+%Y%m%d"`
#tar -czf $DataPath/$SourceFile.tar.gz $DataPath/$SourceFile &>/dev/null
#rm -rf $DataPath/$SourceFile
#find $DataPath -mtime -7 exec rm -rf {} \;
find ${DataPath:-/tmp/} -mtime +3 -exec rm -rf {} \;
17.shell中的数值计算
命令:
a (()) :既能做数值运算,又能做数值比较
[[email protected] test]# a=$((1+1))
[[email protected] test]# echo a
a
[[email protected] test]# echo $a
2
if ((1>2))
then
echo "ok"
else
echo "not ok"
fi
b let :数值运算
[[email protected] test]# let a=1+2
[[email protected] test]# echo $a
3
c expr :数值运算(判断是否为数字),字符串对比(判断两字符串是否一致),求字串长度
[[email protected] test]# expr 1 + 2
3
[[email protected] test]# expr 1 \* 2
2
[[email protected] test]# expr 1 \/ 2
0
[[email protected] test]# expr 1 \% 2
1
字符串匹配。匹配的字符串以“:”相隔,匹配则为真,反之,为假。
[[email protected] test]# expr "123" : "124" &>/dev/null
[[email protected] test]# echo $?
1
[[email protected] test]# expr "123" : "123" &>/dev/null
[[email protected] test]# echo $?
0
[[email protected] test]# a="mamahuhu"
[[email protected] test]# echo ${a}
mamahuhu
方法一:(最高)
[[email protected] test]# echo ${#a}
8
方案二:(和一相当)
[[email protected] test]# expr length $a
8
方法四:(较好)
[[email protected] test]# echo ${a} |awk '{print length($0)}'
8
方法三:(最差)
[[email protected] test]# echo ${a} |wc -L
8
[[email protected] test]# expr 1 + q &>/dev/null
[[email protected] test]# echo $?
2
[[email protected] test]# expr 1 + 1 &>/dev/null
[[email protected] test]# echo $?
0
[[email protected] test]# cat Calculator.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 01:05:12
#Name:Calculator.sh
#Version:V1.0
#Description:This is a test script.
read -p "num1>>" num1
read -p "num2>>" num2
if `expr 1 + $num1 &>/dev/null` && `expr 1 + $num2 &>/dev/null`
then
echo "$(($num1+$num2))"
echo "$(($num1-$num2))"
echo "$(($num1*$num2))"
echo "$(($num1/$num2))"
else
echo "请输入数字..."
exit 1
fi
c bc
[[email protected] test]# echo 0.5+0.5 |bc
[[email protected] test]# bc
d $[]
[[email protected] test]# a=$[1+1]
[[email protected] test]# echo $a
2
[[email protected] test]# a=$[1+2]
[[email protected] test]# echo $a
3
实例:
[[email protected] test]# cat web.monitor.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 12:00:48
#Name:web.monitor.sh
#Version:V1.0
#Description:This is a test script.
n=0
while (($n<2))
do
killall -0 httpd &>/dev/null
if [ $? -eq 0 ]
then
echo "ok..."
else
let n++ #0--1,1---2
echo "not ok ..."
#if [ 2 -le $n ]
#then
# exit 1
#fi
fi
sleep 1
Done
18.条件测试
test 表达式 #判断文件,目录等待参数(-f)的场景
[ 表达式 ] #判断文件,目录等待参数(-f)的场景;判断数值,-eq,-lt,-gt; -a,-o ,!
[[ 表达式 ]] #判断文件,目录等待参数(-f)的场景;判断数值,>,<,==; &&,||,!;匹配正则
((表达式)) #判断数值,>,<,==。
[[email protected] test]# [[ test.sh =~ *.sh ]] && echo "1" || echo "0"
0
[[email protected] test]# [[ test.sh =~ .*.sh ]] && echo "1" || echo "0"
1
[[email protected] test]# [ test.sh =~ .*.sh ] && echo "1" || echo "0"
-bash: [: =~: 期待二元表达式
0
[[email protected] test]# cat test2.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 12:34:00
#Name:test2.sh
#Version:V1.0
#Description:This is a test script.
#if [[ 1 < 2 && 2 > 3 ]]
#if [ 1 -lt 2 -a 2 -lt 3 ]
#if ((1>2))
if `test -f test.sh`
then
echo "ok"
else
echo "not ok.."
fi
文件测试表达式:
[[email protected] test]# [ -d /root ] && echo "1" || echo "0"
1
[[email protected] test]# [ -d /rooot ] && echo "1" || echo "0"
0
[[email protected] test]# [ -f /etc/passwd ] && echo "1" || echo "0"
1
[[email protected] test]# [ -f /ect/passwd ] && echo "1" || echo "0"
0
[[email protected] test]# [ -e /ect/passwd ] && echo "1" || echo "0"
0
[[email protected] test]# [ -e /etc/passwd ] && echo "1" || echo "0"
1
[[email protected] test]# [ -e /root ] && echo "1" || echo "0"
1
[[email protected] test]# [ -e /rooot ] && echo "1" || echo "0"
0
[[email protected] ~]$ [ -r 3 ] && echo "1" || echo "0"
0
[[email protected] ~]$ chmod +r 3
[[email protected] ~]$ [ -r 3 ] && echo "1" || echo "0"
1
[[email protected] ~]$ [ -s 3 ] && echo "1" || echo "0"
0
[[email protected] ~]$ vim 3
[[email protected] ~]$ du 3
4 3
[[email protected] ~]$ [ -s 3 ] && echo "1" || echo "0"
1
[[email protected] ~]$ [ test1 -nt test2 ] && echo "0" ||echo "1"
[[email protected] ~]$ [ test1 -ot test2 ] && echo "0" ||echo "1"
字符串测试表达式
[[email protected] ~]$ a=123
[[email protected] ~]$ [ -n "$a" ] && echo "1" || echo "0"
1
[[email protected] ~]$ unset a
[[email protected] ~]$ echo $a
[[email protected] ~]$ [ -n "$a" ] && echo "1" || echo "0"
0
[[email protected] ~]$ [ -z "$a" ] && echo "1" || echo "0"
1
[[email protected] ~]$ a=111
[[email protected] ~]$ [ -z "$a" ] && echo "1" || echo "0"
0
[[email protected] ~]$ [ "123" == "123" ] && echo "1" || echo "0"
1
[[email protected] ~]$ [ "123" == "113" ] && echo "1" || echo "0"
0
[[email protected] ~]$ [ "123" != "113" ] && echo "1" || echo "0"
1
[[email protected] ~]$ [ "123" != "123" ] && echo "1" || echo "0"
0
[[email protected] ~]$ expr "123" : "123"
3
[[email protected] ~]$ echo $?
0
[[email protected] ~]$ expr "124" : "123"
0
[[email protected] ~]$ echo $?
1
整数的二元比较及逻辑操作
[],test : -eq -ne -gt -ge -lt -le ;-a -o !
[[]],(()) : =,== != > >= < <= ;&& || !
19.条件判断
if
[[email protected] ~]# cat caiyicai.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 17:10:00
#Name:caiyicai.sh
#Version:V1.0
#Description:This is a test script.
while true
do
read -p "你猜老吕多大:>>>" age
expr $age + 1 &>/dev/null
if [ $? -eq 0 -a -n "$age" ]
then
if (($age>58))
then
echo "猜大了.."
elif (($age<58))
then
echo "猜小了.."
elif (($age==58))
then
echo "猜中了.."
exit 2
else
echo "请输入正确的数字.."
fi
else
echo "请输入数字..."
fi
done
服务状态判断:
(1)根据命令的返回值$?做判断
(2)netstat -tulanp |grep 80,ps -ef |grep httpd ;wc -l ;判断数字
case:
yum install rsync
启动: rsync --daemon
停止: pkill rsync
vim RSYNC.sh
chmod +x RSYNC.sh
mv RSYNC.sh /usr/local/bin/RSYNC
[[email protected] ~]# cat /usr/local/bin/RSYNC
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 18:01:10
#Name:RSYNC.sh
#Version:V1.0
#Description:This is a test script.
case $1 in
start)
rsync --daemon
;;
stop)
pkill rsync
;;
restart)
pkill rsync
sleep 2
rsync --daemon
;;
*)
echo "*"
;;
esac
20.输出内容添加颜色
echo -e "\033[5;42;31m USER: `basename $0` [start|stop|restart] \033[0m"
echo -e "\033[42;31m USER: `basename $0` [start|stop|restart] \033[0m"
40-47;30-37
21.函数
函数定义:
[[email protected] ~]# cat test_fun.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 18:41:27
#Name:test_fun.sh
#Version:V1.0
#Description:This is a test script.
function funname1(){
echo "Hello World.."
}
funname2(){
echo "Linux hello.."
}
function funname3
{
echo "shell hello.."
}
funname1
funname2
funname3
函数传参:
[[email protected] ~]# sh test_var.sh
var1
var2
Linux hello..
[[email protected] ~]# cat test_var.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 18:41:27
#Name:test_fun.sh
#Version:V1.0
#Description:This is a test script.
funname2(){
echo $1
echo $2
echo "Linux hello.."
}
funname2 var1 var2
返回值:
[[email protected] ~]# cat test_var.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 18:41:27
#Name:test_fun.sh
#Version:V1.0
#Description:This is a test script.
funname2(){
echo $1
echo $2
echo "Linux hello.."
return 121
}
funname2 var1 var2
echo $?
[[email protected] ~]# cat /usr/local/bin/RSYNC
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-29 18:01:10
#Name:RSYNC.sh
#Version:V1.0
#Description:This is a test script.
START(){
rsync --daemon
}
STOP(){
pkill rsync
}
RESTART(){
STOP
sleep 2
START
}
case $1 in
start)
START
;;
stop)
STOP
;;
restart)
RESTART
;;
*)
#echo -e "\033 [31m USER: `basename $0` [start|stop|restart] \033[0m"
echo -e "\033[5;42;31m USER: `basename $0` [start|stop|restart] \033[0m"
;;
esac
[email protected] data]# cat ping_log.sh
#!/bin/bash
#Author:Anliu
#Blog: https://i.cnblogs.com/posts?cateId=1583983
#Time:2020-06-23 17:51:43
#Name:ping_log.sh
#Version:V1.0
#Description:This is a test script.
SourceFile=`date -d "-1 day" "+%Y%m%d"`
DataPath=/root/test/
DirName=`date "+%Y%m%d"`
FileName=`date "+%H"`
SourcePath=`pwd`
SPIT(){
[ -d $DataPath$DirName ] || mkdir $DataPath$DirName
mv /data/ping.log $DataPath$DirName/$FileName.log
}
TAR(){
tar -czf $DataPath$SourceFile.tar.gz $DataPath$SourceFile &>/dev/null
rm -rf $DataPath$SourceFile
}
STAIL(){
find $DataPath -mtime +3 -exec rm -rf {} \; &>/dev/null
}
case $1 in
SPIT)
SPIT
;;
TAR)
TAR
;;
STAIL)
STAIL
;;
esac
试题:(3)服务器关键数据备份
22. 数组
(数组还有好多不会) 6.30继续整理
1.定义数组
array[0]=linux #位置定义
array=(wo like linux) #括号定义,默认空格分割
array=(wo like “java and linux”) #java and linux双引号里占一个位置
array=(wo like linux [2]=java #将Linux换成Java
array=(`cat /etc/passwd`) #默认空格分割
array=($red $blue) #数组中可以存储变量
2.查看数组
echo ${array[1]} #查看某一个位置
echo ${array[@]} #查看所有
查看系统定义的数组:#daclare -a
修改数组值:
修改某一位置:直接定义
declare不能修改某个位置,直接全部修改。
3.获取数组长度
echo ${#array[*]} # *和@都是获取所有
echo ${#array[@]} #获取数组元素个数
echo length=${#array[*]} #获取数组元素个数
echo length=${#array[1]} #查看单个数组的长度
4.数组的删除 和变量删除一样
unset array[2] #删除某一位置
unset array #删除整个数组
5.数组的遍历
方法一:
方法二:
实例1
将文件file_abc_{1..20}.txt批量改为file_123_{1..20}.txt
①用shell命令:
[[email protected] test1]# for i in ${file[@]}; do mv $i ${i/abc/123} ; done;
shell命令用分号分开:
[[email protected] test1]# for i in ${file[@]}; do mv $i ${i/abc/123} ;echo $; done;
array1=(`lsblk -l |awk '/sd[a-z][0-9]/{print $1}'`) #以空格分割
[[email protected] ~]# echo ${array1[0]} #取0索引对应的元素
sda1
[[email protected] ~]# echo ${#array1[@]} #获取数组长度
实例:
[[email protected] ~]# cat tools.sh
#!/bin/bash
while true
do
echo "1.查看磁盘分区
2.CPU负载
3.剩余内存
4.退出"
read -p "请输入你要执行的操作:>>>" num
PART(){
#echo $HOSTNAME
#fdisk l
echo "hostname:$HOSTNAME"
echo "system: `cat /etc/redhat-release`"
#定义数组
array1=(`lsblk -l |awk '/sd[a-z][0-9]/{print $1}'`)
array2=(`lsblk -l |awk '/sd[a-z][0-9]/{print $4}'`)
array3=(`lsblk -l |awk '/sd[a-z][0-9]/{print $6}'`)
array4=(`lsblk -l |awk '/sd[a-z][0-9]/{print $7}'`)
#遍历数组
num=`echo $((${#array1[@]}-1))`
for i in `seq 0 $num` #i=0
do
cat <<EOF
---------${array1[$i]}-----------
path: ${array1[$i]}
size: ${array2[$i]}
file_os: ${array3[$i]}
mount_on:${array4[$i]}
EOF
done
}
case $num in
1)
PART
#echo "parting...."
;;
2)
echo "loading..."
;;
3)
echo "mem...."
;;
4)
exit 0
;;
*)
print "please input true list..."
esac
done
案例:ssh秘钥批量分发
(1)生成秘钥
[[email protected] ~]# ssh-******
生成**之后 影藏目录。.ssh目录下会产生两个文件
[[email protected] ~]# cd .ssh/
[[email protected] .ssh]# ls
id_rsa id_rsa.pub
(2)秘钥发送
[[email protected] ~]# ssh-copy-id 192.168.42.61
[[email protected] ~]# sshpass -p 123456 ssh-copy-id 192.168.42.61