Skip to main content
deleted 8 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using xq (from https://kislyuk.github.io/yq/) to just get those strings out:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

IFS=:

xq -r --arg path_string "$*" \
        'getpath(($path_string | split(":") | map(split("/")))[])' file.xml

This gives the path expressions to xq as a :-delimited list in the variable $path_string. This string is subsequently split into its constituent paths, and these are then further split into path elements, so that one path internally may look like

[
  "config",
  "global",
  "resources",
  "default_setup",
  "connection",
  "dbname"
]

The path arrays are given to the getpath() function which extracts the values located at those paths.

The output, for the given XML document, will be

localhost
root
pass123
testdb

Creating shell assignment statements instead:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

eval "$(
    IFS=:
    xq -r --arg path_string "$*" '
            ($path_string | split(":") | map(split("/"))[]) as $path |
            @sh "\($path[-1])=\(getpath($path)|@sh)"' file.xml
)"

printf 'host = "%s"\n' "$host"
printf 'user = "%s"\n' "$username"
printf 'pass = "%s"\n' "$password"
printf 'database = "%s"\n' "$dbname"

For the given paths and XML document, the xq statement above would create the output

'host'='localhost'host='localhost'
'username'='root'username='root'
'password'='pass123'password='pass123'
'dbname'='testdb'dbname='testdb'

This would be safe to eval to assign the host, username, password, and dbname shell variables.

The output of the script would be

host = "localhost"
user = "root"
pass = "pass123"
database = "testdb"

Using xq (from https://kislyuk.github.io/yq/) to just get those strings out:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

IFS=:

xq -r --arg path_string "$*" \
        'getpath(($path_string | split(":") | map(split("/")))[])' file.xml

This gives the path expressions to xq as a :-delimited list in the variable $path_string. This string is subsequently split into its constituent paths, and these are then further split into path elements, so that one path internally may look like

[
  "config",
  "global",
  "resources",
  "default_setup",
  "connection",
  "dbname"
]

The path arrays are given to the getpath() function which extracts the values located at those paths.

The output, for the given XML document, will be

localhost
root
pass123
testdb

Creating shell assignment statements instead:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

eval "$(
    IFS=:
    xq -r --arg path_string "$*" '
            ($path_string | split(":") | map(split("/"))[]) as $path |
            @sh "\($path[-1])=\(getpath($path))"' file.xml
)"

For the given paths and XML document, the xq statement above would create the output

'host'='localhost'
'username'='root'
'password'='pass123'
'dbname'='testdb'

This would be safe to eval to assign the host, username, password, and dbname shell variables.

Using xq (from https://kislyuk.github.io/yq/) to just get those strings out:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

IFS=:

xq -r --arg path_string "$*" \
        'getpath(($path_string | split(":") | map(split("/")))[])' file.xml

This gives the path expressions to xq as a :-delimited list in the variable $path_string. This string is subsequently split into its constituent paths, and these are then further split into path elements, so that one path internally may look like

[
  "config",
  "global",
  "resources",
  "default_setup",
  "connection",
  "dbname"
]

The path arrays are given to the getpath() function which extracts the values located at those paths.

The output, for the given XML document, will be

localhost
root
pass123
testdb

Creating shell assignment statements instead:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

eval "$(
    IFS=:
    xq -r --arg path_string "$*" '
            ($path_string | split(":") | map(split("/"))[]) as $path |
            "\($path[-1])=\(getpath($path)|@sh)"' file.xml
)"

printf 'host = "%s"\n' "$host"
printf 'user = "%s"\n' "$username"
printf 'pass = "%s"\n' "$password"
printf 'database = "%s"\n' "$dbname"

For the given paths and XML document, the xq statement above would create the output

host='localhost'
username='root'
password='pass123'
dbname='testdb'

This would be safe to eval to assign the host, username, password, and dbname shell variables.

The output of the script would be

host = "localhost"
user = "root"
pass = "pass123"
database = "testdb"
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using xq (from https://kislyuk.github.io/yq/) to just get those strings out:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

IFS=:

xq -r --arg path_string "$*" \
        'getpath(($path_string | split(":") | map(split("/")))[])' file.xml

This gives the path expressions to xq as a :-delimited list in the variable $path_string. This string is subsequently split into its constituent paths, and these are then further split into path elements, so that one path internally may look like

[
  "config",
  "global",
  "resources",
  "default_setup",
  "connection",
  "dbname"
]

The path arrays are given to the getpath() function which extracts the values located at those paths.

The output, for the given XML document, will be

localhost
root
pass123
testdb

Creating shell assignment statements instead:

#!/bin/sh

set -- \
        config/global/resources/default_setup/connection/host \
        config/global/resources/default_setup/connection/username \
        config/global/resources/default_setup/connection/password \
        config/global/resources/default_setup/connection/dbname

eval "$(
    IFS=:
    xq -r --arg path_string "$*" '
            ($path_string | split(":") | map(split("/"))[]) as $path |
            @sh "\($path[-1])=\(getpath($path))"' file.xml
)"

For the given paths and XML document, the xq statement above would create the output

'host'='localhost'
'username'='root'
'password'='pass123'
'dbname'='testdb'

This would be safe to eval to assign the host, username, password, and dbname shell variables.