shell Ksh -通过ssh命令声明类型(OOP)

7tofc5zh  于 2023-06-30  发布在  Shell
关注(0)|答案(1)|浏览(112)

我试图在示例化之前通过ssh远程声明一个类型(类)。有没有可能序列化类型的方法?
下面是一个简化的REPRODUCTIBLE示例(没有构造函数)。
先谢谢你。

#!/bin/ksh

typeset -T SubConfiguration_t=(
    dirs=(
        bin="binDir"
        lib="libDir"
    )

    ports=(
        ssh="22"
    )
)

typeset -T Configuration_t=(
    test=(
        hello="hello world !"
    )
    SubConfiguration sub=()
)

ssh user@host /bin/ksh << EOF
    set -x
    $(typeset -p .sh.type.SubConfiguration_t) # This outputs: typeset -r -b -L 4 .sh.type.SubConfiguration=(dirs=(bin=binDir;lib=libDir;)ports=(ssh=22))
    $(typeset -p .sh.type.Configuration_t)    # Same pattern
    Configuration_t config
    echo ${config.sub.dirs.lib}
EOF

输出:

+ .sh.type.SubConfiguration_t.dirs.bin=binDir
+ .sh.type.SubConfiguration_t.dirs.lib=libDir
+ .sh.type.SubConfiguration_t.ports.ssh=22
+ typeset -r -b -L 4 .sh.type.SubConfiguration_t
Usage: typeset [-bflmnprstuxACHS] [-a[type]] [-i[base]] [-E[n]] [-F[n]] [-L[n]]
               [-M[mapping]] [-R[n]] [-X[n]] [-h string] [-T[tname]] [-Z[n]]
               [name[=value]...]
   Or: typeset [ options ] -f [name...]
k3fezbri

k3fezbri1#

我的回答有点晚了。
在shell中,您可以源代码片段。
在同一目录中为每种类型创建一个单独的文件,即测试脚本文件,然后运行test_ssh. ksh。
我不建议直接操作. sh.type命名空间。

子配置_t文件:

typeset -T SubConfiguration_t=(
    dirs=(
        bin="binDir"
        lib="libDir"
    )

    ports=(
        ssh="22"
    )
)

配置_t文件:

typeset -T Configuration_t=(
    test=(
        hello="hello world !"
    )
    SubConfiguration_t sub=()
)

test_ssh.ksh文件:

#!/bin/ksh

scrpt=${0##*/}
scrptDir=${0%/*}

ssh user@host /bin/ksh << EOF
    set -x
#
# source types here
#
${ < ${scrptDir}/SubConfiguration_t;}
${ < ${scrptDir}/Configuration_t;}
#
# source script code here
#
    typeset -p .sh.type.SubConfiguration_t
    typeset -p .sh.type.Configuration_t
    typeset -p .sh.type

    Configuration_t config

    typeset -p config

    echo \${config.sub.dirs.lib}
EOF

相关问题