mirror of
https://github.com/pi-hole/pi-hole
synced 2024-11-18 14:18:15 +00:00
remove comments and blank lines when parsing files
This commit is contained in:
parent
36907edd50
commit
d51e0c49b1
@ -107,7 +107,7 @@ echo_current_diagnostic() {
|
|||||||
log_write "\n${COL_LIGHT_PURPLE}*** [ DIAGNOSING ]:${COL_NC} ${1}"
|
log_write "\n${COL_LIGHT_PURPLE}*** [ DIAGNOSING ]:${COL_NC} ${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
file_exists() {
|
if_file_exists() {
|
||||||
# Set the first argument passed to tihs function as a named variable for better readability
|
# Set the first argument passed to tihs function as a named variable for better readability
|
||||||
local file_to_test="${1}"
|
local file_to_test="${1}"
|
||||||
# If the file is readable
|
# If the file is readable
|
||||||
@ -338,7 +338,7 @@ diagnose_operating_system() {
|
|||||||
echo_current_diagnostic "Operating system"
|
echo_current_diagnostic "Operating system"
|
||||||
|
|
||||||
# If there is a /etc/*release file, it's probably a supported operating system, so we can
|
# If there is a /etc/*release file, it's probably a supported operating system, so we can
|
||||||
file_exists /etc/*release && \
|
if_file_exists /etc/*release && \
|
||||||
# display the attributes to the user from the function made earlier
|
# display the attributes to the user from the function made earlier
|
||||||
get_distro_attributes || \
|
get_distro_attributes || \
|
||||||
# If it doesn't exist, it's not a system we currently support and link to FAQ
|
# If it doesn't exist, it's not a system we currently support and link to FAQ
|
||||||
@ -409,6 +409,7 @@ ping_gateway() {
|
|||||||
|
|
||||||
# If the gateway variable has a value (meaning a gateway was found),
|
# If the gateway variable has a value (meaning a gateway was found),
|
||||||
if [[ -n "${gateway}" ]]; then
|
if [[ -n "${gateway}" ]]; then
|
||||||
|
log_write "${INFO} Default gateway: ${gateway}"
|
||||||
# Let the user know we will ping the gateway for a response
|
# Let the user know we will ping the gateway for a response
|
||||||
log_write "* Trying three pings on IPv${protocol} gateway at ${gateway}..."
|
log_write "* Trying three pings on IPv${protocol} gateway at ${gateway}..."
|
||||||
# Try to quietly ping the gateway 3 times, with a timeout of 3 seconds, using numeric output only,
|
# Try to quietly ping the gateway 3 times, with a timeout of 3 seconds, using numeric output only,
|
||||||
@ -647,11 +648,23 @@ process_status(){
|
|||||||
|
|
||||||
make_array_from_file() {
|
make_array_from_file() {
|
||||||
local filename="${1}"
|
local filename="${1}"
|
||||||
|
# If the file is a directory
|
||||||
if [[ -d "${filename}" ]]; then
|
if [[ -d "${filename}" ]]; then
|
||||||
|
# do nothing since it cannot be parsed
|
||||||
:
|
:
|
||||||
else
|
else
|
||||||
|
# Otherwise, read the file line by line
|
||||||
while IFS= read -r line;do
|
while IFS= read -r line;do
|
||||||
file_content+=("${line}")
|
# Strip out comments and blank lines
|
||||||
|
new_line=$(echo "${line}" | sed -e 's/#.*$//' -e '/^$/d')
|
||||||
|
# If the line still has content
|
||||||
|
if [[ -n "${new_line}" ]]; then
|
||||||
|
# Put it into the array
|
||||||
|
file_content+=("${new_line}")
|
||||||
|
else
|
||||||
|
# Otherwise, it's a blank line or comment, so do nothing
|
||||||
|
:
|
||||||
|
fi
|
||||||
done < "${filename}"
|
done < "${filename}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@ -669,7 +682,7 @@ parse_file() {
|
|||||||
# For each line in the file,
|
# For each line in the file,
|
||||||
for file_lines in "${file_info[@]}"; do
|
for file_lines in "${file_info[@]}"; do
|
||||||
# Display the file's content
|
# Display the file's content
|
||||||
log_write " ${file_lines}" | grep -v "#" | sed '/^$/d'
|
log_write " ${file_lines}"
|
||||||
done
|
done
|
||||||
# Set the IFS back to what it was
|
# Set the IFS back to what it was
|
||||||
IFS="$OLD_IFS"
|
IFS="$OLD_IFS"
|
||||||
@ -680,7 +693,7 @@ diagnose_setup_variables() {
|
|||||||
echo_current_diagnostic "Setup variables"
|
echo_current_diagnostic "Setup variables"
|
||||||
|
|
||||||
# If the variable file exists,
|
# If the variable file exists,
|
||||||
file_exists "${VARSFILE}" && \
|
if_file_exists "${VARSFILE}" && \
|
||||||
log_write "* Sourcing ${VARSFILE}...";
|
log_write "* Sourcing ${VARSFILE}...";
|
||||||
# source it
|
# source it
|
||||||
source ${VARSFILE};
|
source ${VARSFILE};
|
||||||
@ -713,7 +726,7 @@ dir_check() {
|
|||||||
# For each file in the directory,
|
# For each file in the directory,
|
||||||
for filename in "${directory}"; do
|
for filename in "${directory}"; do
|
||||||
# check if exists first; if it does,
|
# check if exists first; if it does,
|
||||||
file_exists "${filename}" && \
|
if_file_exists "${filename}" && \
|
||||||
# do nothing
|
# do nothing
|
||||||
: || \
|
: || \
|
||||||
# Otherwise, show an error
|
# Otherwise, show an error
|
||||||
|
Loading…
Reference in New Issue
Block a user