Print space of echo

Source: Internet
Author: User

Print space of echo

Edit a script to verify the md5 of files in batches

 

#! /Bin/bashmd5sum = "/usr/bin/md5sum" count = 0 # record how many files are involved in md5 comparison check = 0 # record how many files md5 is correct while read linedo echo-e $ line | $ md5sum-c if [$? -Eq 0]; then let check + = 1 fi let count + = 1 done
When I run the command, I always prompt that my file is incorrectly formatted. My file is generated by md5sum. How can this problem be solved?

 

Then I run

 

echo "dbdf9049296c84c1f295e8c467a210d0  /usr/bin/grub-mkrescue" | md5sum -c
Is displayed correctly,

 

Then I ran it by chance.

 

echo "dbdf9049296c84c1f295e8c467a210d0 /usr/bin/grub-mkrescue" | md5sum -c
It turns out to be wrong. How is it possible?

 

I began to carefully compare and dizzy, with a space difference. Then, as a flash of thought, echo $ line treats consecutive spaces as one,

 

#! /Bin/bashmd5sum = "/usr/bin/md5sum" count = 0 # record how many files are involved in md5 comparison check = 0 # record how many files md5 is correct while read linedo echo-e "$ line" | $ md5sum-c if [$? -Eq 0]; then let check + = 1 fi let count + = 1 done
 
  
Pay attention to this issue when you learn it. Now let's take a look:
  

 

 

tmp="a b   d"echo $tmpa b decho "$tmp"a b   d

 

 

As for the reason, you need to understand the shell parsing process:

Replace the variable to execute the command according to the parameters after the command is divided by IFS (a variable in the Shell script, IFS (Internal Field Seprator), Internal domain separator, the Section enclosed by "" and "is used as a parameter of the command.

The execution process of echo $ tmp is:

Replace variable: echo a B d run the command: echo a B d divide the parameters a, B, d after the echo command by IFS, that is, echo has three parameters, not one

Therefore, the output is a B d, and only one space is displayed.

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.