[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Long variable value get corrupted sometimes

From: Daniel Qian
Subject: Re: Long variable value get corrupted sometimes
Date: Thu, 17 Feb 2022 09:58:19 +0800

Thanks for your tips, a lot learned.

Greg Wooledge <greg@wooledge.org> 于2022年2月16日周三 20:47写道:

> On Wed, Feb 16, 2022 at 04:10:40PM +0800, Daniel Qian wrote:
> > FOO=$(cat /tmp/foo.txt)
> > got_md5=$(echo "$FOO" | md5sum -b | cut -d ' ' -f 1)
> In addition to what other people have said... echo is not reliable.  It
> may alter your text, if you feed it backslashes, or arguments like "-e"
> or "-n".
> text=$(cat /tmp/foo.txt)                    # this strips all trailing 
> newlines
> md5=$(printf '%s\n' "$text" | md5sum -b)    # this adds one newline
> md5=${md5%% *}                              # this removes " *-"
> If you need to preserve the correct number of trailing newlines, then
> you'll also have to change the command substitution.  The common
> workaround is:
> text=$(cat /tmp/foo.txt; printf x)
> text=${text%x}
> If you do this, remember that the final newline(s) are still inside the
> variable, so you don't need to add one:
> md5=$(printf %s "$text" | md5sum -b)
> Finally, you should get in the habit of NOT using all-caps variable
> names for regular shell variables.  The all-caps namespace is "reserved"
> for environment variables (like HOME and PATH) and special shell variables
> Ordinary variables that you use in a script should contain lowercase
> letters.  Mixed caps/lowercase is fine, if you swing that way.

Daniel Qian
Apache Committer(chanjarster)
segmentfault: https://segmentfault.com/u/chanjarster

reply via email to

[Prev in Thread] Current Thread [Next in Thread]