You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写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:

  1. Backup first: We make a copy of b.ini as b.ini.bak—always a safe habit so you can revert if something unexpected happens.
  2. Read a.conf line by line: The while loop splits each line into a driver key (like oracle) and its path (like /client64/lib) using = as the separator.
  3. Skip comments/empty lines: The if statement ignores any line starting with # or blank lines, so we only process valid driver entries.
  4. Match section names: Since b.ini uses uppercase section headers (like [ORACLE]) but a.conf uses lowercase keys, we convert the key to uppercase with tr to ensure a match.
  5. Grab the existing driver filename: We use grep to locate the correct section in b.ini, then extract the filename from the existing Driver line (e.g., libsqora.so.19.1).
  6. Update the Driver path: Using sed, we target the specific section in b.ini and replace the old path with the new one from a.conf, keeping the original driver filename. The -i.bak flag creates a temporary backup for sed (works on both Linux and macOS), which we clean up right after.
  7. Handle missing sections: If a driver from a.conf doesn't have a matching section in b.ini, we print a warning instead of crashing the script.

Testing with Your Example Files

For your given files:

  • a.conf has oracle=/client64/lib (uncommented)
  • b.ini has [ORACLE] with Driver=/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:07:32