BASH script help

BASH script help

Post by Richard Uphi » Fri, 28 Jun 2002 00:20:44



I am trying to right a script to add into the .profile to identify
large file sizes and then send a message to the system mail so that
users can be warned when they login if there is a problem.
This is what  am trying at the moment:

#!/bin/bash

MAXFILE=1000

du -a | sort -rn | -gt $MAXFILE > $LOGFILE

Please can some one help with this, I've also tried to sending the du
to a variable and then using an if statement: if [ $VAR1 -gt $var2 ]
but I can not seem to get this to work possibly due to the output of
the du.

The du returns results like this:

29815   ./old.data
16018   ./old.data/pms
10074   ./holding
9522    ./old.data/empty_files
7347    ./copy
3849    ./copy/cust
3012    ./copy/cust/custfl.isf
2965    ./copy/chifl.isf

Any help would be greatly appreciated.

 
 
 

BASH script help

Post by mjt » Fri, 28 Jun 2002 01:17:23


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> I am trying to right a script to add into the .profile to identify
> large file sizes and then send a message to the system mail so that
> users can be warned when they login if there is a problem.
> This is what  am trying at the moment:

> #!/bin/bash

> MAXFILE=1000

> du -a | sort -rn | -gt $MAXFILE > $LOGFILE

> Please can some one help with this, I've also tried to sending the du
> to a variable and then using an if statement: if [ $VAR1 -gt $var2 ]
> but I can not seem to get this to work possibly due to the output of
> the du.

> The du returns results like this:

> 29815   ./old.data
> 16018   ./old.data/pms
> 10074   ./holding
> 9522    ./old.data/empty_files
> 7347    ./copy
> 3849    ./copy/cust
> 3012    ./copy/cust/custfl.isf
> 2965    ./copy/chifl.isf

> Any help would be greatly appreciated.

i think 'find' would be more conducive to your needs and can
combine the operations you're looking for: "man find" (be sure
to read about the "-size n" option

- --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Michael J. Tobler: motorcyclist, surfer,  #    Black holes result
 skydiver, and author: "Inside Linux",     #   when God divides the  
 "C++ HowTo", "C++ Unleashed"              #     universe by zero

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9GejwtTveLPAHcDIRAlYkAJ9jFWRLli59zy+506NcvqpK07IVtwCeL8UB
q8w2KP+y97DTYSs3LCrDwbI=
=0c3a
-----END PGP SIGNATURE-----

 
 
 

BASH script help

Post by Bit Twist » Fri, 28 Jun 2002 07:05:05



Quote:> I am trying to right a script to add into the .profile to identify
> large file sizes and then send a message to the system mail so that
> users can be warned when they login if there is a problem.
> This is what  am trying at the moment:

> #!/bin/bash

> MAXFILE=1000

> du -a | sort -rn | -gt $MAXFILE > $LOGFILE

> Please can some one help with this, I've also tried to sending the du
> to a variable and then using an if statement: if [ $VAR1 -gt $var2 ]
> but I can not seem to get this to work possibly due to the output of
> the du.

Yep variable would be everything from du

You might want to read
http://www.linuxdoc.org/LDP/abs/html/index.html

 
 
 

BASH script help

Post by Jason Baughe » Fri, 28 Jun 2002 11:57:09




Quote:> I am trying to right a script to add into the .profile to identify
> large file sizes and then send a message to the system mail so that
> users can be warned when they login if there is a problem.
> This is what  am trying at the moment:

> #!/bin/bash

> MAXFILE=1000

> du -a | sort -rn | -gt $MAXFILE > $LOGFILE

> Please can some one help with this, I've also tried to sending the du
> to a variable and then using an if statement: if [ $VAR1 -gt $var2 ]
> but I can not seem to get this to work possibly due to the output of
> the du.

> The du returns results like this:

> 29815   ./old.data
> 16018   ./old.data/pms
> 10074   ./holding
> 9522    ./old.data/empty_files
> 7347    ./copy
> 3849    ./copy/cust
> 3012    ./copy/cust/custfl.isf
> 2965    ./copy/chifl.isf

> Any help would be greatly appreciated.

I did it like this:

#!/bin/sh
du -a | sort -rn | while read size direc
do
  if [ $size -gt 1000 ]
    then echo "$size    $direc"
  fi
done

You'll have to add a > $LOGFILE to the end of the echo command if you
want to put the info in the logfile.  Mine just kicks it to the screen.

--
Jason Baugher
Virtual Adept Professional Consulting Services
1406 Adams St.
Quincy, IL 62301
(217) 221-5406
http://baugher.pike.il.us/virtualadept

 
 
 

BASH script help

Post by Richard Uphi » Sat, 29 Jun 2002 01:45:06


Thanks Jason that has really helped. To expand on this what I aim to
do is echo a warning on the screen with out the details ie "Warning
Large file sizes"  and then cat the details to either a file or even
better try and get them into an email in pine - any ideas.
 
 
 

BASH script help

Post by tob.. » Sat, 29 Jun 2002 05:37:03



> Thanks Jason that has really helped. To expand on this what I aim to
> do is echo a warning on the screen with out the details ie "Warning
> Large file sizes"  and then cat the details to either a file or even
> better try and get them into an email in pine - any ideas.

I suggest inserting a cronjob for this.  The old, dirty way to do it is
to write this as root:

        crontab -e

(It's quite common to have a file /etc/crontab by now, in some versions of
RedHat and others you can also just insert any executable script in
/etc/cron.daily and it will run by itself)

The script might look something like this:

===
#!/bin/bash
## Directory to be scanned:
DIR=/
for user in `cat /etc/passwd | cut -f1 -d:`
do
 find $DIR -user $user -size +5000 -printf '%k\t%p\n' | sort -n | mail -s "big files" $user
done
====

It can of course be tuned a lot to your local demands.  "mail" might have to be
replaced by "Mail", and "-s" won't work at all kind of unixes, but at least
most Linux distributions.

I must warn you that this will be extremely annoying for all your users,
receiving daily notifications about big files.

Personally, I usually do something like this to find out what files or
directories takes the most space:

  du -ax | sort -n | tail

I would also suggest to create a script that checks if the disk space is low.
I have those files:

/etc/cron.daily/logrotate:
#!/bin/sh

. /home/tobix/lib/is_space
. /etc/margin

if is_space /var/log $DISK_MARGIN
then
    /usr/sbin/logrotate /etc/logrotate1.conf
else
    echo "Using aggressive logrotation due to low disk space"
    /usr/sbin/logrotate /etc/logrotate2.conf
fi

/etc/logrotate2.conf:
## Aggressive log rotation
rotate 8
size 70k
include /etc/logrotate.conf

/etc/logrotate1.conf:
## Non-aggressive logrotation ... rotate when log files are more than 6M
## big (I would say at least 60MB ... but unfortunately the harddisk is
## rather tiny at the moment)
size 6M
rotate 9
include /etc/logrotate.conf

/etc/logrotate.conf:
# send errors to root
errors root

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
compress

missingok

/var/log/wtmp {
    create 0664 root utmp

Quote:}

/var/log/btmp {
    create 0664 root utmp

Quote:}

## Insert more log files ...

/etc/margin:
DISK_MARGIN=16666

/etc/cron.daily/tmpreaper:
#!/bin/sh
PATH=/usr/sbin:/usr/bin:/sbin:/bin

. /etc/margin
. /home/tobix/lib/is_space

## I prefer fuzzy logic ... presented by some
## continuarly mathematical function instead of
## a plain and simple "if ... else", or boundaries
## in general ... but, I made "is_space" first and
## thought twice only later ... and I don't have
## the time thinking harder by now.
if is_space /tmp $[DISK_MARGIN*16]
then
## 1 year if 16x minimum safe free disk space
GRACE="360d"
elif is_space /tmp $[DISK_MARGIN*2]
then
## 2 monthes if 2x minimum safe free disk space
GRACE="60d"
elif is_space /tmp $DISK_MARGIN
then
## 1 week if minimum safe disk space
GRACE="7d"
elif is_space /tmp $[DISK_MARGIN/2]
then
## 4 days if half safe disk space
GRACE="4d"
else
## just the stuff of today if we're very low on disk space
GRACE="1d"
fi

## Maybe tmpreaper isn't installed?  RedHat has "tmpwatch" instead, I think ...
## tmpreaper is from Debian.
if ! [ -x /usr/sbin/tmpreaper ]; then
    exit 0
fi

# Remove `/tmp/...' files not accessed in 7 days, protecting the .X, .ICE,
# .iroha and .ki2 files; but removing symlinks. For directories not the access
# time, but the modification time is used (--mtime-dir), as reading a directory
# to check the contents will update the access time!
#
# Note that the sockets are safe even without the `--protect', unless `--all'
# is given, and the `.X*-lock' files would be safe also, as long as they have
# no write permissions, so this particular protect is mainly illustrative, and
# redundant.  For best results, don't try to get fancy with the moustache
# expansions.  KISS.  Always --test your protect patterns.
#
# In case you're wondering: .iroha is for cannaserver and .ki2 is for kinput2
# (japanese software, lock files).

# ! Important !  Please read the manual regarding the --protect option.
#                The pattern *MUST* be surrounded by single quotes.

tmpreaper --mtime-dir --symlinks $GRACE  \
  --protect '/tmp/.X*-{lock,unix,unix/*}' \
  --protect '/tmp/.ICE-{unix,unix/*}' \
  --protect '/tmp/.iroha_{unix,unix/*}' \
  --protect '/tmp/.ki2-{unix,unix/*}' \
  --protect '/tmp/lost+found' \
    /tmp

~/lib/is_space:
param4() {
  echo $4;

Quote:}

is_space() {
  disk=$1;
  size=$2;
  left=`param4 \`df -k $1 | tail -1\``
  if [ $left -lt $2 ];
  then
    return 1;
  else
    return 0;
  fi

Quote:}

--
Unemployed programmer / sys.admin.  - +47 917 000 50
 
 
 

1. bash script help

I have no bash script skills at all

I want to convert collection of mp3 files
to wave or cdr
Using a script ripped off from the cdwrite howto ( below )
the result is a wave file with two extensions "file.mp3.wav".
Obviously I want only ".wav".

The "correct" script would be appreciated.

#!/bin/bash

for I in *.mp3

do

        mpg321 --w - "$I"  > "$I".wav

done

--
faeychild

2. Anjuta IDE :: Integreated Development Environment for C/C++) :: Gnome

3. RH 5.1 on a Toshiba Portege 610ct laptop?

4. simple bash script help

5. Fudged it up big time

6. Linux BASH Script HELP!!!!!

7. Registering an URL...

8. Passing option to bash script help needed

9. Simple bash script help?

10. re bash script help

11. Bash script help..