编写Shell脚本:利用a.conf非注释内容更新b.ini的Driver路径
Hey there! Since you're new to shell scripting, let's put together a straightforward script that does exactly what you need—updating the Driver paths in b.ini using the uncommented entries from a.conf. I'll break it down step by step so you understand how it works.
The Shell Script
#!/bin/bash # First, create a backup of b.ini just in case something goes wrong cp b.ini b.ini.bak # Read each line from a.conf while IFS='=' read -r driver_key driver_path; do # Skip lines that are comments (start with #) or empty if [[ -z "$driver_key" || "$driver_key" =~ ^# ]]; then continue fi # Convert the driver key to uppercase to match the section names in b.ini driver_section=$(echo "$driver_key" | tr '[:lower:]' '[:upper:]') # Extract the existing driver filename from b.ini (e.g., libsqora.so.19.1) existing_driver_file=$(grep -A 10 "^\[$driver_section\]$" b.ini | grep -E "^Driver\s*=" | cut -d'=' -f2 | xargs basename) # Only proceed if we found the driver file (to avoid errors for missing sections) if [[ -n "$existing_driver_file" ]]; then # Update the Driver line in the corresponding section of b.ini sed -i.bak "/^\[$driver_section\]$/,/^\[/ s/^\(Driver\s*=\s*\).*/\1${driver_path}\/${existing_driver_file}/" b.ini # Remove the temporary sed backup file (Linux/Mac compatible) rm -f b.ini.bak else echo "Warning: No [${driver_section}] section found in b.ini, skipping..." fi done < a.conf echo "Update complete! Original b.ini backed up to b.ini.bak"
What Each Part Does
Let's break down the script so you know exactly what's happening:
- Backup first: We make a copy of
b.iniasb.ini.bak—always a safe habit so you can revert if something unexpected happens. - Read
a.confline by line: Thewhileloop splits each line into a driver key (likeoracle) and its path (like/client64/lib) using=as the separator. - Skip comments/empty lines: The
ifstatement ignores any line starting with#or blank lines, so we only process valid driver entries. - Match section names: Since
b.iniuses uppercase section headers (like[ORACLE]) buta.confuses lowercase keys, we convert the key to uppercase withtrto ensure a match. - Grab the existing driver filename: We use
grepto locate the correct section inb.ini, then extract the filename from the existingDriverline (e.g.,libsqora.so.19.1). - Update the Driver path: Using
sed, we target the specific section inb.iniand replace the old path with the new one froma.conf, keeping the original driver filename. The-i.bakflag creates a temporary backup forsed(works on both Linux and macOS), which we clean up right after. - Handle missing sections: If a driver from
a.confdoesn't have a matching section inb.ini, we print a warning instead of crashing the script.
Testing with Your Example Files
For your given files:
a.confhasoracle=/client64/lib(uncommented)b.inihas[ORACLE]withDriver=/usr/lib/oracle/19.5/client64/lib/libsqora.so.19.1
After running the script, the [ORACLE] section's Driver line will become:
Driver = /client64/lib/libsqora.so.19.1
You'll also see a warning about the db2 entry from a.conf (since there's no [DB2] section in b.ini).
内容的提问来源于stack exchange,提问作者Kracki
相关产品推荐
相关产品推荐

