Given the Configuration File:
# Pi-hole SSL Configuration File
# Script Settings
#logfile=/var/log/"$SCRIPT".log
# Certbot Settings
EMAIL=Pi-hole@jpits.us
WEBROOT=/var/www/html/
notDOMAIN=pi-hole.jpits.us
Given this as a function:
if [ ! -f "$CFG_FILE" ]; then
echo "NO CONFIGURATION FILE"
exit 1
fi
CFG_CONTENT=$(cat $CFG_FILE | sed -E '/[^=]+=[^=]+/!d' | sed -E 's/\s+=\s/=/g')
eval "$CFG_CONTENT"
neededVars=(
EMAIL
WEBROOT
DOMAIN
)
echo 'Certbot Configuration:'
for var in "${neededVars[@]}"
do
# echo "${required}"
if [ -n "${!var}" ]; then
echo -e "\t $var: ${!var}"
else
echo -e "Missing Required Value:\t" [$var]
echo -n "Please enter a value: "
read ${var}
echo $var
fi
done
# Check to see if a configuration profile is set
# If set, then check to see if missing items.
# If missing, items, get known ones, and ask user for unknown
# All else fails, we cannot run this script in silent mode...
# WOrst case, exit 1 with error status, user is smart and can fix
#CFG_FILE=/etc/test.conf
The actual name of the variable I want to read is stored in a variable. How do I tell the read command to read the variable name which is inside that variable?
Any help is a appreciated and would make my day.
The output:
Your such a dumbo. Were you even thinking? Go to bed and get a good 8 hours of sleep. Your mind will work much faster.
#!/bin/bash
# Pi-hole SSL Activator
# Pretty much runs itself...
## Set Global Variables
CFG_FILE=/tmp/test.conf
function runSilentCommand() {
command="$@"
if [ -z "$command" ]; then
echo 'Null Usage of function runSilentCommand'
else
bash -c "${command[@]}" &>/dev/null
if [ "$?" -gt 0 ]; then
ecode="$?"
echo -e "COMMAND FAILURE:\t" "${command[@]}"
echo -e "EXIT CODE:\t" "$ecode"
return "$ecode"
else
return 0
fi
fi
}
function installCertbot() {
apt-get install -y software-properties-common python-software-properties
add-apt-repository ppa:certbot/certbot
apt-get update
}
function generateCert() {
certbot --quiet --agree-tos --email "$email" certonly --webroot -w "$webroot" -d "$domain"
}
function editConfig() {
echo
}
function configCheck() {
if [ ! -f "$CFG_FILE" ]; then
echo "NO CONFIGURATION FILE"
exit 1
fi
CFG_CONTENT=$(cat $CFG_FILE | sed -E '/[^=]+=[^=]+/!d' | sed -E 's/\s+=\s/=/g')
eval "$CFG_CONTENT"
neededVars=(
EMAIL
WEBROOT
DOMAIN
)
echo 'Certbot Configuration:'
for var in "${neededVars[@]}"
do
# echo "${required}"
if [ -n "${!var}" ]; then
echo -e "\t $var: ${!var}"
else
echo -e "Missing Required Value:\t" [$var]
echo -n "Please enter a value: "
read var
echo $var
fi
done
# Check to see if a configuration profile is set
# If set, then check to see if missing items.
# If missing, items, get known ones, and ask user for unknown
# All else fails, we cannot run this script in silent mode...
# WOrst case, exit 1 with error status, user is smart and can fix
#CFG_FILE=/etc/test.conf
}
function main() {
# Testing the silent function. Will kill safari's parent process found in app path
runSilentCommand 'kill -9 $(pgrep -f "Safari.app" | head -n 1)'
configCheck
}
# Run Me!
main
# Options: interactive, verbose, silent, disable, uninstall