getopts 获取同一参数的多个值
我希望使用getOpts从同一参数中获取多个值。我想在通过文件提供的主机列表上使用此脚本进行SSH并运行命令。
用法:。\ ssh.sh -f file_of_hosts.txt -c“ command1”“ command2”
预期输出:
ssh userid@server1:command1
ssh userid@server1:command2
ssh userid@server2:commnand1
ssh userid@server2:commnand2
我使用的示例代码,但无法获得预期的结果
id="rm08397"
while getopts ":i:d:s:f:" opt
do
case $opt in
f ) file=$OPTARG;;
c ) cmd=$OPTARG;;
esac
done
shift "$(($OPTIND -1))"
# serv=$(for host in `cat $file`
# do
# echo -e "$host#"
# done
# )
# for names in $serv
# do
# ssh $id@$serv:
for hosts in $file;do
for cmds in $cmd;do
o1=$id@$hosts $cmds
echo $o1
done
done
I am looking to get multiple values from same argument using getopts. I want to use this script to ssh and run commands on a list of hosts provided through a file.
Usage: .\ssh.sh -f file_of_hosts.txt -c "Command1" "command2"
Expected output:
ssh userid@server1:command1
ssh userid@server1:command2
ssh userid@server2:commnand1
ssh userid@server2:commnand2
Sample Code I used but failed to get expected results
id="rm08397"
while getopts ":i:d:s:f:" opt
do
case $opt in
f ) file=$OPTARG;;
c ) cmd=$OPTARG;;
esac
done
shift "$(($OPTIND -1))"
# serv=$(for host in `cat $file`
# do
# echo -e "$host#"
# done
# )
# for names in $serv
# do
# ssh $id@$serv:
for hosts in $file;do
for cmds in $cmd;do
o1=$id@$hosts $cmds
echo $o1
done
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过重复
-c
:You can achieve the effect by repeating
-c
: