我正在尝试列出所有与 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
我是一名优秀的程序员,十分优秀!