shell编写for例子

1.批量打包sh文件

#!/bin/bash

for i in `find . -maxdepth 1 -name "*.sh"`

do
        tar -czvf $i.tgz $i
done

2.批量解压文件

#!/bin/bash

for i in `find . -maxdepth 1 -name "*.tgz"`

do
        tar -xzvf $i -C /tmp/test/
done

3.批量拷贝文件

#!/bin/bash
#auto scp files for client
#by authors zgh 2019

FILES=$*

if [ -z $* ];then
        echo  -e 'Usages:{$0 /boot|/tmp|/tmp/test.txt}'
        exit
fi

#for i in `seq 100 200`
for i in `echo 192.168.1.15 127.0.0.1`

do
        #scp -r /tmp/test.txt  root@192.168.1.$i:/data/webapps/www
        scp /tmp/test.txt root@$i:/root/
done

 4.批量执行命令

#!/bin/bash
#auto scp files for client

for i in `seq 14 16`

do
        ssh -l root 192.168.1.$i 'ls /root'
done

 5.配置免密钥

ssh-keygen
回车
回车
回车
回车
回车

生成这两个文件

把id_rsa.pub密钥文件复制到需要操作得虚拟机上,并更名为authorized_keys,注意会覆盖原虚拟机的其他密钥

 

把密钥追加到authorized_keys里面

ssh-copy-id -i /root/.ssh/id_rsa.pub 127.0.0.1

原文地址:https://www.cnblogs.com/aqicheng/p/11995885.html