Practical bash script

Source: Internet
Author: User
Practical bash scripts ping all machines in the Lan foriin 'seq1254'; do (ping192.168.0.0. I & amp;); sleep0.1; killallping; doneforiin 'seq110 '; doping10.228.151. $ i-c1; done | grepttl batch practical bash script ping all machines in the Lan for I in 'seq 1 254 '; do (ping 192.168.0. $ I &); sleep 0.1; killall ping; done for I in 'seq 1 10'; do ping 10.228.151. $ I-c1; done | grep ttl: add hosts mappings to the client in batches #! /Bin/bash for (I = 2; I <= 100; I ++) do _ ip = 10.11.45. $ I ssh $ _ ip "echo \" $ {_ ip} \ "\ $ (hostname)>/etc/hosts" done client command execution tool vim batch_run.sh #! /Bin/bash # wangyang usage () {echo "Usage $0 list_file \" command \ "" exit 1} if [[$ #! = 2]; then usage fi _ file = $1 _ command = $2 if [! -F $ _ file]; then echo "ERR can't find list file $ _ file" exit 2 fi for _ ip in $ (cat $ _ file | grep-v "^ #") do echo $ _ ip ssh $ _ ip "$ _ command" echo done vim hosts.txt 192.168.0.2 192.168.0.3 192.168.0.4 example: # batch_run.sh hosts.txt "cat/etc/hosts" file distribution script vim batch_cp.sh #! /Bin/bash usage () {echo "Usage $0 list_file local_file remote_path" exit 1} if [[$ #! = 3]; then usage fi _ file = $1 _ lf = $2 _ rp = $3 if [! -F $ _ file]; then echo "ERR can't find list file $ _ file" exit 2 fi for _ ip in $ (cat $ _ file | grep-v "^ #") do echo $ _ ip scp-r $ _ lf $ _ ip: $ _ rp echo done vim hosts.txt 192.168.0.2 192.168.0.3 192.168.0.4 example :#. /batch_cp.sh hosts.txt/tmp/aaa/root/delete duplicate rows in text processing, we often encounter the need to delete duplicate rows. How can this problem be solved? Which of the following are three common methods? First, use sort + uniq. Note that uniq alone cannot be used. Shell> sort file | uniq I did a simple test here. when the duplicate rows in the file are no longer together, uniq deletes all duplicate rows from the service. After sorting, all identical rows are adjacent, so unqi can delete duplicate rows normally. Second, use the sort + awk command. Note that awk alone does not work, for the same reason. Shell> sort file | awk '{if ($0! = Line) print; line = $0} 'awk '! A [$0] + + 'fetion_info.txt of course, re-design the code behind the pipeline by yourself. the sort command may not be required to sort and pull the code first. Third, use the sort + sed command, which also requires the sort command to be sorted first. Shell> sort file | sed '$! N;/^ (. *) n1 $ /! P; d' finally comes with an example of text that must be sorted by sort first. of course, the reason for this need to be sorted by sort is very simple, that is, the "locality, the same row may appear in different regions. once a new same row appears, the preceding records will be overwritten with comm -- the union of the difference sets of the two files needs to be obtained today. it is too troublesome to write a script by yourself, there are ready-made tools, comm. Note that before using comm, both files must be sort. In our work, we often encounter the following questions: In our work, I often encounter the following problems: I have two files: file1 and file2: There are two files: file 1 and file 2:1) How can I print out the lines that are only contained in file1? 1) how to print the content that only exists in file 1? 2) How can I print out the lines that are only contained in file2? 2) how to print the content that only exists in file 2? 3) How can I print out the lines that are contained both in file1 and file2? 3) How can I print out the contents of both file 1 and file 2? There is a powerful shell command that can easily meet our needs, it is: comm. There is a good shell command that can meet our needs, it is comm. When you meet the above questions, "comm" shocould be your first choice :-) When you encounter the above problem, "comm" should be your first choice: -) comm [-123]? File1 ?? File2 comm will read file1 and file2 and generate three columns of output: comm will read file 1 and file 2 and generate three output columns: lines only in file1; lines only ?? In file2; and lines in both files. only the rows in file 1 exist, only the rows in file 2 exist, and the rows in both files exist. For detailed explanation, pls man comm. For more information, see man comm. Example: bash-2.03 $ cat file1111111112222222233333333444444445555555566666666777777778888888899999999bash-2.03 $ cat file200000000222222224444444466666688888888 1) suppress lines unique to FILE11) filter out contents in file1 bash-2.03 $ comm-1 file1 file2 00000000 22222222 44444444 66666666 88888888 2) suppress lines unique to FILE22) filter out contents in file2 bash-2.03 $ comm-2 file1 file2 11111111 2222222233333333 444444 4455555555 6666666677777777 8888888899999999 3) suppress lines that appear in both files3) filter out all contents in file1 and file2 bash-2.03 $ comm-3 file1 file2 000000001111111133333333555555557777777799999999 4) print out the lines that are only contained in file1? 4) print the content that only exists in file 1? Bash-2.03 $ comm-23 file1 file211111133333333555555777777779999999999 5) Print out the lines that are only contained in file2? 5) print the content that only exists in file 2? Bash-2.03 $ comm-13 file1 file200000000 6) Print out the lines that are contained both in file1 and file2? 6) print out all the contents of file 1 and file 2? Bash-2.03 $ comm-12 file1 file2222222224444444466666688888888 Besides the comm, we still have various ways to finish the above tasks. in addition to comm, we have other methods to complete these tasks. 4) Print out the lines that are only contained in file1? 4) print the content that only exists in file 1? Diff file1 file2 | grep "^ <" | sed's/^ <// g' for I in $ (> temp; done; cat temp
Related Article

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.