Satan made me do it

This commit is contained in:
kake26 2024-11-15 22:48:57 -06:00
parent 7c113360c8
commit 795f72db03
Signed by: kake26
GPG key ID: E8AFC43591876B4D
9 changed files with 118 additions and 69 deletions

7
modules/config.ini Normal file
View file

@ -0,0 +1,7 @@
[section]
key=newvalue
key2=fuck this
[newsection]
key=newvalue

6
modules/config.json Normal file
View file

@ -0,0 +1,6 @@
{
"bashini": "new",
"section": {
"key": "beer"
}
}

57
modules/config_ini.bash Executable file
View file

@ -0,0 +1,57 @@
#!/bin/bash
set -eo pipefail
# To read a value from the INI file
# Assume the INI file is formatted like:
# [section]
# key=value
# Function to read a value from an INI-style file
read_ini() {
local INI_FILE="$1"
local SECTION="$2"
local KEY="$3"
# Use awk to read the specific section and key
awk -F '=' "/^\[$SECTION\]/ {section=1} section==1 && /^$KEY=/ {print \$2; exit}" "$INI_FILE"
}
# To write or update a value in the INI file
# Function to set a value in an INI-style file
write_ini() {
local INI_FILE="$1"
local SECTION="$2"
local KEY="$3"
local VALUE="$4"
# Create the INI file if it doesn't exist
if [ ! -f "$INI_FILE" ]; then
touch "$INI_FILE"
fi
# Check if the key already exists
if grep -q "^\[$SECTION\]" "$INI_FILE"; then
if grep -q "^$KEY=" "$INI_FILE"; then
# If the key exists, update it
sed -i "/^\[$SECTION\]/, /^\[/ { /^$KEY=/ s/=.*/=$VALUE/ }" "$INI_FILE"
else
# If the key does not exist, add it under the section
sed -i "/^\[$SECTION\]/a $KEY=$VALUE" "$INI_FILE"
fi
else
# If the section does not exist, add the section and the key
echo -e "\n[$SECTION]\n$KEY=$VALUE" >> "$INI_FILE"
fi
}
# Usage
#write_ini "config.ini" "section" "key" "newvalue"
#write_ini "config.ini" "newsection" "key" "newvalue"
# Usage
#value=$(read_ini "config.ini" "section" "key")
#echo "$value"
#value=$(read_ini "config.ini" "section" "key2")
#echo "$value"

44
modules/config_json.bash Executable file
View file

@ -0,0 +1,44 @@
#!/bin/bash
JSON_FILE="config.json"
read_json() {
local JSON_FILE="$1"
local SECTION="$2"
local KEY="$3"
jq -r ".${SECTION}[\"${KEY}\"]" "$JSON_FILE"
}
# Function to write a value to a JSON configuration file
write_json() {
local JSON_FILE="$1"
local SECTION="$2"
local KEY="$3"
local VALUE="$4"
# It important to check if the file is valid otherwise stuff breaks
if ! jq -e . < "$JSON_FILE" > /dev/null 2>&1; then
echo "Error: JSON file is not valid JSON."
return 1
fi
# Using jq to set the value. The original file is replaced with a new file with the updated value.
jq ".${SECTION}.${KEY} = \"$VALUE\"" "$JSON_FILE" > "temp.json" && mv "temp.json" "$JSON_FILE"
}
create_json() {
local JSON_FILE="$1"
echo '{"bashini": "new"}' | jq '.' > "$JSON_FILE" # well we need something in there
}
check_json() {
local JSON_FILE="$1"
if [ ! -f "$JSON_FILE" ]; then
create_json "$JSON_FILE"
fi
}
check_json "$JSON_FILE"

70
modules/keydb.bash Normal file
View file

@ -0,0 +1,70 @@
#!/bin/bash
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"

47
modules/menu.bash Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
# Function to display the menu using whiptail
show_menu() {
while true; do
CHOICE=$(whiptail --title "Main Menu" \
--menu "Choose an option:" 15 60 4 \
"1" "Option 1" \
"2" "Option 2" \
"3" "Option 3" \
"4" "Exit" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus != 0 ]; then
echo "Menu cancelled."
exit 1
fi
case $CHOICE in
1)
whiptail --title "Option 1" --msgbox "You selected Option 1" 8 45
;;
2)
whiptail --title "Option 2" --msgbox "You selected Option 2" 8 45
;;
3)
whiptail --title "Option 3" --msgbox "You selected Option 3" 8 45
;;
4)
clear
echo "Goodbye!"
exit 0
;;
esac
done
}
# Check if whiptail is installed
if ! command -v whiptail &> /dev/null; then
echo "Error: whiptail is not installed"
echo "Please install it using: sudo apt-get install whiptail"
exit 1
fi
# Clear the screen and show the menu
clear
show_menu

14
modules/ntfy.bash Normal file
View file

@ -0,0 +1,14 @@
#!/bin/bash
# was split out to a seperate project, but will be pulled in when done
# check for curl
if ! command -v curl &> /dev/null
then
echo "This module needs curl to be installed."
exit
fi
ntfyserver="http://192.168.168:83"

55
modules/sys.bash Executable file
View file

@ -0,0 +1,55 @@
#!/bin/bash
#set -x
# a bash file that can provide various bits of info about the system
function cpuinfo() {
# now lets extract the number of core and the architecture
num_cores=$(lscpu | grep -E "^CPU\(s\):" | awk '{print $2}')
architecture=$(lscpu | grep -E "^Architecture:" | awk '{print $2}')
# get cpu usage
cpu_usage=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}')
echo "$num_cores cores, $architecture architecture, CPU usage: $cpu_usage"
}
function meminfo() {
echo "Memory:"
# We will get to this later
#total=$(free -h| grep ) #| grep -E "Mem|Swap"
total=$(free -h | awk '/^Mem/ {print $2}')
used=$(free -h | awk '/^Mem/ {print $3}')
free=$(free -h | awk '/^Mem/ {print $4}')
echo "Total: $total, Used: $used, Free: $free"
}
function diskinfo() {
echo "Disk:"
# We will get to this later
disks=$(lsblk -d -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL | awk 'NR>1 {print $1,$2,$3,$4,$5}')
for disk in $disks; do
echo "$disk"
done
}
function networkinfo() {
echo "Network:"
# get network interfaces and usage
net=$(ifstat -j | jq -r '.[] | "\(.interface) TX:\(.tx.kbps) RX:\(.rx.kbps)"')
for interface in $net; do
echo "$interface"
done
}
# This seems to be a good layout for sys.bash for now
cpuinfo
meminfo
diskinfo
networkinfo