php - shell - 代码是正确的,但不起作用

标签 php linux bash shell

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

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

/image/jNMNq.jpg

最佳答案

哦,非常感谢您发布代码。 好吧,在尝试查看两个字符串之间的差异后,发现问题出在名为 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')}")"

关于php - shell - 代码是正确的,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42468828/

相关文章:

php - Doctrine2...最佳补水模式?

php - 如何仅在满足条件时才开始 Activity

linux - 在 MATLAB 中执行 .mex 文件时,如何诊断为什么无法加载 .so 文件?

linux - 统计文件的脚本 (Bash)

bash,文本文件在最后一个空格之前删除每行中的所有文本

c++ - 在 C++ 中将 .docx 转换为 .txt

php - 如何使用 zend from 在 zend Framework 2 中添加自定义属性

php - 基于 Woocommerce 中尺寸自定义字段的自定义购物车商品价格计算

linux - 编译时如何使用特定的库?

linux - 使用 pwm-ir-tx 模块时没有可用的用户空间 chardev