92 lines
2 KiB
Bash
Executable file
92 lines
2 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# this is the main script template
|
|
|
|
set -eo pipefail #these can cause problems when you want stuff to keep going
|
|
|
|
if [[ -n "${BASHD_DEBUG}" ]]; then # a inevitability that this will be used
|
|
set -x
|
|
fi
|
|
|
|
trap cleanup EXIT # A little more robust cleanup
|
|
|
|
# trap errors and set the ERR trap
|
|
trap 'echo -e "\nERROR: $BASH_COMMAND\nFILE: ${BASH_SOURCE[0]}\nLINE: ${BASH_LINENO[0]}\n" >&2; exit 1' ERR
|
|
|
|
cleanup() {
|
|
# We can clean up any temp files or what nots, but for now a place holder
|
|
true
|
|
}
|
|
|
|
source ./config_json.bash # can be json or ini, I preffer json
|
|
|
|
# Keydb / Redis interaction functions, works for both
|
|
|
|
dbcmd="keydb-cli"
|
|
|
|
# Check if keydb-cli is installed
|
|
check_redis_cli() {
|
|
if ! command -v $dbcmd &> /dev/null; then
|
|
echo "Error: $dbcmd is not installed. Please install keydb package."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Save data to Redis
|
|
# Usage: redis_save key value
|
|
redis_save() {
|
|
local key="$1"
|
|
local value="$2"
|
|
|
|
if [[ -z "$key" || -z "$value" ]]; then
|
|
echo "Error: Both key and value are required"
|
|
echo "Usage: redis_save key value"
|
|
return 1
|
|
fi
|
|
|
|
if $dbcmd SET "$key" "$value"; then
|
|
echo "Successfully saved '$value' with key '$key'"
|
|
else
|
|
echo "Failed to save data to Redis"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Retrieve data from Redis
|
|
# Usage: redis_get key
|
|
redis_get() {
|
|
local key="$1"
|
|
|
|
if [[ -z "$key" ]]; then
|
|
echo "Error: Key is required"
|
|
echo "Usage: redis_get key"
|
|
return 1
|
|
fi
|
|
|
|
local value
|
|
value=$($dbcmd GET "$key")
|
|
if [[ $? -eq 0 ]]; then
|
|
if [[ -z "$value" ]]; then
|
|
echo "No value found for key '$key'"
|
|
return 1
|
|
else
|
|
echo "$value"
|
|
fi
|
|
else
|
|
echo "Failed to retrieve data from Redis"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Example usage
|
|
check_redis_cli
|
|
|
|
# Uncomment these lines to test the functions
|
|
# redis_save "mykey" "Hello from Bash!"
|
|
# redis_get "mykey"
|
|
|
|
# Code goes here
|
|
|
|
redis_save "bashini" "new"
|
|
redis_get "bashini"
|
|
|