摘要: 你会学到一种原创的同时循环两个列表的方法。类似于Python或者Haskell的zip函数,非常简洁直观,效果如下: $ paste ( seq 1 5 ) ( seq 129 133 ) | while read host ip; do echo " vm$host: 172.16.116.$ip " ; done vm1: 172.16 . 116.129 vm2: 172
摘要:
你会学到一种原创的同时循环两个列表的方法。类似于Python或者Haskell的zip函数,非常简洁直观,效果如下:
$ paste seq <span>1</span> <span>5</span>) seq <span>129</span> <span>133</span>) | <span>while</span> read host ip; <span>do</span> <span>echo</span> <span>"</span><span>vm$host: 172.16.116.$ip</span><span>"</span>; <span>done</span><span> vm1: </span><span>172.16</span>.<span>116.129</span><span> vm2: </span><span>172.16</span>.<span>116.130</span><span> vm3: </span><span>172.16</span>.<span>116.131</span><span> vm4: </span><span>172.16</span>.<span>116.132</span><span> vm5: </span><span>172.16</span>.<span>116.133</span>
详情:
在实际应用中,经常需要我们输入对应的两个列表,比如主机名和IP:
vm110 <span>172.18</span>.<span>11.129</span><span> vm111 </span><span>172.18</span>.<span>11.130</span><span> ...</span>
如果有很多的话,使用awk处理一个临时文件,然后使用while read来循环是不错的(例如从Excel里面拷贝成文本文件,然后用awk提取相应的列到一个文件):
<span>awk</span> <span>'</span><span>{print $1 $3}</span><span>'</span> orig.txt | <span>while</span> read host ip; <span>do</span> <span>echo</span> $host : $ip; <span>done</span> <p>但是,有没有能直接在命令行上生成这些列表并循环的方法呢?因为我更喜欢用for i in vm{110..120}; do echo $i; done这种方式来循环列表,但是这种方式只支持一个列表,怎么找到对应的另一个列表呢?</p> <p>直接google,就会发现没有什么好的方法(以下均来自StackOverflow):</p> <p>1、有的直接使用bash的数组甚至hash表,都是较新的版本才有,然后使用数字index来循环。这种方法一点也不直观:</p> <p> </p><pre class="brush:php;toolbar:false">list1=<span>"</span><span>a b c</span><span>"</span><span> list2</span>=<span>"</span><span>1 2 3</span><span>"</span><span> array1</span>=<span>($list1) array2</span>=<span>($list2) count</span>=<span>${#array1[@]} </span><span>for</span> i <span>in</span> `<span>seq</span> <span>1</span><span> $count` </span><span>do</span> <span>echo</span> ${array1[$i-<span>1</span>]} ${array2[$i-<span>1</span><span>]} </span><span>done</span>
谁也不想写类似${#array1[@]}这样的复杂表达,因为我们不是在编程,而是在输入一条命令。
2、有的使用了各种正则表达式命令,我一眼看不出来什么意思,没人会为了循环两个列表,去专门写一个脚本文件:
#!/bin/<span>sh</span><span> list1</span>=<span>"</span><span>1 2 3</span><span>"</span><span> list2</span>=<span>"</span><span>a b c</span><span>"</span> <span>while</span> [ -n <span>"</span><span>$list1</span><span>"</span><span> ] </span><span>do</span><span> head1</span>=`<span>echo</span> <span>"</span><span>$list1</span><span>"</span> | <span>cut</span> -d <span>'</span> <span>'</span> -f <span>1</span><span>` list1</span>=`<span>echo</span> <span>"</span><span>$list1</span><span>"</span> | <span>sed</span> <span>'</span><span>s/[^ ]* *\(.*\)$/\1/</span><span>'</span><span>` head2</span>=`<span>echo</span> <span>"</span><span>$list2</span><span>"</span> | <span>cut</span> -d <span>'</span> <span>'</span> -f <span>1</span><span>` list2</span>=`<span>echo</span> <span>"</span><span>$list2</span><span>"</span> | <span>sed</span> <span>'</span><span>s/[^ ]* *\(.*\)$/\1/</span><span>'</span><span>` </span><span>echo</span><span> $head1 $head2 </span><span>done</span>
还有其他几种,有兴趣的可以去看看,http://stackoverflow.com/questions/546817/iterating-over-two-lists-in-parallel-in-bin-sh。
但是有一种方法提醒了我:
list1=<span>"</span><span>aaa1 aaa2 aaa3</span><span>"</span><span> list2</span>=<span>"</span><span>bbb1 bbb2 bbb3</span><span>"</span><span> tmpfile1</span>=$( <span>mktemp</span> /tmp/list.XXXXXXXXXX ) || exit <span>1</span><span> tmpfile2</span>=$( <span>mktemp</span> /tmp/list.XXXXXXXXXX ) || exit <span>1</span> <span>echo</span> $list1 | <span>tr</span> <span>'</span> <span>'</span> <span>'</span><span>\n</span><span>'</span> ><span> $tmpfile1 </span><span>echo</span> $list2 | <span>tr</span> <span>'</span> <span>'</span> <span>'</span><span>\n</span><span>'</span> ><span> $tmpfile2 paste $tmpfile1 $tmpfile2 </span><span>rm</span> --force $tmpfile1 $tmpfile2
这种方法创建了两个临时文件,好像还不如前面的方法,但是在我看来,这很有启发性:他使用了paste来结合两个列表,这是linux下原生的合并列表命令,相当于其他语言的zip。
另外,临时文件也可以避免,因此我想出了以下的方法(并不推荐):
paste echo vm{<span>1</span>..<span>5</span>} | <span>tr</span> <span>'</span> <span>'</span> <span>'</span><span>\n</span><span>'</span>) echo <span>172.16</span>.<span>116</span>.{<span>129</span>..<span>133</span>} | <span>tr</span> <span>'</span> <span>'</span> <span>'</span><span>\n</span><span>'</span>) | <span>while</span> read host ip; <span>do</span> <span>echo</span> $host: $ip; <span>done</span>
其中vm{1..5}会产生“vm1 vm2 vm3 vm4 vm5”,以空格分隔,而paste是把两个列文件合并成一个,所以必须把空格替换成换行,这就是tr做的事。明显使用tr很不好,增加了命令的复杂度。
另外
于是我想到了seq,好像可以指定分隔符,一查文档,居然默认就是换行,于是命令得以大幅简化:
paste seq <span>1</span> <span>5</span>) seq <span>129</span> <span>133</span>) | <span>while</span> read host ip; <span>do</span> <span>echo</span> <span>"</span><span>vm$host: 172.16.116.$ip</span><span>"</span>; <span>done</span><span><br></span>
这个命令可以循环2个及以上同等长度的列表,而且非常直观。就是开头提到的方法。

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
