Skip to content

Instantly share code, notes, and snippets.

@si-rbrisita
Forked from jexp/neo.sh
Last active October 30, 2015 18:01
Show Gist options
  • Save si-rbrisita/854a717fb1e8beeb56b3 to your computer and use it in GitHub Desktop.
Save si-rbrisita/854a717fb1e8beeb56b3 to your computer and use it in GitHub Desktop.
Send Cypher commands to Neo4j instance.
#!/usr/bin/env bash
# usage neo.sh [-h host:port] [-u user:pass] [cmd]
# end cypher statements with semicolon
# terminate read loop with ^d or return
# Originially from: https://gist.github.com/jexp/8213614
# Slight edits and comments by:
# @author Robert Brisita <rbrisita@snap-interactive.com>
# Check if host parameter was given
HOST="localhost:7474"
if [ "$1" == "-h" ]
then
shift 1 # Remove argument name from argument array
HOST="$1" # Save value
shift 1 # Remove argument value from argument array
fi
# Check if authorization parameter was given
AUTH=""
if [ "$1" == "-u" ]
then
shift 1
AUTH="-u $1"
shift 1
fi
# Set up Neo4j execution point
URL="http://${HOST}/db/manage/server/console"
# Set up CURL HTTP Headers
HEADERS=' -H accept:application/json -H content-type:application/json '
# The run function with given command
function run
{
CMD="$@"
DATA="{\"command\":\"${CMD}\",\"engine\":\"shell\"}"
RES=$(curl -s $HEADERS $AUTH -d "$DATA" "$URL") # Execute CURL
# Bash substitution
RES=${RES#'[ "'}
PROMPT_PATTERN="\", \"neo4j-sh (*)$ \" ]"
RES=${RES%$PROMPT_PATTERN}
# Continue reading, incomplete command
if [ "$RES" == "\", \"> \" ]" ]
then
return 1
else
echo -e "${RES//\\\\n/\\n}"
return 0
fi
}
# Check if command given
if [ "$@" ]
then
run "$@"
exit $?
fi
# Create Neo4j shell command
NEO_SH="neo4j-sh (*)\$ "
# Display prompt (-p) and read into CMD
read -p "$NEO_SH" CMD;
# Run command and continue reading if need be
while [ "$CMD" ]; do
run "$CMD"
if [ $? == 0 ]
then
read -p "$NEO_SH" CMD
else
# Continue reading, incomplete command
read -p "> " CMD1
CMD="${CMD} ${CMD1}" # Build command
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment