Linux Shell脚本实现INI文件添加、删除、修改及读取操作
感谢DeepSeek大模型,生成的脚本,稍作修改即可使用。先上使用说明,后附完整代码。
#!/bin/bash VERSION="1.0" SCRIPT_NAME=$(basename "$0") show_help() { cat << EOF Usage: $SCRIPT_NAME <command> [options] Commands: add Add a new key-value pair to a section update Update an existing key-value pair delete Delete a key or entire section read Read a value from the INI file Options: -f, --file INI file path (required) -s, --section Section name (required for all commands) -k, --key Key name (required for add/update/delete/read) -v, --value Value to set (required for add/update) Examples: $SCRIPT_NAME add -f config.ini -s Database -k host -v localhost $SCRIPT_NAME read -f config.ini -s Database -k host EOF } add_ini() { local file="$1" local section="$2" local key="$3" local value="$4" # Create file if not exists [ -f "$file" ] || touch "$file" # Check if key already exists if grep -q "^\[$section\]" "$file"; then if awk -v section="$section" -v key="$key" ' BEGIN { in_section = 0 } /^\[.*\]$/ { in_section = ($0 == "[" section "]") } in_section && $1 == key { found = 1; exit } END { exit !found }' FS='=' "$file"; then echo "Error: Key '$key' already exists in section [$section]" >&2 return 1 fi fi # Add new entry if ! grep -q "^\[$section\]" "$file"; then # Section doesn't exist, append to end printf "\n[%s]\n%s=%s\n" "$section" "$key" "$value" >> "$file" else # Section exists, insert after section header sed -i.bak -e "/^\[$section\]/a $key=$value" "$file" fi } update_ini() { local file="$1" local section="$2" local key="$3" local value="$4" # Verify section exists if ! grep -q "^\[$section\]" "$file"; then echo "Error: Section [$section] does not exist" >&2 return 1 fi # Verify key exists if ! awk -v section="$section" -v key="$key" ' BEGIN { in_section = 0 } /^\[.*\]$/ { in_section = ($0 == "[" section "]") } in_section && $1 == key { found = 1; exit } END { exit !found }' FS='=' "$file"; then echo "Error: Key '$key' not found in section [$section]" >&2 return 1 fi # Update value sed -i.bak -e "/^\[$section\]/,/^\[/s/^$key\s*=.*/$key=$value/" "$file" } delete_ini() { local file="$1" local section="$2" local key="$3" if [ -z "$key" ]; then # Delete entire section sed -i.bak -e "/^\[$section\]/,/^\[.*\]/ { /^\[$section\]/d; /^\[.*\]/!d; }" "$file" else # Delete specific key sed -i.bak -e "/^\[$section\]/,/^\[/ { /^$key\s*=/d }" "$file" fi } read_ini() { local file="$1" local section="$2" local key="$3" local found=0 awk -v section="$section" -v key="$key" ' BEGIN { FS = "="; in_section = 0 } /^\[.*\]$/ { in_section = ($0 == "[" section "]") } in_section && $1 == key { print $2 found = 1 exit } END { if (!found) { if (!in_section) { print "Error: Section [$section] not found" > "/dev/stderr" } else { print "Error: Key '$key' not found" > "/dev/stderr" } exit 1 } } ' "$file" } # Main command processing case $1 in add|update|delete|read) cmd=$1 shift ;; -h|--help) show_help exit 0 ;; -v|--version) echo "$SCRIPT_NAME version $VERSION" exit 0 ;; *) echo "Error: Unknown command '$1'" >&2 show_help exit 1 ;; esac # Parse common options file="" section="" key="" value="" while [[ $# -gt 0 ]]; do case $1 in -f|--file) file="$2" shift 2 ;; -s|--section) section="$2" shift 2 ;; -k|--key) key="$2" shift 2 ;; -v|--value) value="$2" shift 2 ;; --) shift break ;; -*) echo "Error: Invalid option '$1'" >&2 exit 1 ;; *) break ;; esac done # Validate required parameters [ -z "$file" ] && { echo "Error: Missing --file parameter" >&2; exit 1; } [ -z "$section" ] && { echo "Error: Missing --section parameter" >&2; exit 1; } case $cmd in add|update) [ -z "$key" ] && { echo "Error: Missing --key parameter" >&2; exit 1; } [ -z "$value" ] && { echo "Error: Missing --value parameter" >&2; exit 1; } ;; delete|read) [ -z "$key" ] && key="" ;; esac # Execute command case $cmd in add) add_ini "$file" "$section" "$key" "$value" ;; update) update_ini "$file" "$section" "$key" "$value" ;; delete) delete_ini "$file" "$section" "$key" ;; read) read_ini "$file" "$section" "$key" ;; esac
凯特网版权声明:以上内容允许转载,但请注明出处,谢谢!