Bash 脚本不起作用,这使得nginx的每个日志文件都有gzip
请不要告诉我unix和其他工具中的日志循环。我需要这个脚本。 当我尝试这样做时:Bash 脚本不起作用,这使得nginx的每个日志文件都有gzip,bash,shell,loops,find,gzip,Bash,Shell,Loops,Find,Gzip,请不要告诉我unix和其他工具中的日志循环。我需要这个脚本。 当我尝试这样做时: find /root/nginx.log* -type f -print0 -and ! -name "*.gz" -and ! -name "nginx.log.2017.05.08" -exec gzip "{}" \; 一切正常,我有这个结果,我需要: -rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.07.gz -rw-r--r-
find /root/nginx.log* -type f -print0 -and ! -name "*.gz" -and ! -name "nginx.log.2017.05.08" -exec gzip "{}" \;
一切正常,我有这个结果,我需要:
-rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.07.gz
-rw-r--r-- 1 root root 17942221 May 8 10:33 nginx.log.2017.05.08
-rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.09.gz
-rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.10.gz
-rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.11.gz
-rw-r--r-- 1 root root 1381791 May 8 10:33 nginx.log.2017.05.12.gz
当我尝试编写脚本时:
#!/bin/bash
name="nginx"
data=$(date +%Y.%m.%d)
current_nginx=$name.$data
echo $current_nginx
find /root/nginx.log* -type f -print0 -and ! -name "*.gz" -and ! -name "$current_nginx" -exec gzip "{}" \;
执行此脚本后,我有:
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.07.gz
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.08.gz
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.09.gz
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.10.gz
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.11.gz
-rw-r--r-- 1 root root 1379440 May 8 10:35 nginx.log.2017.05.12.gz
调试脚本:
bash -x test.sh
+ name=nginx
++ date +%Y.%m.%d
+ data=2017.05.08
+ current_nginx=nginx.2017.05.08
+ echo nginx.2017.05.08
nginx.2017.05.08
+ find /root/nginx.log.2017.05.07.gz /root/nginx.log.2017.05.08.gz /root/nginx.log.2017.05.09.gz /root/nginx.log.2017.05.10.gz /root/nginx.log.2017.05.11.gz /root/nginx.log.2017.05.12.gz /root/nginx.log.2017.05.13.gz /root/nginx.log.2017.05.14.gz -type f -print0 -and '!' -name '*.gz' -and '!' -name nginx.2017.05.08 -exec gzip '{}' ';'
/root/nginx.log.2017.05.07.gz/root/nginx.log.2017.05.08.gz/root/nginx.log.2017.05.09.gz/root/nginx.log.2017.05.10.gz/root/nginx.log.2017.05.11.gz/root/nginx.log.2017.05.12.gz/root/nginx.log.2017.05.13.gz/root/nginx.log.2017.05.14.gz
我的状况
-和-名称“$current\u nginx”
不起作用。我哪里有错?请帮助。您缺少当前的.log\u nginx您缺少当前的.log\u nginx