initial commit
This commit is contained in:
commit
69d63cb9ce
6 changed files with 139 additions and 0 deletions
7
README.md
Normal file
7
README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# lactozora the bash framework
|
||||
|
||||
A framework for making bash scripts with some useful features
|
||||
|
||||
# Where did the name come from?
|
||||
|
||||
Well, one day I was watching a youtube video about a new type of scam that was discovered by the youtuber Pleasent Green. Which dealt with becoming a reseller / middle man for a fictiious herb called lactozora. I basically just stole the name.
|
7
config.ini
Normal file
7
config.ini
Normal file
|
@ -0,0 +1,7 @@
|
|||
|
||||
[section]
|
||||
key=newvalue
|
||||
key2=fuck this
|
||||
|
||||
[newsection]
|
||||
key=newvalue
|
6
config.json
Normal file
6
config.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"bashini": "new",
|
||||
"section": {
|
||||
"key": "beer"
|
||||
}
|
||||
}
|
57
config_ini.bash
Executable file
57
config_ini.bash
Executable 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
config_json.bash
Executable file
44
config_json.bash
Executable 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"
|
18
script.bash
Normal file
18
script.bash
Normal file
|
@ -0,0 +1,18 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -eo pipefail
|
||||
|
||||
if [[ -n "${BASHD_DEBUG}" ]]; then # a inevitability that this will be used
|
||||
set -x
|
||||
fi
|
||||
|
||||
trap cleanup EXIT # A little more robust cleanup
|
||||
|
||||
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
|
||||
|
||||
# Code goes here
|
Loading…
Add table
Add a link
Reference in a new issue