Last active
March 2, 2025 21:58
-
-
Save Nikolai2038/bf637343962f740314b46875ad05d56b to your computer and use it in GitHub Desktop.
Bash: Get and set specified variable in specified section of `.ini` file using `awk`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
main() { | |
local ini_file="${1}" && { shift || true; } | |
local section_name="${1}" && { shift || true; } | |
local variable_name="${1}" && { shift || true; } | |
if [ -z "${ini_file}" ] || [ -z "${section_name}" ] || [ -z "${variable_name}" ]; then | |
echo "Usage: ini_file_variable_get.sh <ini_file> <section_name> <variable_name> [is_error_if_section_is_not_found=1] [is_error_if_variable_is_not_found=1]" >&2 | |
return 1 | |
fi | |
local is_error_if_section_is_not_found="${1:-1}" && { shift || true; } | |
local is_error_if_variable_is_not_found="${1:-1}" && { shift || true; } | |
awk -v section="${section_name}" -v variable="${variable_name}" -v error_on_section_not_found="${is_error_if_section_is_not_found}" -v error_on_variable_not_found="${is_error_if_variable_is_not_found}" ' | |
BEGIN { | |
in_section = 0; | |
section_found = 0; | |
variable_found = 0; | |
} | |
$0 ~ "^\\[" section "\\]$" { | |
in_section = 1; | |
section_found = 1; | |
next; | |
} | |
$0 ~ "^\\[" { | |
in_section = 0; | |
} | |
in_section && $0 ~ "^" variable "[[:space:]]*=" { | |
split($0, arr, "="); | |
value = arr[2]; | |
gsub(/^[[:space:]]+|[[:space:]]+$/, "", value); | |
print value; | |
variable_found = 1; | |
exit; | |
} | |
END { | |
if (!section_found && error_on_section_not_found == 1) { | |
print "Error: Section [" section "] not found." > "/dev/stderr"; | |
exit 1; | |
} | |
if (section_found && !variable_found && error_on_variable_not_found == 1) { | |
print "Error: Variable " variable " not found in section [" section "]." > "/dev/stderr"; | |
exit 1; | |
} | |
} | |
' "${ini_file}" || return "$?" | |
return 0 | |
} | |
main "$@" || exit "$?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
main() { | |
local ini_file="${1}" && { shift || true; } | |
local section_name="${1}" && { shift || true; } | |
local variable_name="${1}" && { shift || true; } | |
local new_value="${1}" && { shift || true; } | |
if [ -z "${ini_file}" ] || [ -z "${section_name}" ] || [ -z "${variable_name}" ]; then | |
echo "Usage: ini_file_variable_set.sh <ini_file> <section_name> <variable_name> [new_value] [is_error_if_section_is_not_found=1] [is_error_if_variable_is_not_found=1]" >&2 | |
return 1 | |
fi | |
local is_error_if_section_is_not_found="${1:-1}" && { shift || true; } | |
local is_error_if_variable_is_not_found="${1:-1}" && { shift || true; } | |
local temp_file | |
temp_file="$(mktemp)" || return "$?" | |
awk -v section="${section_name}" -v variable="${variable_name}" -v new_value="${new_value}" -v error_on_section_not_found="${is_error_if_section_is_not_found}" -v error_on_variable_not_found="${is_error_if_variable_is_not_found}" ' | |
BEGIN { | |
in_section = 0; | |
section_found = 0; | |
variable_found = 0; | |
} | |
$0 ~ "^\\[" section "\\]$" { | |
in_section = 1; | |
section_found = 1; | |
print $0; | |
next; | |
} | |
$0 ~ "^\\[" { | |
in_section = 0; | |
print $0; | |
next; | |
} | |
in_section && $0 ~ "^[#;]?[[:space:]]*" variable "[[:space:]]*=" { | |
# Handle commented or uncommented variable | |
# Also, keep spaces around "=" | |
match($0, "^[#;]?[[:space:]]*" variable "([[:space:]]*=[[:space:]]*)", arr); | |
suffix = arr[1]; | |
# Print the updated variable without the comment | |
print prefix variable suffix new_value; | |
variable_found = 1; | |
next; | |
} | |
{ | |
print $0; | |
} | |
END { | |
if (!section_found && error_on_section_not_found == 1) { | |
print "Error: Section [" section "] not found." > "/dev/stderr"; | |
exit 1; | |
} | |
if (section_found && !variable_found && error_on_variable_not_found == 1) { | |
print "Error: Variable [" variable "] not found in section [" section "]." > "/dev/stderr"; | |
exit 1; | |
} | |
# If section was found but variable was not found, add the variable to the section | |
if (section_found && !variable_found) { | |
print variable "=" new_value; | |
} | |
} | |
' "${ini_file}" > "${temp_file}" || return "$?" | |
# If we cannot move file - remove temp file | |
mv "${temp_file}" "${ini_file}" || { local return_code="$?"; rm "${temp_file}"; return "${return_code}"; } | |
return 0 | |
} | |
main "$@" || exit "$?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment