CENTOS - istat

Hi, I am using CENTOS and for now, I am not able to install or to configure the istat command on Linux. So, is there a way to do the same that the shell is doing below, but not using istat command? I would appreciate any help on it.

#!/bin/sh

HOUR=0
MONTHN=0

if [ -f $1 ]
   then
   :
else
   echo 99999999 > $2
   exit
fi

LINE=`istat $1  | grep modified`

LINE2=`istat $1 | grep Owner` 

DAY=`echo  $LINE | awk '{ print $5 }'`
YEAR=`echo  $LINE | awk '{ print $8 }'`
MONTH=`echo  $LINE | awk '{ print $4 }'`
HOUR=`echo $LINE | awk '{ print $6 }'`
P1=`echo $LINE | awk '{print $8}'`

HOUR=`echo $HOUR | sed "s/://g"`

for i in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    do
    MONTHN=`expr $MONTHN \+ 1`
    if [ "$MONTH" = "$i" ]
       then
       break
    fi
done

MONTHN=`expr $MONTHN \+ 100 | cut -c2-4`
echo  $YEAR$MONTHN$DAY$HOUR > $2

USER=`echo $LINE2 | awk '{ print $2 }'`
echo $USER > $3

Many scripting languages can give you the information that man istat (All) provides, but I am not sure why you need to save the information to two new files ... but it is your script. For a man perl (All) replacement:

use strict;
use warnings;

use File::Basename;
use POSIX qw{ strftime };

$, = '';
$\ = "\n";

my $NAME = basename $0;

unless (0 < @ARGV) {
    print STDERR 'Usage: ', $NAME, ' <file> <mtimefile> <ownerfile>';
    exit 1;
}

my $file = shift @ARGV;
my @F = stat $file;

unless (0 < @F) {
    print STDERR $file, ': no such file or directory';
    exit 1;
}

$file = shift @ARGV;

open FH, '>', $file or die $file;
print FH strftime '%Y%m%d%H%M', localtime $F[9];
close FH;

$file = shift @ARGV;
open FH, '>', $file or die $file;
print FH $F[4], '(', scalar getpwuid($F[4]), ')';
close FH;

exit 0;

Which give you:

$ perl istatinfo istatinfo mtime owner
$ cat mtime
201411161938
$ cat owner
1002(derek)

for the file:

$ ls -l istatinfo
-rw-r--r--. 1 derek ludwig 588 Nov 16 19:38 istatinfo

Please format the mtime and owner to your liking.

Good job derekludwig. :slight_smile:

Not trying to one up you, but, since I already went to the trouble...

Here's the OPs script modified to work with bash (Centos standard I think).

#!/bin/bash

HOUR=0
MONTHN=0
[ -f $2 ] || touch $2
[ -f $3 ] || touch $3

if [ -f $1 ]
then
    :
else
    echo 99999999 > $2
    exit
fi

# LINE=`istat $1  | grep modified`

# LINE2=`istat $1 | grep Owner` 
 LINE=`ls -l --time-style='+%Y %m %d %H:%M' | grep $1`

# MONTH=`echo  $LINE | awk '{ print $4 }'`
YEAR=`echo  $LINE | awk '{ print $6 }'`
MONTHN=`echo  $LINE | awk '{ print $7 }'`
DAY=`echo  $LINE | awk '{ print $8 }'`
HOUR=`echo $LINE | awk '{ print $9 }'`
P1=`echo $LINE | awk '{print $1}'`

HOUR=`echo $HOUR | sed "s/://g"`

# for i in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# do
#     # MONTHN=`expr $MONTHN \+ 1`
#     if [ "$MONTH" = "$i" ]
#     then
#         break
#     fi
# done

# MONTHN=`expr $MONTHN \+ 100 | cut -c2-4`
echo $YEAR$MONTHN$DAY$HOUR > $2

USER=`echo $LINE | awk '{ print $3 }'`
echo $USER > $3

# eof #

output

$ ls -l --time-style='+%Y %m %d %H:%M' | grep istat.sh
-rwxr-xr-x. 1 owner group  833 2014 11 16 17:58 istat.sh*
$ istat.sh targetfile mtimefile ownerfile
201411151034 > mtimefile
owner > ownerfile

I'm wondering about the usefulness of this meager output though???

No worries, ongoto, I didn't think of the time-style option of man ls (linux). But if that is an available option, this could be simplified as:

ls -l --time-style=%Y%m%d%H%M $1 | awk '{ print $6; print $3; }'

Do you have access to the "stat" command? IIRC that provides pretty much the same data.

Yeah.
That beats the sox off the script that was provided.

Cheers

Hi ongoto, thank you for writing the script. I did what you wrote, however, the time is not returning correctly. It is returning: $H$M instead of the hour. See below the script without the comments.

tks.

#!/bin/bash

HOUR=0
MONTHN=0

[ -f $2 ] || touch $2
[ -f $3 ] || touch $3

if [ -f $1 ]
   then
   :
else
   echo 99999999 > $2
   exit
fi

LINE=`ls -l --time-style='+%Y %m %d $H:$M' | grep $1`

DAY=`echo  $LINE | awk '{ print $8 }'`
YEAR=`echo  $LINE | awk '{ print $6 }'`
MONTHN=`echo  $LINE | awk '{ print $7 }'`
HOUR=`echo $LINE | awk '{ print $9 }'`
P1=`echo $LINE | awk '{print $1}'`

HOUR=`echo $HOUR | sed "s/://g"`

echo  $YEAR$MONTHN$DAY$HOUR > $2

USER=`echo $LINE | awk '{ print $3 }'`
echo $USER > $3

I see a typo :cool:

Hope this helps.

Those !@#$%^&*( chars have sharp teeth... :slight_smile:

Hi,

Yes, it worked, I also didnt notice it.

tks.

Br. John,

Line 17of your script has:

LINE=`ls -l --time-style='+%Y %m %d $H:$M' | grep $1`

Line 19 of ongoto's script has:

LINE=`ls -l --time-style='+%Y %m %d %H:%M' | grep $1`

Again, the entire script could be simplified to something like:

ls -l --time-style=%Y%m%d%H%M $1 | awk '{ print $6; print $3; }'