Problem with While Loop in AIX Server

Hello,

I am trying to run the following Script on AIX Server. The script checks if first field is EFFECTIVE_TIME , if it is then it converts second field in readable format else it redirects entire line to $MAIL. Both the mentioned files exists prior to running the script

#!/bin/ksh
MAIL=~/backup_scripts/Changed_Policy_Details.txt
TMP=~/backup_scripts/List_of_Changed_Policies_tmp1.txt
while read line
do
read -a arr <<< $line
if [[ ${arr[0]} == EFFECTIVE_TIME ]];then
a=`perl -le "print scalar(localtime(${arr[1]}))"`
echo ${arr[0]} $a >> $MAIL
else
echo $line >> $MAIL
fi
done < $TMP

The $TMP and $MAIL File exists and the contents of $TMP are as follows :

Contents of /usr/openv/hpbackup/db/class/serverfgt/info :


ACTIVE 1
EFFECTIVE_TIME 1279345218
GRANULAR_RESTORE_INFO 0


Contents of /usr/openv/hpbackup/db/class/servertyg/schedule/Full/days :


0 13400 21000
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0

Contents of /usr/openv/hpbackup/db/class/serverglt/info :


ACTIVE 0
EFFECTIVE_TIME 1279643561
GRANULAR_RESTORE_INFO 1

I am getting following error from AIX 5.3.

0403-057 Syntax error at line 4 : `<' is not expected

Could someone please help. I am not getting errors on Red Hat Server but problem only on AIX.

Thanks
Rahul

On AIX the default ksh shell is ksh88. Try running the script using ksh93

A strange thing is that read -a arr is bash syntax, not ksh93 syntax. Try using read -A instead.

1 Like

i think awk experts in this forum can do it better, but it should work on every system with KSH93.

1 Like

Hello agent.kgb,

Your script is providing effective timestamp in the below format where we are getting current timestamp :

EFFECTIVE_TIME Thu Nov  5 09:18:26 EST 2015

Although as per the file we should get following timestamp after converting from epoch to human readable format : Sat Jul 17 01:40:18 2010

$ perl -le ' print scalar localtime(shift)' 1279345218
Sat Jul 17 01:40:18 2010

Thanks
Rahul

sorry, 2 slashes forgotten :slight_smile:

awk '$1 == "EFFECTIVE_TIME" {print "printf \"EFFECTIVE_TIME %T\n\" \\#"$2}  $1 != "EFFECTIVE_TIME" {print "print "$0}' $TMP | ksh93 >$MAIL 
1 Like

Hello agent.kgb,

Thanks a lot for your help. It worked on Linux. I will test this on AIX as well. Does AIX 5.3 have ksh93 ?

Also can you please explain the command.

Thanks
Rahul

AIX 5.3 has ksh93, but I don't have it anymore and can't say right now how "fresh" ksh93 there.

awk '
$1 == "EFFECTIVE_TIME" {
  print "printf \"EFFECTIVE_TIME %T\n\" \\#"$2
  }  

$1 != "EFFECTIVE_TIME" {
  print "print "$0
  }' $TMP

According to your script you're interested only in the strings which begin with EFFECTIVE_TIME as the 1st word. In awk we have 2 statements - the first statement is for the strings, which have EFFECTIVE_TIME as the 1st word, and the second statement is for all other strings.

In the second case (easiest case) we just print the string itself. It is usually done with print $0 .

In the first case we have to transform UNIX seconds to "normal" representation of the date. AIX standard awk doesn't have function to work with time/date as GNU awk, but ksh93 has.

So based on the input data we form a new script, which will be executed by ksh and transforms timestamp. If you run the one-line without | ksh93 , you will see the script written with awk. Something like:

$ awk '$1 == "EFFECTIVE_TIME" {print "printf \"EFFECTIVE_TIME %T\n\" \\#"$2} $1 != "EFFECTIVE_TIME" {print "print "$0}' t1
print ACTIVE 1
printf "EFFECTIVE_TIME %T
" \#1279345218
print GRANULAR_RESTORE_INFO 0
print

With this last part | ksh93 we just say, that the script should be executed by ksh93.

There is an easier way without awk:

cat t1 | while read A rest ; do [ "$A" == "EFFECTIVE_TIME" ] && printf "EFFECTIVE_TIME %T\n" \#$rest  || print -- "$A $rest" ; done

but in this case you will loose spaces between first word and the rest of the data, if you have more than 1 space character.

1 Like

Thanks a lot for your help and providing excellent explaination :slight_smile:

---------- Post updated 11-06-15 at 02:10 PM ---------- Previous update was 11-05-15 at 08:43 PM ----------

Hello,

The code is working fine on RedHat but on AIX it is showing error as shown below:

ksh93[45]: printf: T: unknown format specifier

Could you please help.

Thanks
Rahul

can you show the output of:

$ oslevel -s
$ ksh93
$ echo ${.sh.version}

Hello agent.kgb,

I am getting desired Output on AIX 7.1. It seems that the ksh93 version is old on AIX5.3

Also Could you please help in converting epoch timestamp from the below file. The 7th field is epoch timestamp :

78903 0 1 WEEKEND_FULL_ORACLE_BKP Full GYUI-12543 1446541202 0000006420 0000000000 186880
78401 0 1 WEEKLY_FULL_UNIX_BKP Full WEEKLY_TEST 1446508902 0000038818 0000000000 448755456
78306 0 1 WEEKLY_FULL_UNIX_BKP1 Full FTYU-32145 1446455973 0000091647 0000000000 390188288

I am able to convert the 7th field alone but when I am trying to place all the fields in the output report ( 1st to NF fields) then I am getting error. Below command worked

awk '{print "printf \"%T\n\" \\#"$7}' tmp1.txt | ksh93

Could you please help.

Thanks
Rahul M

Would this work (tested on bash only):

while read -a A; do printf "%s %s %s %s %s %s %(%F %T)T %s %s %s\n" ${A[0]} ${A[1]} ${A[2]} ${A[3]} ${A[4]} ${A[5]} ${A[6]} ${A[7]} ${A[8]} ${A[9]}; done < file
78903 0 1 WEEKEND_FULL_ORACLE_BKP Full GYUI-12543 2015-11-03 10:00:02 0000006420 0000000000 186880
78401 0 1 WEEKLY_FULL_UNIX_BKP Full WEEKLY_TEST 2015-11-03 01:01:42 0000038818 0000000000 448755456
78306 0 1 WEEKLY_FULL_UNIX_BKP1 Full FTYU-32145 2015-11-02 10:19:33 0000091647 0000000000 390188288