script to tell whether there exists directory or not

Hi,

I am writing a script as follows:

#!/bin/sh
set -x
for i in `cat a`; do
#sleep 3
print $i >> server.txt
sleep 5
if test -d `vtask  $i "' ls -lrt /opt/log/ '" ` ;
then
print $i >> 12.5.txt
else
print $i >> 12.0.txt
fi
done

where the vtask is a command line execution script
in my 'a' file I have some servers

I want to check if the log directory exists in the servers if it exists then it should update in the 12.5.txt else if the log directory does not exists if should update in the 12.0.txt directory.

Any corrections in my code please help

Thanks
Dinesh

Any error msgs? Any output at all? What does not work?

ok I will make that simple,

I want to just grep the '12.5.0.2307' from a command output

############################################################################
CA IT Client Manager
--------------------
Agent - Basic Hardware Inventory 12.5.0.2307
Agent - Asset Management 12.5.0.2307
Agent - Data Transport 12.5.0.2307
Agent - Software Delivery 12.5.0.2307
Agent - Deployment 12.5.0.2307
############################################################################

runtask <servername> 'version.sh | grep '12.5.0.2307' '

The above command gives me full output of the text file since it matches all the 12.5.0.2307 I want to grep just '12.5.0.2307' alone...I don't want the whole line just the 12.5.0.2307, how to achieve this?

If your grep / fgrep supports the -o switch, you could use that.

Hi, My box is aix and the grep does not support -o option. Any other suggestions? ofcourse I can think about awk but not sure how to get the word alone from the output

awk '{while(match($0,/12\.5\.0\.2307/))
{
 print substr($0,RSTART,RLENGTH)
 sub(/12\.5\.0\.2307/,"")
}}' file

or with Perl:

perl -ne 'while(/(12\.5\.0\.2307)/){
print $1 . "\n";
s/12\.5\.0\.2307//}' file
1 Like

Hi,

Thanks for your reply and the solution. But, I am struck a bit hope to get a lift from you.

#!/bin/sh
set -x
for i in `cat serverlist`; do
sleep 2
vtask $i ". /etc/profile.CA all; dsmver" > version.txt
ver=`awk '{while(match($0,/12\.5\.0\.2307/)) {  print substr($0,RSTART,RLENGTH) sub(/12\.5\.0\.2307/,"")}}' version.txt | sort -u`
echo $ver
if [ $? -eq 0 ];
then
print $i >> 12.5.txt
else
print $i >> 12.0.txt
fi
done

The output is shown below:

# ./agentversion.sh 
+ cat serverlist
+ sleep 2
+ vtask xxxxxx . /etc/profile.CA all; dsmver
+ 1> version.txt
+ + sort -u
+ awk {while(match($0,/12\.5\.0\.2307/)) {  print substr($0,RSTART,RLENGTH) sub(/12\.5\.0\.2307/,"")}} version.txt
ver=
+ echo

+ [ 0 -eq 0 ]
+ print yyyyyy
+ 1>> 12.5.txt
+ sleep 2
+ vruntask mmmmmm . /etc/profile.CA all; dsmver
+ 1> version.txt
+ + sort -u
+ awk {while(match($0,/12\.5\.0\.2307/)) {  print substr($0,RSTART,RLENGTH) sub(/12\.5\.0\.2307/,"")}} version.txt
ver=12.5.0.23071
+ echo 12.5.0.23071
12.5.0.23071
+ [ 0 -eq 0 ]
+ print ssssssss
+ 1>> 12.5.txt

what I want to know is even if the command does not find '12.5.0.2307' version in the server it still goes to the 12.5.txt file, I want if the command does not find the version as '12.5.0.2307' or it got disconnected it should go to 12.0.txt

The version.txt file has the following entries

12.5.0.2307
12.5.0.2307
12.5.0.2307

not sure why another '1' is added after 12.5.0.23071

The value of ? ( $? ) which you are testing is for the echo $ver statement (which will almost always be true (0)) and subsequently, only 12.5.txt will be appended to.

You don't seem to be needing the awk command.

Try:

#!/bin/sh
set -x
for i in `cat serverlist`; do
sleep 2
vtask $i ". /etc/profile.CA all; dsmver" > version.txt
#ver=`awk '{while(match($0,/12\.5\.0\.2307/)) {  print substr($0,RSTART,RLENGTH) sub(/12\.5\.0\.2307/,"")}}' version.txt | sort -u`
#echo $ver
#if [ $? -eq 0 ];
if fgrep -q "10.12.2.2307" version.txt 2>/dev/null
then
print $i >> 12.5.txt
else
print $i >> 12.0.txt
fi
done
1 Like