6

I'd like to write a shell script in ksh or bash which exits with 1 if a specific file is older than 30 minutes. (Last modification time is older than half hour). It would be easy on a Linux or a modern unix, but I have to do it on AIX 5.2 version.

So the constraints:

  • there is no -mmin -mmax options in 'find'
  • there is no -d option in touch (touch -d '30 minutes ago' temp then find ! -newer temp doesn't work)
  • there is no 'stat' command
  • there is no %s parameter in the 'date' command (date +%s doesn't work)
  • I'm not allowed to install any software

Could you please help how can I do it with these constraints?

csadam
  • 188
  • 2
  • 8
  • I used to work on AIX a ton in the past. I wrote several scripts to detect software on the machine. What shell are you working with, tsh? I think you should have grep right? Do you have awk? – Jason Huntley Mar 26 '12 at 17:47
  • 2
    Do you have a compiler? This sounds like it would actually be easier in C... – Kyle Smith Mar 26 '12 at 18:02
  • @KyleSmith has a point - this would be easy in C (you may not have the `stat` command, but I know you have the `stat()` syscall!) Also easy in Perl/Ruby/etc. – voretaq7 Mar 26 '12 at 18:05
  • Do you have any any of standard interpreted languages, like perl, python, ruby, or so on? – Zoredache Mar 26 '12 at 18:07
  • Incidentally, [AIX v6r1 find has -mmin](http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds2%2Ffind.htm) -- I know that doesn't help you (unless you can upgrade :-), but might be useful in the future. – voretaq7 Mar 26 '12 at 18:16
  • I maybe have C compiler, but never used it. There is also Perl but I don't want to use it if it's not absolutely necessary. There is also grep, awk and sed. I'm not sure if there is tsh, I've never tried it. The default shell is ksh. – csadam Mar 26 '12 at 19:21
  • Any python? I have python 2.3.1 on my AIX 5.3 box but I'm not sure if this was standard. – MikeyB Mar 27 '12 at 01:55

4 Answers4

5

I can do it using just touch, test, and date. (tested on AIX 5.3.0.0)

First you need to create a file 30 minutes in the past (Unfortunately, this requires prior knowledge of the current timezone on the machine. But you may be able to work that into things if need be.)

In this example, the current timezone is EST5EDT (GMT-4). If you're lucky, the machine timezone will be set at GMT and you can just use TZ=00:30:

-bash-3.00# date
Mon 26 Mar 14:22:31 2012
-bash-3.00# touch -t `TZ=04:30 date '+%Y%m%d%H%M.%S'` foo
-bash-3.00# ls -al foo
-rw-r--r--   1 root     system            0 26 Mar 13:52 foo

Now use test to compare the dates:

-bash-3.00# test '/smit.log' -ot foo && echo 'smit.log is older than 30min'
smit.log is older than 30min
MikeyB
  • 38,725
  • 10
  • 102
  • 186
  • Very nice approach ! And easier to understand than using sed/awk/cut dark-magic when parsing the ls output. – Tonny Mar 26 '12 at 18:48
  • Creative solution I will use it, thanks. If the TZ is negative then it needs extra "-s around the time. – csadam Mar 27 '12 at 12:55
  • Oh? Don't see that here: `$ TZ= date` → `Tue 27 Mar 13:41:15 2012`, `$ TZ=00:30 date` → `Tue 27 Mar 13:11:17 2012`, `$ TZ=-00:30 date` → `Tue 27 Mar 14:11:20 2012` – MikeyB Mar 27 '12 at 13:42
  • Ok. You're right. I've mixed up those funky upper quotes :) it works with the negatives also for me. – csadam Mar 27 '12 at 15:13
3

Use ls.

ls -l lists the last modification time of the file.
You can dissect the output with awk or cut and then parse the date.

Note that on AIX (as on most *nix systems) the format of the date provided by ls depends on how long ago the file was last modified. See the documentation for specifics.

voretaq7
  • 79,345
  • 17
  • 128
  • 213
  • That's the way to do it. Aix is a nice OS but some of it's Unix tooling is a bit basic at times. Please note that some of the command-line options to the ls command can be used to manipulate how the time is displayed. This may help to make the output more easier to parse. – Tonny Mar 26 '12 at 18:12
  • @Tonny I vaguely recall there's a way to make AIX print unix timestamps for the modification time, but I couldn't find a reference to that anywhere so I assume I'm misremembering... – voretaq7 Mar 26 '12 at 18:18
  • I seem to recall that as well, but it has been a while since I have done some heavy Aix scripting. It has sunk away very deep. I have a 5.3 system at the office at my disposal. Will see what I can dig up tomorrow (if I have the time... Busy days....) – Tonny Mar 26 '12 at 18:44
1

If you have the basics, bash/tsh and sed. You can write a simple script which will test the filetime associated with ls output:

#/bin/bash

LSOUTPUT=`ls -l`
IFS='
'

for f in $LSOUTPUT; do
  echo $f

  _sedout=`echo $f|sed 's/.*....-..-.. ..:\(..\).*/\1/'`

  if [ "$_sedout" -gt 30 ]; then
    echo "Passed!"
  fi

  echo
done

You will have to vary your sed regex to pull the right location, but this should work as long as you have sed. If ls doesn't provide enough detail, I believe there is a command called istat you can use.

If ksh is your shell, the syntax should be very similar to that of bash. I just used that example because I'm on Ubuntu.

Jason Huntley
  • 1,253
  • 3
  • 10
  • 22
-1

You can still install GNU find in findutils - http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html

jirib
  • 1,240
  • 8
  • 15