0% found this document useful (0 votes)
4 views1 page

Bakbashrc

The document is a bash script that checks for the installation of jq, a JSON processor. If jq is installed, it defines functions to retrieve values from a JSON configuration file and to navigate directories based on aliases specified in that file. It also provides error handling for missing configuration files and undefined aliases.

Uploaded by

tono.sudar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Bakbashrc

The document is a bash script that checks for the installation of jq, a JSON processor. If jq is installed, it defines functions to retrieve values from a JSON configuration file and to navigate directories based on aliases specified in that file. It also provides error handling for missing configuration files and undefined aliases.

Uploaded by

tono.sudar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Check if jq is installed

if ! command -v jq &> /dev/null


then
echo "jq could not be found. Please install it to use JSON features in bashrc."
# Installation instructions for user reference
# On Ubuntu/Debian: sudo apt-get install jq
# On macOS: brew install jq
else
# Define the location of your configuration file
BASH_CONFIG_FILE="$HOME/.[Link]"

# Function to get a value from the JSON config using a jq path (e.g., .api_key)
get_config_value() {
if [ -f "$BASH_CONFIG_FILE" ]; then
# The -r option outputs the raw value without quotes
jq -r "$1" "$BASH_CONFIG_FILE"
else
echo "Error: Configuration file not found at $BASH_CONFIG_FILE" >&2
fi
}

# Example Usage:
# Exporting a variable on shell startup
export MY_API_KEY=$(get_config_value '.api_key')

# Defining a custom navigation function using the aliases from the JSON file
j_nav() {
local target_path
target_path=$(get_config_value ".aliases.$1")

if [ -n "$target_path" ] && [ "$target_path" != "null" ]; then


cd "$target_path" || echo "Error: Could not change directory to $target_path"
else
echo "Alias '$1' not found in JSON config."
fi
}
fi

You might also like