Chinaunix
标题:
shell 同时循环两个变量 问题
[打印本页]
作者:
水底游
时间:
2013-08-27 19:59
标题:
shell 同时循环两个变量 问题
#!/bin/sh
a=`cat ip.txt|awk '{print $1}'`
b=`cat ip.txt|awk '{print $2}'`
for i in $a
do
expect -c"
set ip $i
set password $j
set timeout 3
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof;"
done
我要怎样传入$i和$j的值的?
遇到好几次类似这样要同时循环两个变量的。。。都不知怎么搞
ip.txt的内容 $i获取ip $j获取密码
[root@localhost expect]# cat ip.txt
192.16.1.211 centos
192.16.1.212 qwe123
192.16.1.213 centos
192.16.1.214 centos214
192.16.1.216 centosjava
192.16.1.240 suselinux
192.16.1.246 panabit
192.16.1.250 centos
我上面的那个脚本应该怎样传入$i和$j的值的?
复制代码
作者:
关阴月飞
时间:
2013-08-27 20:03
试试:
while read i j
do
expect -c"
set ip $i
set password $j
set timeout 3
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof;"
done <ip.txt
复制代码
作者:
seesea2517
时间:
2013-08-28 09:24
楼上的方法最简单,还有另外两个方案可以参考:
[seesea@UC ~]$ cat xx.sh
#!/bin/sh
# 方法一
a=( `cat ip.txt|awk '{print $1}'` )
b=( `cat ip.txt|awk '{print $2}'` )
echo "方法一"
for ((i=0; i<${#a[@]}; ++i))
do
echo "${a[$i]} -- ${b[$i]}"
done
# 方法二
echo "方法二"
while read -a ar_line
do
echo "${ar_line[0]} -- ${ar_line[1]}"
done < ip.txt
[seesea@UC ~]$ bash xx.sh
方法一
192.16.1.211 -- centos
192.16.1.212 -- qwe123
192.16.1.213 -- centos
192.16.1.214 -- centos214
192.16.1.216 -- centosjava
192.16.1.240 -- suselinux
192.16.1.246 -- panabit
192.16.1.250 -- centos
方法二
192.16.1.211 -- centos
192.16.1.212 -- qwe123
192.16.1.213 -- centos
192.16.1.214 -- centos214
192.16.1.216 -- centosjava
192.16.1.240 -- suselinux
192.16.1.246 -- panabit
192.16.1.250 -- centos
复制代码
作者:
waker
时间:
2013-08-28 09:45
本帖最后由 waker 于 2013-08-28 09:46 编辑
#!/bin/sh \
exec expect "$0" ${1+"$@"}
set timeout 3
set infile [ open ip.txt ]
while { [ gets $infile inline ] >= 0 } {
set ip [lindex $inline 0]
set password [lindex $inline 1]
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof
}
复制代码
作者:
水底游
时间:
2013-08-28 09:50
最后的
done<ip.txt什么作用?或者是什么意思?
回复
2#
关阴月飞
作者:
水底游
时间:
2013-08-28 09:58
循环读取文件内容,明白了
回复
5#
水底游
作者:
关阴月飞
时间:
2013-08-28 09:59
回复
5#
水底游
就是读取ip.txt的内容
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2