Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim comments from ini file lines only when # is outside of quotes #961

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion distrobox-assemble
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,35 @@ run_distrobox() (
fi
)

# Remove comments from ini file line. Respects quoted instances of `#`.
# Arguments:
# a value string
# Outputs:
# a value string with the comments removed
remove_comments() {
line="${1}"
filtered_line=$(echo "${line}" | awk '
# Configure awk to treat each character as a separate field
BEGIN { FS="" }
{
# Loop through each character of the line
for(i = 1; i <= NF; i++){
# If character is a quote, toggle the quoting flag
if ($i == "\"") {
q = !q
}
# If character is a hash and we are outside of quotes, stop processing
if (!q && $i == "#") {
exit
}
# Print the current character
printf("%s", $i)
}
}
')
echo "${filtered_line}"
}

# Sanitize an input, add single/double quotes and escapes
# Arguments:
# a value string
Expand Down Expand Up @@ -397,7 +426,7 @@ parse_file() (
continue
fi
# Remove comments and trailing spaces
line="$(echo "${line}" | sed 's/#.*//g' | sed 's/\s*$//g')"
line="$(remove_comments "${line}" | sed 's/\s*$//g')"

# Detect start of new section
if [ "$(echo "${line}" | cut -c 1)" = '[' ]; then
Expand Down