Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
imkh
V2EX  ›  Linux

shell 脚本一个神奇的字符比较问题

  •  
  •   imkh · Apr 5, 2016 · 3539 views
    This topic created in 3868 days ago, the information mentioned may be changed or developed.
    # cat compare.sh 
    #!/bin/bash
    
    status=$(/tmp/vpn/vpn1.exp | grep '10.31.90.101'|awk '{print $NF}'|uniq)
    echo "$status"
    if [[ "$status" == "A" ]];then 
      echo 'A'
    else 
      echo 'not A'
    fi
    
    # /tmp/vpn/vpn1.exp | grep '10.31.90.101'|awk '{print $NF}'|uniq
    A
    # ./compare.sh 
    A
    not A
    

    为什么 /tmp/vpn/vpn1.exp | grep '10.31.90.101'|awk '{print $NF}'|uniq 输出是个 A , 但在 if [[ "$status" == "A" ]]比较确失败了?代码直接执行了 else 部分

    6 replies  •  2016-04-05 22:29:15 +08:00
    curiouslfq
    curiouslfq
    Apr 5, 2016❤️1
    1
    [[ "$status" = "A" ]]
    BOYPT
    BOYPT
    Apr 5, 2016❤️1
    2
    你应该用 sh -x ./compare.sh 执行,看看 status 具体是什么,可能是“ A\n ”,换行了呢,或者有其他不可见符号
    curiouslfq
    curiouslfq
    Apr 5, 2016
    3
    确定输出只有一个字符吗
    imkh
    imkh
    OP
    Apr 5, 2016
    4
    @BOYPT 谢谢
    imkh
    imkh
    OP
    Apr 5, 2016
    5
    @curiouslfq 谢谢
    imkh
    imkh
    OP
    Apr 5, 2016
    6
    status=$'A\r' 多了个回车符
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1591 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 12:53 · PVG 20:53 · LAX 04:53 · JFK 07:53
    ♥ Do have faith in what you're doing.