歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 實用的bash腳本

實用的bash腳本

日期:2017/3/1 14:50:40   编辑:關於Linux
實用的bash腳本 ping局域網中所有的機器 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 批量在客戶端添加hosts對應關系 #!/bin/bash for ((i=2; i<=100; i++)) do _ip=10.11.45.$i ssh $_ip "echo \"${_ip} \"\$(hostname) >> /etc/hosts" done 客戶端命令執行工具 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 例子: #batch_run.sh hosts.txt "cat /etc/hosts" 文件分發腳本 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 例子: #./batch_cp.sh hosts.txt /tmp/aaa /root/ 刪除文本中得重復行 在進行文本處理的時候,我們經常遇到要刪除重復行的情況。那怎麼解決呢? 下面就是三種常見方法? 第一,用sort+uniq,注意,單純uniq是不行的。 shell> sort file | uniq 這裡我做了個簡單的測試,當file中的重復行不再一起的時候,uniq將服務刪除所有的重復行。經過排序後,所有相同的行都在相鄰,因此unqi可以正常刪除重復行。第二,用sort+awk命令,注意,單純awk同樣不行,原因同上。 shell> sort file | awk '{if ($0!=line) print;line=$0}' awk '!a[$0]++' fetion_info.txt 當然,自己把管道後面的代碼重新設計一下,可能不需要sort命令先排序拉。第三,用sort+sed命令,同樣需要sort命令先排序。 shell> sort file | sed '$!N; /^(.*)n1$/!P; D' 最後附一個必須先用sort排序的文本的例子,當然,這個需要用sort排序的原因是很簡單,就是後面算法設計的時候的“局部性,相同的行可能分散出現在不同的區域,一旦有新的相同行出現,那麼前面的已經出現的記錄就被覆蓋了 comm --取兩個文件的差集合並集 今天需要取兩個文件的並集,自己寫腳本處理太麻煩,一查,果然有現成的工具,comm。 需要注意的事,使用comm之前,兩個文件都是必須是sort好了的。 In our work, we often encounter the following questions: 在我們的工作中,我經常遇到下面的問題: I have two files: file1 and file2: 有兩個文件:文件1和文件2: 1) How can I print out the lines that are only contained in file1? 1) 如何打印出只存在於文件1中的內容? 2) How can I print out the lines that are only contained in file2? 2) 如何打印出只存在於文件2中的內容? 3) How can I print out the lines that are contained both in file1 and file2? 3) 如何打印出文件1和文件2都有的內容? There is a powerful shell command that can easily meet our needs, it is: comm. 這有一個很好的shell命令能夠滿足我們的需求,它就是comm。 When you meet the above questions, "comm" should be your first choice:-) 當你遇到上面的問題,“comm”應該是你第一選擇:-) comm [ -123 ]??file1??file2 comm will read file1 and file2 and generate three columns of output: comm 將會讀取文件1和文件2並且產生三列輸出: lines only in file1; lines only??in file2; and lines in both files. 只存在文件1中的行;只存在文件2中的行;兩個文件都存在的行。 For detailed explanation, pls man comm. 更詳細的解釋,請參閱man comm。 Example: 例如: bash-2.03$ cat file1 11111111 22222222 33333333 44444444 55555555 66666666 77777777 88888888 99999999 bash-2.03$ cat file2 00000000 22222222 44444444 66666666 88888888 1) suppress lines unique to FILE1 1) 過濾掉file1中的內容 bash-2.03$ comm -1 file1 file2 00000000 22222222 44444444 66666666 88888888 2) suppress lines unique to FILE2 2) 過濾掉file2中的內容 bash-2.03$ comm -2 file1 file2 11111111 22222222 33333333 44444444 55555555 66666666 77777777 88888888 99999999 3) suppress lines that appear in both files 3) 過濾掉file1和file2中都有的內容 bash-2.03$ comm -3 file1 file2 00000000 11111111 33333333 55555555 77777777 99999999 4) Print out the lines that are only contained in file1? 4) 打印出只存在於文件1中的內容? bash-2.03$ comm -23 file1 file2 11111111 33333333 55555555 77777777 99999999 5) Print out the lines that are only contained in file2? 5) 打印出只存在於文件2中的內容? bash-2.03$ comm -13 file1 file2 00000000 6) Print out the lines that are contained both in file1 and file2? 6) 打印出文件1和文件2都有的內容? bash-2.03$ comm -12 file1 file2 22222222 44444444 66666666 88888888 Besides the comm, we still have various ways to finish the above tasks. 除了comm,我們還有其他方法來完成這些任務。 4) Print out the lines that are only contained in file1? 4) 打印出只存在於文件1中的內容? diff file1 file2 | grep "^<"|sed 's/^< //g' for i in $(>temp ; done; cat temp
Copyright © Linux教程網 All Rights Reserved