views:

61

answers:

2

What would be the best solution to check (from the command line with a script), if a certain xml file contains this line:

<endpoint uri="endpoint.php" class="flex.messaging.endpoints.AMFEndpoint"/>

or this line

<!-- <endpoint uri="endpoint.php" class="flex.messaging.endpoints.AMFEndpoint"/> -->

and stop execution if the second one (commented out) is found?

Thx, martin

+4  A: 

Single line or across multiple lines? If the former, you can use grep.

Update: There seem to be some XML aware variants like xgrep, xmltwig and xmlstarlet.

Noufal Ibrahim
+1 for xmlstarlet
Maxim Veksler
A: 

assuming pattern occurs at single line

#!/bin/bash
awk '
/<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/ && /<!--/{
    exit
}
/<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/{
    # to execute external script inside awk, uncomment below
    #cmd = "myscript.sh"
    #system(cmd)
} 
' file

OR you can return a code back to shell

#!/bin/bash
var=$(awk '
/<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/ && /<!--/{
    print 1
}
/<endpoint uri=.*endpoint.php.*class.*flex.messaging.endpoints.AMFEndpoint/{
    print 0
} 
' file)
[ "$var" -eq 1 ] && exit
[ "$var" -eq 0 ] && ./myscript.sh
ghostdog74
These are not totally reliable. A node called <endpoints (note the extra s) would mess things up.
Noufal Ibrahim
its OP's sample, not mine. If there is possibility of "s" in endpoint, then matching it exactly will do.
ghostdog74
this looks good! It is sufficient how it is checked. I am trying to execute a script in the second case...just putting a command in there does not work for mee.g. (./doSomething.sh)do I need to use exec()?
martin