gpt4 book ai didi

linux - 将可用 Windows 服务列表传递到数组中并提示用户选择要激活的服务

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:37 28 4
gpt4 key购买 nike

我正在尝试列出所有与 Linux 主机模式匹配的 Windows 服务,并提示用户选择要激活的服务。我有下面的脚本,但是有一个问题。服务名称有空格。因此,空格后的每个单词都被读取为数组中的新元素。你能帮忙解决这个问题吗?

#!/bin/bash
host="hhk"
domain="hck"
user="rdama"
pw="Pa$$w0rd"

get_installedversions(){
array=( $(net rpc service list -I $host -U $domain/$user%$pw | grep -i Rpc) )
len=${#array[*]}
echo "The array has length $len members. They are:"

i=0
while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
declare -a "var$i=${array[$i]}"
let i++
done
echo -n "Enter the service to be activated: "
read version
}
get_installedversions
echo selected service is ${array[$version]}
net rpc activate service ${array[$version] -I $host -U $domain/$user%$pw

当前输出:

1) RpcEptMapper    5) RpcLocator     9) (RPC)         13) Procedure
2) "RPC 6) "Remote 10) Locator" 14) Call
3) Endpoint 7) Procedure 11) RpcSs 15) (RPC)"
4) Mapper" 8) Call 12) "Remote
#?

要求的输出:

1) RpcEptMapper            "RPC Endpoint Mapper"
2) RpcLocator "Remote Procedure Call (RPC) Locator"
3) RpcSs "Remote Procedure Call (RPC)"
#?

“net rpc service list”命令的输出是

RpcEptMapper            "RPC Endpoint Mapper"
RpcLocator "Remote Procedure Call (RPC) Locator"
RpcSs "Remote Procedure Call (RPC)"

谢谢,R 达玛。

最佳答案

使用 select 命令要简单得多。我们假设 net rpc service list 输出的单词列表不会受到分词或路径名扩展的过度影响。

# Just to cut down on some repetition
run_net_rpc () {
net rpc "$@" -I "$host" -U "$domain/$user%$pw"
}

readarray -t services < <(run_net_rpc service list | grep -i Rpc)
select svc in "${services[@]}"; do
run_net_rpc activate service "$svc"
break
done

关于linux - 将可用 Windows 服务列表传递到数组中并提示用户选择要激活的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49888528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com