LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2011, 01:07 PM   #16
prowla
Member
 
Registered: Feb 2011
Location: UK
Distribution: RHEL 5 & 6, Ubuntu 10
Posts: 93

Rep: Reputation: 3

You don't need awk and all that to get the info out of the "df"; this shows how:

Code:
df -P | while read fs kblocks used avail perc mount
do 
  echo ${perc%%\%} $mount
done
 
Old 02-24-2011, 02:19 PM   #17
Diggy
Member
 
Registered: Jan 2009
Posts: 47

Original Poster
Rep: Reputation: 17
colucix,

It worked! It does raise another question for me, though.

I tried the same script on two machines. Machine 1 had files Afile.1 through .4, and Bfile.1 through .4, and Cfile.1 through .4. Machine 2 had only Afile.1 through .4. All of the files on Machine 1 were concatenated, just as I wanted. And, on Machine 2, Afile.1 through .4 were concatenated. But, files named Bfile.? and Cfile.? were created in the directory. Of course, I don't want that to happen.

So, in order to use the same script on different machines where any combination of Afiles, Bfiles, and Cfiles might exist, what should I do so that "missing" files are ignored. Hopefully, with modifications, your construct can be made to do that. BTW, the use of Afile, Bfile, and Cfile are purely illustrative. They could be named Curly.1, Moe.1, and Larry.1, etc.

As ever, thanks.

Diggy
 
Old 02-24-2011, 02:34 PM   #18
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
Quote:
Originally Posted by Diggy View Post
colucix,

It worked! It does raise another question for me, though.

I tried the same script on two machines. Machine 1 had files Afile.1 through .4, and Bfile.1 through .4, and Cfile.1 through .4. Machine 2 had only Afile.1 through .4. All of the files on Machine 1 were concatenated, just as I wanted. And, on Machine 2, Afile.1 through .4 were concatenated. But, files named Bfile.? and Cfile.? were created in the directory. Of course, I don't want that to happen.

So, in order to use the same script on different machines where any combination of Afiles, Bfiles, and Cfiles might exist, what should I do so that "missing" files are ignored. Hopefully, with modifications, your construct can be made to do that. BTW, the use of Afile, Bfile, and Cfile are purely illustrative. They could be named Curly.1, Moe.1, and Larry.1, etc.

As ever, thanks.

Diggy
To ignore "missing" files, you can do existence check like this:
In colucix's script, instead of
Code:
for file in /var/log/fileA.? /var/log/fileB.? /var/log/fileC.?
  do
    > $file
  done
Try:
Code:
  for file in /var/log/fileA.? /var/log/fileB.? /var/log/fileC.?
  do
    if [ -f $file ]; then
       > $file
    fi
  done
 
Old 02-24-2011, 03:01 PM   #19
Diggy
Member
 
Registered: Jan 2009
Posts: 47

Original Poster
Rep: Reputation: 17
Ah, a conditional statement! Worked like a charm.

Thanks to you all for your help. This made my day!

Best,

Diggy
 
Old 02-24-2011, 03:34 PM   #20
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another way is by means of the nullglob option:
Code:
shopt -s nullglob
for file in /var/log/fileA.? /var/log/fileB.? /var/log/fileC.?
do
  > $file
done
From the bash reference manual:
Quote:
nullglob
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
 
Old 02-24-2011, 04:00 PM   #21
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Diggy View Post
I really want to delete files
If services log to file then logrotate is the answer (and not script) if you want to retain logging. If you do not want to retain logging then do not log (as much as you do now).
 
1 members found this post helpful.
Old 02-24-2011, 04:28 PM   #22
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by unSpawn View Post
If services log to file then logrotate is the answer (and not script) if you want to retain logging. If you do not want to retain logging then do not log (as much as you do now).
Good point, unSpawn! I was too focused on the shell issues to think about the logical thing to do.
 
Old 02-24-2011, 04:43 PM   #23
Diggy
Member
 
Registered: Jan 2009
Posts: 47

Original Poster
Rep: Reputation: 17
UnSpawn of rkhunter fame? Excellent. I use that great program!

You're right about logrotate, but I need to use the script, regardless. Let me elucidate (finally got to use that word :-) ).

The box I'm running the script on is old - old OS, old hardware, small disk. Because so much log activity takes place, the /var directory fills up about every four weeks because of the growth of certain log files. Logrotate rotates them fine, but can't keep up with the growth. I suppose I could change the rotation to a shorter period, or do it based on file size, but I'll be doing a hardware refresh to a bigger, better box in about eight weeks, so the script will be a stopgap measure. And, I'm not concerned about deleting precious log files; they're parsed on a continuous basis for events of interest. Finally, rather than giving this man a fish, you've helped teach him how to fish, thereby feeding him for live.

My gratitude.

Diggy
 
Old 02-24-2011, 05:40 PM   #24
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by colucix View Post
too focused on the shell issues to think about the logical thing to do.
Heh, well, as it appears from the OP's explanation the logical thing to do isn't always the right thing to do...
 
Old 02-24-2011, 05:45 PM   #25
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Diggy View Post
I use that great program!
Good to know it's of use.


As we're scripting away here's another take on things zapping everything (or so I'd hope) bigger than half a GB in /var/log:
Code:
/usr/sbin/lsof -Pwln +D/var/log|awk '/log\// {print $NF}'|sort -u|while read LOG; do
 [ `stat -c %s "${LOG}"` -gt $[10240*50] ] && $>${LOG}
done
this may or may not be as you like it and it's not tested so YMMV(VM).

* Whatever you do you should 'kill -HUP $PID' afterwards to allow processes to re-open file descriptors.
 
Old 02-25-2011, 08:11 AM   #26
Diggy
Member
 
Registered: Jan 2009
Posts: 47

Original Poster
Rep: Reputation: 17
unSpawn,

As an aside, the box that this script will be running on has provided great service to us for nearly seven years. As mentioned, it'll be peacefully retired in about two months. I've tried to take all of the necessary precautions in case it fails but, God willing, it'll make it to the finish line.

The last construct you showed me is interesting, and well beyond my scripting level. I'll study it, and test it on a throw-away VM to see how it performs.

Many thanks.

Diggy
 
Old 02-25-2011, 11:32 AM   #27
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Diggy View Post
the box that this script will be running on has provided great service to us for nearly seven years. As mentioned, it'll be peacefully retired in about two months. I've tried to take all of the necessary precautions in case it fails but, God willing, it'll make it to the finish line.
A proper burial would be a bit over the top but I hope you give its remnants a good final destination?..


Quote:
Originally Posted by Diggy View Post
The last construct you showed me is interesting, and well beyond my scripting level. I'll study it, and test it on a throw-away VM to see how it performs.
It's simple: run lsof and only show items in /var/log | awk for any line that has "log/" in it and print the last field (file name) | sort output showing unique items only | while read the output line by line into the variable "LOG"; and for each item do
[ `stat the item only returning its size in bytes` and check if the size is greater than $[10240*50] ] if it is then empty the item
done.
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 01:44 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 AM.

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