BASH Base64 编码脚本编码不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12290484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH Base64 Encode script not encoding right
提问by jfreak53
I am trying to get a base64 encode to work and output to a variable in a bash script. The regular cli syntax is:
我试图让 base64 编码工作并输出到 bash 脚本中的变量。常规的 cli 语法是:
echo -ne "auth=$(echo -ne "AG15dXNlckBteWhvc3QuY29tAG15cGFzcw==
$user@$hostLW5lIAo=
$pass" | base64);
[email protected]#!/bin/bash
user=myuser
pass=mypass
host=myhost.com
auth=$(echo -ne "[51][00:33:22] vlazarenko@alluminium (~/tests) > echo -ne "user=myuser
pass=mypass
host=myhost.com
auth=$(printf "##代码##%s@%s##代码##%s" "$user" "$host" "$pass" | base64)
[email protected]##代码##mypass" | base64
AG15dXNlckBteWhvc3QuY29tAG15cGFzcw==
[52][00:33:42] vlazarenko@alluminium (~/tests) > bash base64.sh
AG15dXNlckBteWhvc3QuY29tAG15cGFzcw==
[53][00:33:46] vlazarenko@alluminium (~/tests) > dash base64.sh
LW5lIAo=
$user@$host##代码##$pass" | base64);
echo $auth
mypass" | base64
But when I try putting this into a variable in a script it outputs, but a very small encoding, so I know it's not working. My code in the script is:
但是当我尝试将它放入脚本中的变量时,它会输出一个非常小的编码,所以我知道它不起作用。我在脚本中的代码是:
##代码##I know it has something to do with the quotes, but I've tried a myriad of thing's with different quotes and singles and backslashes with no go.
我知道这与引号有关,但我尝试了无数带有不同引号、单引号和反斜杠的方法,但没有成功。
Any thoughts?
有什么想法吗?
EDIT: A bit more for the info. This should output with the user/pass/host above:
编辑:更多信息。这应该与上面的 user/pass/host 一起输出:
##代码##But in the script it outputs:
但在脚本中它输出:
##代码##回答by favoretti
Ok, I'll add this as an answer for the records's sake:
好的,为了记录,我将其添加为答案:
Problem was in having /bin/shas a default interpreter shell, which I assume, in this case was dash.
问题在于有/bin/sh一个默认的解释器 shell,我假设在这种情况下是dash.
Test script used:
使用的测试脚本:
##代码##Results:
结果:
##代码##回答by Gordon Davisson
Different versions of echobehave very differently when you give them anything other than a plain string. Some interpret command options (like -ne), while some just print them as output; some interpret escape sequences in the string (even if not given a -eoption), some don't.
echo当你给它们提供除普通字符串以外的任何东西时,不同版本的行为非常不同。一些解释命令选项(如-ne),而另一些只是将它们打印为输出;有些解释字符串中的转义序列(即使没有给出-e选项),有些则不解释。
If you want consistent behavior, use printfinstead:
如果您想要一致的行为,请printf改用:
As a bonus, since the password (and username and host) are in plain strings rather than the format string, it won't make a mess trying to interpret escape sequences in them (does your real password have a backslash in it?)
作为奖励,由于密码(以及用户名和主机)是纯字符串而不是格式字符串,因此尝试解释其中的转义序列不会造成混乱(您的真实密码中是否有反斜杠?)

