For source Insight show Android source correct "//TODO" make adjustments

Source: Internet
Author: User

Start with a simple script, and later found that some directories are with spaces, so it can not be processed. So check the information to make adjustments.


The contents of the shell script are:

#!/bin/bash

Test=$1

["$test"! = ""] && Action=echo

L= "c h cpp Java"

#owing to folder with space, so the find ... is a little complex
If ["$action"! = "echo"]; Then
For i in $l; Do
$action find. -name "*. $i"-print0|xargs-0-i sed-i "s://todo://todo:g" {}
Done
Else
Echo Find. -name "*. $i"-print0\|xargs-0-i sed-i "s://todo://todo:g" {}
Fi


Here's why:

-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; ls-l
Total 0
-rw-r--r--1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r--1 root root 0 2010-08-02 18:12 file 2.log
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; Find-name ' *.log '
./file 1.log
./file 2.log
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; Find-name ' *.log ' | Xargs RM
Rm:cannot remove './file ': No such file or directory
Rm:cannot remove ' 1.log ': No such file or directory
Rm:cannot remove './file ': No such file or directory
Rm:cannot remove ' 2.log ': No such file or directory
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; bye


The reason is very simple, xargs by default is a blank character (space, TAB, line break) to split the record, so the file name./file 1.logwas interpreted as two records../fileand the1.log, unfortunately RM cannot find these two files.

to solve this problem, the smart man came up with a way to get find to print out a file name and then output a NULL character (' + 'instead of a newline character, and then tell Xargs to use the NULL character as the delimiter for the record. This is find.-print0and Xargs's-0the origin of it.

-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; ls-l
Total 0
-rw-r--r--1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r--1 root root 0 2010-08-02 18:12 file 2.log
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; Find-name ' *.log '-print0 | hd
0 1 2 3 4 5 6 7 8 9 A B C D E F |0123456789abcdef|
--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
00000000:2E 2f 6c, 2e 6c 6f 67xx2e 2f 66 |. /file 1.log.. /f|
00000010:69 6c 2e 6c 6f 67xx|ile 2.log. |
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; Find-name ' *.log '-print0 | xargs-0 rm
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; Find-name ' *.log '
-([email protected]Linuxeden: Forum)-(~/tmp/find)-
[bash-4.1.5]
; bye


you might want to ask, why choose' + 'Instead of other characters to do separators? This is also easy to understand: General programming languages are used' + 'as the end flag of a string, the path name of the file cannot contain' + 'characters.


For source Insight show Android source correct "//TODO" make adjustments

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.