目标:想用awk与scp命令批量传送文件

前提:先搭好主机间的免密登陆环境(参考:http://www.cnblogs.com/tankaixiong/p/4172942.html)

实现脚本方法:

1.在一个文件中记录好各个主机的端口与ip地址,第一列为主机描述信息,参考信息

[root@zejin240 share]# cat scp_port_ip.txt
host1 192.168.10.11
host2 192.168.20.22
host3 192.168.30.33
host4 192.168.40.44

2.编写shell脚本

[root@zejin240 share]# cat scp_all.sh
#!/bin/bash
src_path='/home/chenzejin/mysql_backup_all.sh'
des_path='/home/chenzejin/mysql_backup_all_from11.sh'
awk '{ cmd="scp -P"$2 "'" $src_path"'" " chenzejin@"$3":""'"$des_path"'";print cmd } ' /mnt/hgfs/share/scp_port_ip.txt

说明:最后一行用awk去调用主机ip与端口的信息,并拼接成一个完事的scp命令,这个脚本中只是打印拼接命令,确定拼接命令正确用,将print cmd改成system(cmd)即可完成真正命令的执行

调试信息如下:

[root@zejin240 share]# sh -x scp_all.sh
+ src_path=/home/chenzejin/mysql_backup_all.sh
+ des_path=/home/chenzejin/mysql_backup_all_from11.sh
+ awk '{ cmd="scp -P"$2 " /home/chenzejin/mysql_backup_all.sh" " chenzejin@"$3":""/home/chenzejin/mysql_backup_all_from11.sh";print cmd } ' /mnt/hgfs/share/scp_port_ip.txt
scp -P33330 /home/chenzejin/mysql_backup_all.sh chenzejin@192.168.10.11:/home/chenzejin/mysql_backup_all_from11.sh
scp -P33331 /home/chenzejin/mysql_backup_all.sh chenzejin@192.168.20.22:/home/chenzejin/mysql_backup_all_from11.sh
scp -P33332 /home/chenzejin/mysql_backup_all.sh chenzejin@192.168.30.33:/home/chenzejin/mysql_backup_all_from11.sh
scp -P33333 /home/chenzejin/mysql_backup_all.sh chenzejin@192.168.40.44:/home/chenzejin/mysql_backup_all_from11.sh

说明:在使用awk命令引用外部变量时,需要注意:外部变量名需要用"'"$src_path"'"这样的格式来引用,而awk中的域信息$0 $1 $2……等不用任何引号包含。

为什么需要用"'"$src_path"'"这样的格式来引用外部变量呢,其实shell在解析时是从左到匹配单引号与双引号的,会一步步解释过去,具体的可以参考其它的文章:http://www.cnblogs.com/mydomain/archive/2012/09/24/2699467.html

至此,完成了我们的设定目标。

awk引用外部变量及调用系统命令方法的更多相关文章

  1. shell if判断(曾经被一个字符串相等的判断纠结半小时,最后只是if后少了个空格!) 和 awk引用外部变量判断

    一.if判断 数字: $A=12 $B=15 if(("$A"<"$B")) if(("$A"=="$B")) 字 ...

  2. 【shell】awk引用外部变量

    在使用awk的过程中,经常会需要引用外部变量,但是awk需要使用单引号将print包起来,导致print后的$引用无效,可以采用下面的方式 例如: #!/bin/bash a="line1 ...

  3. Python:字符串中引用外部变量的3种方法

    方法一: username=input('username:') age=input('age:') job=input('job:') salary=input('salary') info1='' ...

  4. JavaScript获取后台C#变量以及调用后台方法

    http://www.educity.cn/develop/495493.html 有时需要在JavaScript中获取后台变量的值,来判断JavaScript的执行逻辑,或者需要调用C#后台方法获取 ...

  5. block引用外部变量原理

    block在赋值时才会生成对应的block结构体实例(结构体数据结构在编译时已经生成),赋值时会扫一遍里面引用的外部变量(嵌套block中的外部变量也算,只不过嵌套block中的外部变量会被内外两个b ...

  6. awk获取外部变量

    语法 awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file...] 获得普通外部变量 [xingxing.dx ...

  7. python函数定义中引用外部变量的一个问题

    如果在函数定义的默认值中引用了一个外部变量,如下所示 x = 3 def func(a = x): print(a, x) 那么a的默认值就会是3, 但是print语句中的x会是调用时的x值 lamb ...

  8. awk 根据外部变量匹配某一域值

    shell>> i='a' awk '$1 ~ /'$i'/ {print $0}' test.txt awk中,变量 增加单引号即可

  9. 外部事件触发调用对象方法时this指向问题

    问题如下: var obj = { name: 'dang', test:function(){ alert(this.name); } }; obj.test(); //这样是可以的 $('.box ...

随机推荐

  1. 关于 MySQL 的 boolean 和 tinyint(1)

    boolean类型MYSQL保存BOOLEAN值时用1代表TRUE,0代表FALSE,boolean在MySQL里的类型为tinyint(1),MySQL里有四个常量:true,false,TRUE, ...

  2. tcpdump交叉编译及使用

    第一步.下载 官方网站:http://www.tcpdump.org/ 需要下载libpcap包和tcpdump包 我下载的版本是:libpcap-1.4.0.tar.gz和tcpdump-4.4.0 ...

  3. [Aaronyang] 写给自己的WPF4.5 笔记22 [3d交互与动画 3/4]

    OK,前面我们的3d模型都比较囧啊,最近也看了一点ZAM了解了一下,大致至少可以做个简单的模型用来演示. 1.交互,动起来的思路 ①修改Model3D对象的变换 ②修改应用于ModelVisual3D ...

  4. c++ bind1st 和 bind2nd的用法

    std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码. 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用 ...

  5. Initialize the Storage Emulator by Using the Command-Line Tool

    http://msdn.microsoft.com/en-us/library/azure/gg433132.aspx To initialize the storage emulator Click ...

  6. Mac上编译libimobiledevice库

    0.准备工作: 使用brew或Mac Ports安装:libgnutls or openssl. libplist .libusb.libusbmuxd 1.下载代码: 下载地址:https://gi ...

  7. 在Window下安装Oracle 12C Cloud Control Agent

    ① 准备好安装源,这个ORACLE普通账号无法下载到,有需要的可以联系我   p14570373_112000_Generic.zip,用于Windows 64位操作系统 ② 解压p14570373_ ...

  8. 阿里云 Redis 服务遇到的问题

    ERR unknown command eval 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: St ...

  9. Visual Studio 新建项目报错" this template attempted to load component assembly 'NuGet.VisualStudio.Interop, ….".

    "Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version ...

  10. SAP ECC PP 配置文档

    SAP ECC 6.0 Configuration Document Production Planning & Control (PP) 1. General Settings 1.1 Ma ...