CURTIME=$(date '+%s')
FILETIME1=$(stat $LOG -c %Y)

I created this script in Ubuntu and it works fine there but on SunOS, (Generic_150400-64 sun4v sparc sun4v), it is not working.

This script is to check if logs are updating and do ( If else ) depending on the output of seconds in TIMEDIFF1 .

If else will perform stuff like, if logs are updated 15 mins ago do this, if 30 mins ago do that... and so on.

So time in seconds as an output is important for if else statement to run efficiently.

so I am trying to determine how old a file is (how long ago it was modified / updated) in SECONDS. and I need time in seconds.

  • What have you already tried? Can you paste your code attempts? – Q23 Jul 11 at 17:03
  • first four lines where i need modification is already pasted above my question, but ill post here again. CURTIME=$(nawk "BEGIN{print srand}") LOG=/home/apache_logs.txt FILETIME1=$(stat $LOG -c %Y) TIMEDIFF1=$(expr $CURTIME - $FILETIME1) – Bharat Jul 11 at 17:14
  • 1
    So basically you’re trying to determine how old a file is (how long ago it was modified / updated)?  Do you need to know exactly how old it is, or is it OK to get a “yes” or “no” answer to a query like “Was it longer ago than N seconds?” (for some value of N )? … … … … … … … Please do not respond in comments; edit your question to give clearer and more complete description of the objective that you want to achieve. – G-Man Says 'Reinstate Monica' Jul 11 at 18:43
  • made the questions more clearer – Bharat Jul 11 at 20:09
  • 1
    it is not working is the least useful of all problem descriptions. When anything in your life is not working and you want someone to help you to get it working, you need to tell them in what way it's not working. In this case - wrong output, no output, core dump, error messages, something else? What happens if you include a shebang at the top - #!/bin/env bash? What makes you think an awk script would be appropriate for this task? – Ed Morton Jul 14 at 12:19

As replacement for stat you can use the following perl oneliner:

perl -e 'print((stat("/tmp/a"))[10])'

To workaround the current date you can use the touch command to create a temporary file and read the modification timestamp from the temp file like:

touch /tmp/currtime
CURRTIME=$(perl -e 'print((stat("/tmp/currtime"))[10])')
FILETIME=$(perl -e 'print((stat("/path/to/log"))[10])')
echo "Time difference is $DIFFTIME seconds"
rm /tmp/currtime

The reference about the perl stat() command can be found at: https://perldoc.perl.org/functions/stat.html

  • thanks a lot Lambert – Bharat Jul 12 at 13:26

If the version of date on your SunOS system doesn’t support the +%s argument, then using awk to fix that, while not impossible, is impractical.

Does stat filename -c %Y work?  If it doesn’t, you’re pretty much out of luck.  If it does, you can use it to get the current time:

if [ -e "$tmpfile" ]
    printf "%s exists; that’s odd.\n" "$tmpfile"
if ! { touch "$tmpfile"  &&  CURTIME=$(stat "$tmpfile" -c %Y)  &&  rm "$tmpfile";}
    printf "Something went wrong.\n"

If your version of bash doesn’t support [ -e filename ], then use -f instead.

P.S. You should quote all your variables:

FILETIME1=$(stat "$LOG" -c %Y)
  • stat filename -c %Y work? no it didnt work – Bharat Jul 11 at 18:26
  • if you know any other commands, please let me know, i just need a workaorund for these two commands – Bharat Jul 11 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.