LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-17-2013, 11:04 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
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.
 
Old 06-17-2013, 11:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
If you want data in an array in a certain order, then you need to place it in the array in the certain order.
 
Old 06-17-2013, 12:23 PM   #3
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.

Last edited by cli; 06-17-2013 at 12:25 PM.
 
Old 06-18-2013, 12:44 AM   #4
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-18-2013, 01:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?
 
Old 06-18-2013, 03:22 AM   #6
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Hi Grail Guru,
May I expect any help for the above which all I have already explained?
 
Old 06-18-2013, 07:24 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?
 
Old 06-19-2013, 01:26 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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] )
 
Old 06-19-2013, 01:36 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to deal with bash[$style] arrays in a POSIX compliant script? GrapefruiTgirl Programming 8 01-02-2015 11:17 PM
[SOLVED] Adding to DAEMONS/MODULES arrays from bash script crabchucker Linux - Newbie 4 01-24-2012 03:41 PM
[SOLVED] Pass 2 arrays to a BASH script santhosh_cv Programming 3 11-10-2011 05:23 AM
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration