shell总结二

shell script:

脚本语言,目标是抽象成脚本,用脚本代替大量重复的操作,减轻工作量

frist.sh:轮转日志,压缩

命令都写全路径,否则crontab可能会有问题

[root@test ~]# cd /tmp/

[root@test tmp]# ls

91.txt 94.txt 97.txt aa ks-script-HMqUb2 sedp.txt xinghao.txt zz

92.txt 95.txt 98.txt file log.txt seds.txt yum1.log

93.txt 96.txt 9.txt home.tar script test.txt yum.log

[root@test tmp]# vi frist.sh

[root@test tmp]# cat frist.sh

#!/bin/bash

#rotate nginx logs grouped by date

#written by kongsh

#2019-07-21

#modify by

#加载环境变量

source /etc/profile

Home=/tmp/

Logdir=/var/log/nginx

Mailto=kongsh@hengbao.com

Date=date +%Y%m%d

mkdir -p $Home/log

cp $Logdir/nginx.access.log L o g d i r / n g i n x . a c c e s s . l o g . Logdir/nginx.access.log. Logdir/nginx.access.log.Date

if [ $? -eq 0 ]

then

date >> $Home/log/log.txt

echo "success copy file." >> $Home/log/log.$Date



cat /dev/null > $Logdir/nginx.access.log

[ $? eq 0 ] && echo "success change log >> $Home/log/log.$Date"||echo "failed c

hange log >> H o m e / l o g / l o g / Home/log/log/ Home/log/log/Date"

else

echo "failed copy file.">>$Home/log/log.$Date

fi

cat H o m e / l o g / l o g . Home/log/log. Home/log/log.Date|mail -s “totate nginx log result” $Mailto

exit

[root@test tmp]#

说明:

l mail -s:发邮件

l 变量命名:没有固定的规则,通常是字母+数字,字母第一个字母大写,不要全部大写,有可能和系统的冲突,见名知意

l 变量赋值:不需要加$,前后都不能有空格,有空格的话会把前后的字符当做命令

[root@test tmp]# a=11

[root@test tmp]# a =11

-bash: a: command not found

[root@test tmp]# a= 11

-bash: 11: command not found

l 变量使用:用$调用

[root@test tmp]# echo $a

11

[root@test tmp]# $a

-bash: 11: command not found

l {}:

[root@test tmp]# echo ${a}b

11b

[root@test tmp]# echo $ab

[root@test tmp]#

l 双引号:

[root@test tmp]# cd script/

[root@test script]# ls

rename test.sh

[root@test script]# ll

total 4

drwxr-xr-x. 2 root root 51 Feb 12 04:39 rename

-rw-r–r--. 1 root root 10 Feb 12 04:22 test.sh

[root@test script]# a=ll

[root@test script]# echo $a

total 4 drwxr-xr-x. 2 root root 51 Feb 12 04:39 rename -rw-r–r--. 1 root root 10 Feb 12 04:22 test.sh

[root@test script]# echo “$a”

total 4

drwxr-xr-x. 2 root root 51 Feb 12 04:39 rename

-rw-r–r--. 1 root root 10 Feb 12 04:22 test.sh

[root@test script]# echo ‘$a’(单引号是原样输出)

$a

[root@test script]#

l $?:系统变量,存的是上1条命令执行结果,判断上一条命令是否执行成功,只要不为0就是执行失败

[root@test script]# ls

rename test.sh

[root@test script]# echo $?

0

[root@test script]# ls ahdh

ls: cannot access ahdh: No such file or directory

[root@test script]# echo $?

2

[root@test script]# aa

-bash: aa: command not found

[root@test script]# echo $?

127

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值