1

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

These are not totally reliable. A node called <endpoints (note the extra s) would mess things up.
its OP's sample, not mine. If there is possibility of "s" in endpoint, then matching it exactly will do.
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 me e.g. (./doSomething.sh) do I need to use exec()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.