LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting up files to the arrays one by one in BASH script (https://www.linuxquestions.org/questions/linux-newbie-8/setting-up-files-to-the-arrays-one-by-one-in-bash-script-4175466364/)

cli 06-17-2013 11:04 AM

Setting up files to the arrays one by one in BASH script
 
Hi All,
I have some log files as below
Code:

# ls -rt | grep -v "Log_Viewer" | xargs -n1
mypc_monthly.log.1
mypc_weekly.log.1
mypc_daily.log.1
mypc_daily.log.2
mypc_daily.log.3
mypc_daily.log.4
mypc_daily.log.5
mypc_daily.log.6
mypc_weekly.log.2
mypc_daily.log.7
mypc_daily.log.8
mypc_daily.log.9
mypc_daily.log.10

Now I need to make a script which first shows the monthly log file
1) mypc_monthly.log.1

then for second it should show recent weekly log file
2) mypc_weekly.log.2

then for all daily logs it should show as seniority wise which are created after recent weekly log file(mypc_weekly.log.2) file i.e

3) mypc_daily.log.7
next
mypc_daily.log.8
next
mypc_daily.log.9
next
mypc_daily.log.10 and so on.

For that I have made a script as below
Code:

# cat Log_Viewer
#!/bin/bash
Log_File[0]="$(ls -rt *monthly*)"
Log_File[1]="$(ls -rt *weekly* | tail -n1)"
# here I am not able to set the array as seniority wise as mentioned above for daily files.
for i in ${!Log_File[@]}; do
        echo "Now you are going see \"${Log_File[i]}\" log file . . ."
        sleep 2; echo; done

# bash Log_Viewer
Now you are going see "mypc_monthly.log.1" log file . . .

Now you are going see "mypc_weekly.log.2" log file . . .

The same way I wanted to be for all daily files(Please see: not all daily but only those) which logs are created after latest weekly log file i.e as mentioned above in number 3). So begging your king help.

grail 06-17-2013 11:59 AM

If you want data in an array in a certain order, then you need to place it in the array in the certain order.

cli 06-17-2013 12:23 PM

Quote:

Originally Posted by grail (Post 4973583)
If you want data in an array in a certain order, then you need to place it in the array in the certain order.

Thanks Guru. You are right, I can set it as below
Code:

Log_File=( mypc_monthly.log.1 mypc_weekly.log.2 mypc_daily.log.7 mypc_daily.log.8 mypc_daily.log.9 mypc_daily.log.10 )
Since every month log file numbers won't be same, every month I don't want to add them to the array manually. Thats why I am thinking to set up the array at one time which can apply for every month. So is there a way to set all daily files as seniority wise which are created after last weekly?
Thanks in advance for your kind help.

cli 06-18-2013 12:44 AM

Any help for the above script, please?
or
how can I print to screen for all daily files which are created after latest weekly file?
I don't want the below command which we know about those daily files, so we can print only those.
Code:

ls -rt *.log.* | tail -n4
I need a command which can automaticaly finds latest weekly log file and can print all daily log files(even that daily is only one or even those daily files are more than one) which are created after that latest weekly file.
Waiting for your kind reply.

grail 06-18-2013 01:18 AM

hmmm ... i seem to be a little slow on the uptake for this one :( Looking at your original post, all the files would appear to already be in order ... no?

cli 06-18-2013 03:22 AM

Hi Grail Guru,
May I expect any help for the above which all I have already explained?

grail 06-18-2013 07:24 AM

I am trying to help and although it seems clear to you, I am not getting the issue??

I understand that you want to place them in the array and you want a specific order. My issue is that it seems the files are already in the correct order, so what is it that I am missing?

David the H. 06-19-2013 01:26 PM

Using ls to get filenames is not a very good idea. While it might be ok for a quick one-shot operation, in a script you should use safer means.

Since the filenames already have names with regular patterns, you should be able to use globbing patterns instead.

The only difficulty involved is that the numbers aren't zero-padded, so you'll have to use separate patterns for the single digit and double digit entries.

Code:

Log_File=(
          mypc_monthly.log.[0-9]
          mypc_monthly.log.1[0-9]
          mypc_weekly.log.[0-9]
          mypc_weekly.log.1[0-9]
          mypc_daily.log.[0-9]
          mypc_daily.log.1[0-9]
        )

The above assumes a maximum of 19 for each log suffix, but you can easily adjust it for more or less.

It could also be compacted down into a single line with a bit of brace expansion.

Code:

Log_File=( mypc_{monthly,weekly,daily}.log.{,1}[0-9] )

David the H. 06-19-2013 01:36 PM

Hmm, after a bit more reading, I see that the requirement for the daily files are a bit more complex than I thought. But it's far from insurmountable.

Code:

Monthly_Logs=( mypc_monthly.log.{,1}[0-9] )
Weekly_Logs=( mypc_weekly.log.{,1}[0-9] )
Daily_Logs_Temp=( mypc_daily.log.{,1}[0-9] )

Last_Weekly=${Weekly_Logs[@]: -1}

for Log in "${Daily_Logs_Temp[@]}"; do
    if [[ $Log -nt $Last_Weekly ]]; then
        Daily_Logs+=( "$Log" )
    fi
done

Log_Files=( "${Monthly_Logs[@]}" "${Weekly_Logs[@]}" "${Daily_Logs[@]}" )

unset Monthly_Logs Weekly_Logs Daily_Logs Daily_Logs_Temp Last_Weekly Log

There are other variations you could use too, such as creating a single array as per my first post and looping through it multiple times. The first pass would get the last weekly file, then a second one to remove any daily file from the array that is older than that.


All times are GMT -5. The time now is 11:46 PM.