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

求助:通过Shell脚本从server.properties生成MySQL登录命令

Solution to Generate MySQL Login Command from server.properties

You can use either a bash one-liner with basic text parsing tools, or a more efficient awk script that reads the file just once. Here are both options tailored to your needs:

Option 1: Grep + Sed (Simple, Multiple File Reads)

Run this command directly in your terminal to generate the login string:

mysql -u$(grep 'umpdb.username' server.properties | cut -d'=' -f2) -p$(grep 'umpdb.password' server.properties | cut -d'=' -f2) -h$(grep 'umpdb.url' server.properties | sed -E 's/.*\/\/([^:]+):.*/\1/') $(grep 'umpdb.url' server.properties | sed -E 's/.*\/([^?]+)\?.*/\1/')

Breakdown of each part:

  • grep 'umpdb.username' server.properties | cut -d'=' -f2: Extracts the username by splitting the line at = and grabbing the second segment.
  • grep 'umpdb.password' server.properties | cut -d'=' -f2: Does the same to pull the password value.
  • sed -E 's/.*\/\/([^:]+):.*/\1/': Parses the URL to isolate the hostname (everything between // and the port colon).
  • sed -E 's/.*\/([^?]+)\?.*/\1/': Extracts the database name (between the final / and the ? in the URL parameters).

Option 2: Awk (Efficient, Single File Read)

This version is better for performance since it reads server.properties only once, which is helpful if the file is large:

awk -F'=' '
/umpdb.username/ { user = $2 }
/umpdb.password/ { pass = $2 }
/umpdb.url/ {
    # Split URL using delimiters: //, :, /, ?
    split($2, url_parts, /\/\/|\?|:|\//)
    host = url_parts[2]
    db_name = url_parts[4]
}
END {
    printf "mysql -u%s -p%s -h%s %s\n", user, pass, host, db_name
}
' server.properties

How this works:

  • -F'=': Sets the field separator to = so we can easily access the value side of each property.
  • We store username and password in variables as we encounter their lines.
  • For the URL line, we split the string into segments using regex delimiters, then pick out the hostname and database name from the resulting array.
  • Finally, we format and print the complete MySQL login command.

Both commands will output exactly what you requested:

mysql -ustackuser -pstackpass -h10.66.11.44 MT_SMS_CHN

内容的提问来源于stack exchange,提问作者Ashiq kt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:29:46