Setting up files to the arrays one by one in BASH script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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?
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?
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.
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.
Last edited by David the H.; 06-19-2013 at 01:46 PM.
Reason: small code change + addendum
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.