Browse Source

Add comments

main
lhess2021 4 years ago
parent
commit
da1b9279ba
  1. 36
      shell-scripts.d/xpath.sh

36
shell-scripts.d/xpath.sh

@ -1,35 +1,38 @@
#!/bin/bash #!/bin/bash
# An interactive xpath query tool # An interactive xpath query commandline tool.
# Stores your previous query.
# File for previous query
touch .prev-xpath.tmp touch .prev-xpath.tmp
# Colored output
GREEN='\033[0;32m' GREEN='\033[0;32m'
NC='\033[0m' # no color NC='\033[0m' # no color
# Ask for canto file
clear clear
read -p "Choose a canto: [i,r,p][1-34]" CANTO read -p "Choose a canto: [i,r,p][1-34] " CANTO
CANTICLE="$(echo $CANTO | tr -d -c a-z)" CANTICLE="$(echo $CANTO | tr -d -c a-z)"
CANTO_NUM="$(echo $CANTO | tr -d -c 0-9)" CANTO_NUM="$(echo $CANTO | tr -d -c 0-9)"
file="-${CANTO_NUM}.xml" # Create complete filename
if [[ "$CANTICLE" == 'i' ]]; then if [[ "$CANTICLE" == 'i' ]]; then
CANTICLE='Inferno' CANTICLE='Inferno'
file="inferno${file}" file="inferno-${CANTO_NUM}.xml"
elif [[ "$CANTICLE" == 'r' ]]; then elif [[ "$CANTICLE" == 'r' ]]; then
CANTICLE='Purgatorio' CANTICLE='Purgatorio'
file="purgatorio${file}" file="purgatorio-${CANTO_NUM}.xml"
elif [[ "$CANTICLE" == 'p' ]]; then elif [[ "$CANTICLE" == 'p' ]]; then
CANTICLE='Paradiso' CANTICLE='Paradiso'
file="paradiso${file}" file="paradiso-${CANTO_NUM}.xml"
else else
echo "Not a valid canto file: $file" echo "Not a valid canto file: $file"
fi fi
# Start interactive query
if [[ -e "$file" ]]; then if [[ -e "$file" ]]; then
clear && echo -e "You are searching ${CANTICLE^} ${CANTO_NUM}.\nAdd a trailing slash ${GREEN}\`/\`${NC} to your search before submitting it. The default first two search choices are ${GREEN}\`canto\`${NC} and then ${GREEN}\`terzine\`${NC}. Press ${GREEN}<enter>${NC} for those defaults and type something else to search that.\n" clear && echo -e "You are searching ${CANTICLE^} ${CANTO_NUM}.\nAdd a trailing slash ${GREEN}\`/\`${NC} to your search before submitting it. The default first two search choices are ${GREEN}\`canto\`${NC} and then ${GREEN}\`terzine\`${NC}. Press ${GREEN}<enter>${NC} for those defaults and type something else to search that.\n"
i='0'
read -p "Load previous: (Y)/n " PREV read -p "Load previous: (Y)/n " PREV
case "$PREV" in case "$PREV" in
N|n) N|n)
@ -39,19 +42,22 @@ if [[ -e "$file" ]]; then
search="$(cat .prev-xpath.tmp)" search="$(cat .prev-xpath.tmp)"
;; ;;
esac esac
i='0'
# Start while loop for the query. Break the loop if the query ends with a slash `/`.
while true; do while true; do
read -e -i "$search" -p "$(echo -e "\nSearch: \n "${GREEN}"===> "${NC})" nsearch read -e -i "$search" -p "$(echo -e "\nSearch: \n "${GREEN}"===> "${NC})" nsearch
if [[ "${nsearch: -1}" == '/' ]]; then # if last char trailing slash then search if [[ "${nsearch: -1}" == / ]]; then # if last char trailing slash then search
path="${nsearch::-1}" path="${nsearch::-1}" # remove trailing slash
echo "$path" > .prev-xpath.tmp # remove trailing slash echo "$path" > .prev-xpath.tmp # store query for next time
xmllint --xpath "$path" $file | batcat -l 'xml' # perform the search (requires `xmllint` and `batcat` pager)
xmllint --xpath "$path" $file | batcat -l xml
exit exit
elif [[ "$i" == '0' ]] && [[ "$nsearch" == '' ]]; then elif [[ "$i" == 0 ]] && [[ "$nsearch" == '' ]]; then # load '/canto' query on first loop
search="/canto" search="/canto"
elif [[ "$nsearch" == '/canto' ]]; then elif [[ "$nsearch" == '/canto' ]]; then # load '/canto/terzine' on second loop
search="/canto/terzine" search="/canto/terzine"
else else
search="${nsearch}" search="${nsearch}" # else keep search the same
fi fi
i=$[$i+1] i=$[$i+1]
done done

Loading…
Cancel
Save