awk referencing external variables and invoking system command methods

Source: Internet
Author: User
Tags scp command

Goal: To bulk transfer files with awk and SCP commands

Prerequisite: First set up the free-secret landing environment between the host (reference: http://www.cnblogs.com/tankaixiong/p/4172942.html)

Implementing the Scripting Method:

1. The port and IP address of each host are recorded in a file, the first column is the host description information, the reference information

Cat33330192.168. 10.11  33331192.168. 20.22  33332192.168. 30.33  33333192.168. 40.44

2. Writing shell scripts

[Email protected] share]#CatScp_all.SH #!/bin/Bashsrc_path='/home/chenzejin/mysql_backup_all.sh'Des_path='/home/chenzejin/mysql_backup_all_from11.sh'awk '{cmd= "scp-p "' "$src _path"'" [email protected]" $ ": "' "$des _path"'";p rint cmd}'/mnt/hgfs/share/Scp_port_ip.txt

Description: The last line with awk to call the host IP and port information, and stitching into a finished SCP command, the script is just print stitching command, to determine the correct stitching command, the print cmd to System (CMD) to complete the execution of the real command .

The debugging information is as follows:

[Email protected] 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" $ "/home/chenzejin/mysql_backup_all.sh" "[email protected]" $ ":"/home/chenzejin/mysql_backup_ all_from11.sh ";p rint cmd}'/mnt/hgfs/share/Scp_port_ip.txtSCP-p33330/home/chenzejin/mysql_backup_all.SH[Email protected]192.168.10.11:/home/chenzejin/mysql_backup_all_from11.SHSCP-p33331/home/chenzejin/mysql_backup_all.SH[Email protected]192.168.20.22:/home/chenzejin/mysql_backup_all_from11.SHSCP-p33332/home/chenzejin/mysql_backup_all.SH[Email protected]192.168.30.33:/home/chenzejin/mysql_backup_all_from11.SHSCP-p33333/home/chenzejin/mysql_backup_all.SH[Email protected]192.168.40.44:/home/chenzejin/mysql_backup_all_from11.SH

Description: When referencing an external variable using the awk command, it is important to note that the external variable name needs to be referenced in the format "'" $src _path "'", and the domain information in awk is $ ... Wait without any quotation marks included.

Why do you need to use "'" $src _path "'" format to refer to external variables, in fact, the shell in the parsing is from left to match single quotation marks and double quotes, will step by step explain the past, specific can refer to other articles: http://www.cnblogs.com/ Mydomain/archive/2012/09/24/2699467.html

At this point, we have completed our set goals.

awk referencing external variables and invoking system command methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.