1
file=/root/stacktrace.log
minsize=100
filesize=$(wc -c <"$file")
echo $filesize
if [ $filesize -ge $minsize ]; then
    mailx -s 'File size is more than 10MB' example@gmail.com < /dev/null
fi

Above script is working fine in centos. But its not working in solaris os.

Please help me on this.

  • What is not working? And what is the error seen? – Inian Jan 30 '17 at 08:33
  • data_file_alert.sh: syntax error at line 3: `filesize=$' unexpected – pradheep purushothaman Jan 30 '17 at 08:35
  • 1
    How are you running the script? did you specify the interpreter to be invoked explicitly? `#!/bin/bash` or `#!/bin/sh`? – Inian Jan 30 '17 at 08:36
  • 2
    Using `wc` to get a file size is not very sensible. The filesystem already knows the sizes of your files, so rather than potentially do many 100GB of I/O to your poor disks, just ask the filesystem - `stat`, for example, will tell you. Also, your variable should be called `maxsize` if you think about it a little. – Mark Setchell Jan 30 '17 at 08:43
  • Hi Inian, I did't specify any interpreter. same script running properly in centos but solaris its not working. whenever iam running this scripts iam getting syntax error at line 3: `filesize=$' unexpected – pradheep purushothaman Jan 30 '17 at 09:32
  • @pradheeppurushothaman How are you starting this script? If you're using `/bin/sh`, note that Centos (and many other Linux distributions) completely confuse the distinction between POSIX `sh` and `bash`. Linux `sh` scripts that use non-POSIX "bashisms" will fail on a system such as Solaris where `sh` is standards-compliant. I suspect `filesize=$(wc -c <"$file")` is just such a "bashism". – Andrew Henle Jan 30 '17 at 10:53
  • Hi Andrew Thanks for your reply. Iam new of solaris OS. Can you please tell me. what we can use instead filesize=$(wc -c <"$file") – pradheep purushothaman Jan 30 '17 at 11:56
  • @pradheeppurushothaman Since you're apparently running Solaris 10, you may not have the GNU `stat` utility. You should be able use backticks around `ls -l "$file" | awk '{ print $5 }'` where `$5` means the fifth column of the output of the `ls` command, which should be the size of the file. See http://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts – Andrew Henle Jan 30 '17 at 12:15
  • Can you use `/usr/xpg4/bin/sh` instead of `/bin/sh` ? – Mark Plotnick Jan 30 '17 at 20:08
  • thank you so much andrew. its working now – pradheep purushothaman Jan 31 '17 at 12:51
  • As @MarkSetchell said: Do not use `wc`. I once had a performance problem when counting files of some Gb. When you do not have `stat` on Solaris, use `find path -name yourfile -size +x`. – Walter A Jan 31 '17 at 21:28

1 Answers1

0

Try this:

file=/root/stacktrace.log
maxsize=100
if [ -f "$file" ] && [ -n "$(find "$file" -size +"$maxsize"c)" ]; then
    mailx -s "File size is more than $maxsize" example@gmail.com < /dev/null
fi

This uses find to determine that the size is $maxsize or larger, in this case 100 bytes. I also required [ -f "$file" ] to ensure we're looking at a file rather than a directory so find's recursive search won't find a file inside that directory's structure that is sufficiently large.

BSD find and GNU find (but not Solaris find) support better units than just c for "characters" (bytes). Try k, M, or G like -size +"$maxsize"M or else -size +$((maxsize*1048576))c

(Never mind that the syntax highlighting looks odd. You are allowed to nest one level of double quotes inside a "$(…)" command substitution.)

Adam Katz
  • 14,455
  • 5
  • 68
  • 83