Search code examples
xmllinuxerror-handlingxmllint

xmllint capturing error code


I´m using the xmllint Linux command (xmllint description) and I want to capture the error code to use it inside a script. How can I do it?

Thanks


Solution

  • You can doing this :

    if xmllint --xpath '/my/path' file.xml; then
        echo "success"
    else
        echo >&2 "error"
        exit 1
    fi
    

    If you don't have --xpath switch :

    file=/path/to/file
    xpath='/foo/bar'
    result="$(echo "cat $xpath" | xmllint --shell "$file")"
    
    if [ $(echo "$result" | wc -l) -gt 2 ]; then
        echo "success"
    else
        echo >&2 "error"
        exit 1
    fi