Problem with While Loop in AIX Server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with While Loop in AIX Server
# 1  
Old 11-05-2015
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

Code:
#!/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 :

Code:
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.

Code:
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
# 2  
Old 11-05-2015
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.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-05-2015
i think awk experts in this forum can do it better, but it should work on every system with KSH93.

Quote:
awk '$1 == "EFFECTIVE_TIME" {print "printf \"EFFECTIVE_TIME %T\n\" #"$2} $1 != "EFFECTIVE_TIME" {print "print "$0}' $TMP | ksh93 >$MAIL
This User Gave Thanks to agent.kgb For This Post:
# 4  
Old 11-05-2015
Hello agent.kgb,

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

Code:
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

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


Thanks
Rahul


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indentation and fixed width data.

Last edited by rbatte1; 11-05-2015 at 10:52 AM.. Reason: Added CODE & ICODE tags
# 5  
Old 11-05-2015
sorry, 2 slashes forgotten :-)

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

This User Gave Thanks to agent.kgb For This Post:
# 6  
Old 11-05-2015
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
# 7  
Old 11-05-2015
AIX 5.3 has ksh93, but I don't have it anymore and can't say right now how "fresh" ksh93 there.

Code:
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:

Code:
$ 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:
Code:
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.
This User Gave Thanks to agent.kgb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How to ssh from an AIX OS server to a Fabric OS server without password?

Hi I'd like to ssh from an AIX OS server ( v5.3) to a Fabric OS server ( v6.1.2 ) without password. I tried using dsa or rsa keys but it didn't work, the aix server still asked for the password. Somebody help, please :(:(:( (8 Replies)
Discussion started by: bobochacha29
8 Replies

2. AIX

Will it affect my AIX LPAR security, when i set up email alerts on AIX server.

Hello, I've set up email alerts on AIX Servers. so that i can get email notifications (via mail relay server) when ever there is abnormal behavior. for example 1) my script monitors CPU/disk/memory etc... when it reaches high water ark, it will send an email alert. 2) disk usage alerts 3)... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. AIX

Virtual I/O server Problem (before Installing AIX 7.1)

Hello people, I'm facing some problems Installing AIX in my Power server 720 Well, to tell the truth my problem is in Virtual I/O (IVM) installation. OBSERVE THAT I DON'T HAVE A HMC! By the way: I have the latest server Firmware! I've done all the process in ASMI, then inserted and... (1 Reply)
Discussion started by: Xscaio
1 Replies

4. Programming

Problem with Perl script after moving from a Windows/Apache Server to a UNIX server.

I have a Perl script that worked fine before moving it to justhost.com. It was on a Windows/Apache server. Just host is using UNIX. Other Perl scripts on other sites that were also moved work fine so I know Perl is functioning. The script is called cwrmail.pl and is located in my cgi-bin. When I... (9 Replies)
Discussion started by: BigBobbyB
9 Replies

5. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

6. Shell Programming and Scripting

Send email from sendmail on AIX using exchange server as SMTP server

i am new in AIX i am trying to write a script to take a backup for specific files on server to and check error log if backup success send email to administrator , script done except for sending mail , i try to configure sendmail on aix to use our exchange server to send emails but still get error... (0 Replies)
Discussion started by: ahmed_salah
0 Replies

7. AIX

Transferring files from one AIX server to another AIX server in binary mode

Hi, I am a newbie to AIX. We have 2 AIX5.3 servers in our environment, I need to transfer some files in Binary mode from one server to another and some files in ASCII mode from one server to another server. Could you please help me as to how I need to do that? Thanks, Rakesh (4 Replies)
Discussion started by: rakeshc.apps
4 Replies

8. AIX

how to loop through non-empty files with shell script on AIX

I have av script that loops through some statistic files to create a report. We would like to only loop through non-empty files as these files create an empty report-line. I have figured out how to find the non-empty files, but not how to loop through only those files. Here is the code that finds... (4 Replies)
Discussion started by: Tessa
4 Replies

9. AIX

Problem in SSH Install in AIX 4.3 Server.

Hi Friends I am trying to install ssh in one of my AIX4.3 server. I downloaded openssh and openssl from IBM site. While installing them openssh failed. The lpcheck command display looks like this: # lppchk -v lppchk: The following filesets need to be installed or corrected to bring ... (5 Replies)
Discussion started by: efunds
5 Replies

10. UNIX for Advanced & Expert Users

AIX server performance problem!

Hello, I have a performance problem on an AIX box. I'm not sure what is causing this and hoping someone may have suggestions. Currently I'm noticing that cpu's are waiting while the box is in a idle state. I checked the disks and none of them are at 100%. If they were then I would understand... (3 Replies)
Discussion started by: ctcuser
3 Replies
Login or Register to Ask a Question