0

I'm trying to get some files with zero size and insert it with a text. I create a simple script and put it in the crontab every 15 minutes.

I use find after read this question i found it before How to find all Zero bytes files in directory including subdirectories.

Here the script.

#!/usr/bin/bash
statPath=/opt/player/for_test/testing
date=`date +%Y%m%d` #Untuk mengambil tanggal (YYYY-MM-DD)
cm=$(date +%M)
host=`hostname`

for n in `find $statPath -size 0`
do
if [ $cm -ge 0 ] && [ $cm -le 15 ];then
    echo $date"00""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 16 ] && [ $cm -le 30 ];then 
    echo $date"15""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 31 ] && [ $cm -le 45 ];then 
    echo $date"30""|"$host"|"0x00"|"0 >> $n
elif [ $cm -ge 46 ] && [ $cm -le 59 ];then
    echo $date"45""|"$host"|"0x00"|"0 >> $n
fi
done

File in testing directory before running the script

-rw-r--r--   1 root     root           0 Jun 13 13:46 Server01_2016061313_45.log
-rw-r--r--   1 root     root           0 Jun 13 14:01 Server01_2016061314_00.log
-rw-r--r--   1 root     root           0 Jun 13 14:16 Server01_2016061314_15.log
-rw-r--r--   1 root     root           0 Jun 13 14:31 Server01_2016061314_30.log
-rw-r--r--   1 root     root           0 Jun 13 14:46 Server01_2016061314_45.log
-rw-r--r--   1 root     root           0 Jun 13 15:01 Server01_2016061315_00.log

File in testing directory after running the script.

-rw-r--r--   1 root     root           28 Jun 13 13:46 Server01_2016061313_45.log
-rw-r--r--   1 root     root           28 Jun 13 14:01 Server01_2016061314_00.log
-rw-r--r--   1 root     root           28 Jun 13 14:16 Server01_2016061314_15.log
-rw-r--r--   1 root     root           28 Jun 13 14:31 Server01_2016061314_30.log
-rw-r--r--   1 root     root           28 Jun 13 14:46 Server01_2016061314_45.log
-rw-r--r--   1 root     root           28 Jun 13 15:01 Server01_2016061315_00.log

The result that I got is all the file is changed with the same format. I run the script at 3:34 PM.

root@Server01:/opt/player/for_test/testing# more Server01_2016061313_45.log
2016061330|Server01|0x00|0
root@Server01:/opt/player/for_test/testing#
root@Server01:/opt/player/for_test/testing# more Server01_2016061314_15.log
2016061330|Server01|0x00|0

Expected result

For log with "00" minute, the result is 2016061300|Server01|0x00|0
For log with "15" minute, the result is 2016061315|Server01|0x00|0
For log with "30" minute, the result is 2016061330|Server01|0x00|0  
For log with "45" minute, the result is 2016061345|Server01|0x00|0

Is there any easy way to do this? I kinda stuck in here.

Community
  • 1
  • 1
  • all files change with the same format because you are using `$cm` which is defined *before the loop*. – fedorqui 'SO stop harming' Jun 13 '16 at 09:02
  • so I should move the variable `cm` inside the loop? – groovybear Jun 13 '16 at 09:09
  • Oh, wait, I misread your question. So you want to use `cm` to indicate the current block of 15 minutes? Then this should be fine. I just tested it and it works fine -- as a side note, you may want to pre-process `$cm` before so that you don't have to use this huge if/else condition. – fedorqui 'SO stop harming' Jun 13 '16 at 09:15
  • You don't need the variable, since your expected result makes no use of it. – Michael Vehrs Jun 13 '16 at 09:18
  • 1
    I think you intend to use the name of the file found to decide on the file contents. – Michael Vehrs Jun 13 '16 at 09:19
  • Try `echo "$date45|$host|0x00|0" >> $n` instead of using a lot of quotes in each then condition. Moreover, you can remove the first if condition for each elif statement `[ $cm -ge 16 ] &&` because it is implicit since you are using `elif` – Flows Jun 13 '16 at 09:32
  • As @MichaelVehrs says, it looks like you want to take the name of the file into account. If so, you need to parse it and extract the last piece before `.log`. That value will be `cm`. – fedorqui 'SO stop harming' Jun 13 '16 at 09:38
  • how I pre-process the `$cm`? or rather what is the correct command to insert some word to the zero size file? – groovybear Jun 13 '16 at 09:38
  • How is it that after modifying every file at 3:34 pm, none of the files have mtime 15:34? – glenn jackman Jun 13 '16 at 12:43
  • it is change to 15.34, sorry i just copy-paste from the list before running the script. – groovybear Jun 14 '16 at 06:28
  • Does the answer I wrote below is working for you ? Please comment the answer if you still have issues – Flows Jun 16 '16 at 11:20

1 Answers1

0

You need to get cm from the file name. Assuming you always need the 2 last characters before .log you could get them with cm=${n: -6:2}. This is call Parameter Expansion. cm has to be compute for each loop in your for.

Explanation

  • ${} means you use Parameter Expansion

  • From string n extract 2 characters starting 6 before the end of the string n

I used this post as a reference.

Community
  • 1
  • 1
Flows
  • 3,040
  • 1
  • 19
  • 49