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

shell for 循环问题

  •  
  •   DongDongXie · Aug 1, 2018 · 3190 views
    This topic created in 3048 days ago, the information mentioned may be changed or developed.
    trainDates='2018-07-25 2018-07-26 2018-07-26'
    for dat in $trainDates
    do
        echo "$dat"
        temp=$(echo "$trainDates"|awk -F ' ' '{print $1}')
        echo "$temp"
        if [ $dat==$temp ]; then echo "2"; fi
    done
    

    刚入手 shell,请问为什么会这样输出呢,跟我预想的不一样啊:

    2018-07-25
    2018-07-25
    2
    2018-07-26
    2018-07-25
    2
    2018-07-26
    2018-07-25
    2
    
    7 replies  •  2018-08-01 16:50:26 +08:00
    raynor2011
    raynor2011
    Aug 1, 2018
    1
    == 两边加空格
    omph
    omph
    Aug 1, 2018
    2
    [ $dat == $temp ]
    zyp0921
    zyp0921
    Aug 1, 2018
    3
    最好再加上双引号
    zyp0921
    zyp0921
    Aug 1, 2018
    4
    用=就可以了,==是判断数字的。=是判断字符串的
    DongDongXie
    DongDongXie
    OP
    Aug 1, 2018
    6
    @raynor2011 谢谢,加空格之后就可以啦
    DongDongXie
    DongDongXie
    OP
    Aug 1, 2018
    7
    @zyp0921 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1128 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:16 · PVG 02:16 · LAX 10:16 · JFK 13:16
    ♥ Do have faith in what you're doing.