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

标签 linux bash shell sh

我正在尝试列出所有与 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/

相关文章:

bash - 在不同文件夹中的 bash 脚本中创建符号链接(symbolic link)

linux - ./yourscript.sh 和 sh yourscript.sh 之间有什么区别

linux - 对不受信任的源代码的编译和执行进行沙箱化的架构

c++ - 从另一个线程调用 socket->native_handle()

mysql - 当密码为空时跳过 bash 脚本中的 mysql 密码

Bash脚本错误: [i: command not found

linux - 仅删除逗号分隔文件中引号之间的逗号

linux - 如何忽略有关消失文件的 rsync 警告?

linux - 使用 SVN 保留文件权限

linux - 为什么我在 bash 中的 kill -9 命令上没有收到 SIGKILL 信号?