【问题标题】:shell - The code is correct, but it does not workshell - 代码是正确的,但它不起作用
【发布时间】:2017-02-26 12:58:57
【问题描述】:

一切正常。但它给出了一个错误。我解决不了。

try.sh

#!/bin/sh

website="http://lastofdead.xyz"
ipaddress=$( ifconfig | grep -v 'eth0:' | grep -A 1 'eth0' | \
             tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)
mlicense=$(curl -s $website/lodscript/special/lisans.php?lisans)

if [ "$mlicense" = "$ipaddress" ];
then 
    echo "Positive"
else
    echo "Negative"
fi

lisans.php

<?php 
if(isset($_GET['lisans'])) {
echo "188.166.92.168" . $_GET['lisans'];
}
?>

结果:

root@ubuntu:~# bash -x s.sh
+ website=http://lastofdead.xyz
++ cut -d ' ' -f 1
++ cut -d : -f 2
++ tail -1
++ grep -A 1 eth0
++ grep -v eth0:
++ ifconfig
+ ipaddress=188.166.92.168
++ curl -s 'http://lastofdead.xyz/lodscript/special/lisans.php?lisans'
+ mlicense=188.166.92.168
+ '[' 188.166.92.168 = 188.166.92.168 ']'
+ echo Negative
Negative

https://i.stack.imgur.com/jNMNq.jpg

【问题讨论】:

  • 也许尝试在您的条件下使用 -eq 运算符更改单个 = 并删除 " 字符?
  • @R.CanserYanbakan 我试过了,但是没用。
  • @R.CanserYanbakan 这些建议都不正确。
  • 我个人会确认ip地址周围是否有空格
  • 可能不相关,但if [ "$mlicense" = "$ipaddress" ]; 将始终为真,分号不是必需的。

标签: php linux bash shell


【解决方案1】:

哦,非常感谢您发布代码。 好吧,在尝试查看两个字符串之间的差异后,结果发现问题出在Byte Order Mark (BOM) 上。有关这方面的更多信息,请参阅此答案:https://stackoverflow.com/a/3256014

curl 返回的字符串在将其传送到十六进制转储时显示如下:

$ curl -s 'http://lastofdead.xyz/lodscript/special/lisans.php?lisans'
188.166.92.168
$ curl -s 'http://lastofdead.xyz/lodscript/special/lisans.php?lisans' | xxd -c 17 -g 1 -u
0000000: EF BB BF 31 38 38 2E 31 36 36 2E 39 32 2E 31 36 38  ...188.166.92.168

你能看到他们吗?这三个字节,0xEF,0xBB,0xBF,是 BOM 的 UTF-8 表示,这就是字符串不同的原因。上面链接的问题页面显示了一些去除它的方法,例如使用grep,或者您可以将 curl 输出通过管道传输到cut -c 2-,或者甚至通过在curl 行之后进行简单替换:mlicense="${mlicense:1}"。而且,为了确保我们剥离的是 BOM,我们可以使用两行替换:bom="$(echo -en '\xEF\xBB\xBF')"; mlicense="$(echo -n "${mlicense#${bom}}")",甚至将它们变成一行:mlicense="$(echo -n "${mlicense#$(echo -en '\xEF\xBB\xBF')}")"

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2010-12-21
    • 2022-07-08
    • 2020-03-27
    • 1970-01-01
    • 2021-10-17
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多