[root@kafka-server01 scripts]# cat /root/scripts/info 192.168.37.131 root 123456 192.168.37.132 root 123456 [root@kafka-server01 scripts]# vim 6.sh #!/bin/bash file=/root/scripts/info while read line;do user=$(echo $line | cut -d " " -f 2) ip=$(echo $line |cut -d " " -f 1) passwd=$(echo $line|cut -d " " -f 3) # cmd=$* echo "-- $user -- $ip -- $passwd" done < $file [root@kafka-server01 scripts]# sh 6.sh -- root -- 192.168.37.131 -- 123456 -- root -- 192.168.37.132 -- 123456
如果你还记得管道的用法,这个结构应该不难理解吧。command命令的输出作为read循环的输入,这种结构长用于处理超过一行的输出,当然awk也很擅长做这种事
案例:
[root@kafka-server01 scripts]# cat /root/scripts/info 192.168.37.131 root 123456 192.168.37.132 root 123456 [root@kafka-server01 scripts]# vim 5.sh #!/bin/bash file=/root/scripts/info cat $file|while read line;do user=$(echo $line | cut -d " " -f 2) ip=$(echo $line |cut -d " " -f 1) passwd=$(echo $line|cut -d " " -f 3) # cmd=$* echo "-- $user -- $ip -- $passwd" done [root@kafka-server01 scripts]# sh 5.sh -- root -- 192.168.37.131 -- 123456 -- root -- 192.168.37.132 -- 123456