Please help using awk command

Hi All,

I have the script below that checks rpm code version that is freshly installed than 1 week ago.

server_list.txt as my arguement ($1)

Script is run like this:
$ ./script_name server_list.txt

here is my script:
#!/bin/bash
QUERY="rpm -qa --queryformat '%{installtime} %{name}-%{version}-%{release}-%{arch} \n'| awk '{ if ( \$1 > '`date -d "1 week ago" +%s`') print \$2 }'"

LIST=`cat $1`
for i in $LIST
do
echo -e "\n$i"
ssh -q root@$i $QUERY
done

Output is like this:
-------------
adminj.pm.test1.intra
vc_portal_admin-jp.test1-2.22_MATADOR.8-0-noarch
xinetd-2.3.14-9.fc6-x86_64

adminj.pm.test2.intra
vc_portal_admin-jp.test2-2.21.22-0-noarch

adminj.pm.test3.intra

logproc.pm.qfe.intra
vc_kron_generator-jp.qfe-1.1.2-0-noarch

-----------
My problem is:

How will I remove the server in the output list that has no rpm freshly
install 1 week ago ? adminj.pm.test3.intra has no new rpm installed 1
week ago.

Can anyone help me to achieve this ?

Try something like this,

#!/bin/bash
QUERY="rpm -qa --queryformat '%{installtime} %{name}-%{version}-%{release}-%{arch} \n'| awk '{ if ( \$1 > '`date -d "1 week ago" +%s`') print \$2 }'"

LIST=`cat $1`
for i in $LIST
do
var=`ssh -q root@$i $QUERY`
if [ $? = 0 ]
then
 echo -e "\n$i"
 echo -e $var
fi
done

Hi Skmdu,

Thanks .. I've got an idea from your post but I still got the same output.
I modified the part of script like this:

for i in $LIST
do
var=`ssh -q root@$i $QUERY`
if [ $var != "" ]
then
echo -e "\n$i"
echo -e $var
fi
done

but i got this error:

Processing server_list.txt...
./query.sh: line 12: [: !=: unary operator expected

adminj.pm.test1.intra
vc_portal_admin-jp.test1-2.22_MATADOR.9-0-noarch
./query.sh: line 12: [: !=: unary operator expected

adminj.pm.test2.intra
vc_portal_admin-jp.test2-2.21.22-0-noarch

Can you help me to check the problem?

Your script has to be modified like below. then you will get errors.

for i in $LIST
do
var=`ssh -q root@$i $QUERY`
if [ "$var" != "" ]
  then
     echo -e "\n$i"
     echo -e $var 
fi
done

But in my previous post, I checked the exit status of the last command i.e rpm (QUERY) if its success, then print the machine name and output.

If there is no rpm then $? will be other than 0. I guess, its always better to check exitstatus ($?) instead of output.

I understand what you mean that is to check the exit status,
but the problem is, with or without rpm output, exit status is 0 since
the both command was successful.

linuxgeek, Did you try with this ? Was it resolved?

Hi Skmdu,

Thanks for your help. My problem has resolved. Using this :slight_smile:

for i in $LIST
do
var=`ssh -q root@$i $QUERY`
if [ "$var" != "" ]
then
echo -e "\n$i"
echo -e $var
fi
done

More power on this forum. It helps a lot.
Thanks!