For 企业应用案例

来源:互联网 发布:西安交通大学软件学院 编辑:程序博客网 时间:2024/03/29 21:42

For循环语句主要用于对某个数据域进行循环读取、对文件进行遍历,通常用于需要循环某个文件或列表。

语法格式如下:

For var in (表达式)

do

语句

done

1、输出1至100

for i in {1..100}

do

         echo$i

done


2、将文件中的信息输出来

list.txt中信息如下:

192.168.1.11

192.168.1.12

192.168.1.13

192.168.1.14

192.168.1.15

for i in `cat list.txt`

do

        echo $i

done

3、通过ssh远程查看磁盘使用情况,或看操作其他命令

list.txt中信息如下:

127.0.0.11

127.0.0.12

127.0.0.13

127.0.0.14

127.0.0.15

#/bin/bash

#2017-11-21

#author by scott

USER="root"

IP_LIST=`cat ip.txt`

COMMAND="$*"

for ip in $IP_LIST

do

         echo-e "\033[32mThe Ip Address $ip Exec Command for Result:\033[0m"

         ssh-l $USER $ip $COMMAND

done

注:ssh每次都要求输入密码,可以做免秘钥


4、拷贝文件至远程服务器

#!/bin/bash

#2017-11-21

#author by scott

FILE="$1"

USER="root"

REMOTE_DIR="/tmp"

IP=`cat ip.txt`

if [ $# -eq 0 ];then

         echo-e "----------------------------------------------------"

         echo-e "\033[34mUsage:Please Exce Script Menu Follow:$0|ip.txt|list.txt\033[0m"

         echo-e "\033[35m1)ip.txt\033[0m"

         echo-e "\033[35m2)list.txt\033[0m"

else

for i in $IP

do

         echo-e "\033[32mThe Ip Address $i Exec Command for Result:\033[0m"

         scp$FILE $USER@$i:$REMOTE_DIR

done

fi

原创粉丝点击