Removing leading zeros before passing a shell variable to another command

前端 未结 14 1066
长情又很酷
长情又很酷 2020-12-08 18:52

It turns out that iptables doesn\'t handle leading zeros too well. As $machinenumber that is used has to have a leading zero in it for other purposes, the idea

相关标签:
14条回答
  • 2020-12-08 19:20

    Add the number with 0, it will remove leading zeroes

    eg: expr 00010 + 0 #becomes 10
    

    OR

    num=00076
    num=${num##+(0)}
    echo $num
    
    0 讨论(0)
  • 2020-12-08 19:20

    I had to revisit this code the other day due to some unrelated stuff, and due to compatibility with some other software that reads the same script, i found it a lot easiest to rewrite it into this, which should still be valid bash:

    iptables -t nat -I POSTROUTING -s 10.($machinetype).($machinenumber + 0).0/24 -j MASQUERADE

    Basically, adding 0 forces it to be interpreted as an integer, hence automatically stripping the leading zeros

    0 讨论(0)
  • 2020-12-08 19:24

    No, you make all (alomost all) correct. You just must:

    • remove spaces around =
    • use $() or backticks instead of ()

    That would be correct:

     nozero=$(echo $machinenumber | sed 's/^0*//')
    

    Also you must use variables without () around them. You can add "" if you want:

    iptables -t nat -I POSTROUTING -s "10.$machinetype.$nozero.0/24" -j MASQUERADE
    

    And of course variables here are not necessary. You can say simply:

    iptables -t nat -I POSTROUTING -s "10.$(echo $machinenumber | sed 's/^0*//').$nozero.0/24" -j MASQUERADE
    
    0 讨论(0)
  • 2020-12-08 19:26
    nozero=$(echo $machinenumber | sed 's/^0*//')
    

    Try without the spaces around = and with an additional $ sign.

    0 讨论(0)
  • 2020-12-08 19:31

    I can't comment as I don't have sufficient reputation, but I would suggest you accept Dennis's answer (which is really quite neat)

    Firstly, I don't think that your answer is valid bash. In my install I get:

    > machinetype=74
    > machinenumber=05
    > iptables -t nat -I POSTROUTING -s 10.($machinetype).($machinenumber + 0).0/24 -j MASQUERADE
    -bash: syntax error near unexpected token `('
    > echo 10.($machinetype).($machinenumber + 0).0/24
    -bash: syntax error near unexpected token `('
    

    If I quote it I get:

    > echo "($machinetype).($machinenumber + 0)"
    (74).(05 + 0)
    

    I'm assuming you mean:

    > echo 10.$(($machinetype)).$(($machinenumber + 0)).0/24
    10.74.5.0/24
    

    But, of course it's still a bad solution because of octal:

    > machinenumber=09
    > echo 10.$(($machinetype)).$(($machinenumber + 0)).0/24
    -bash: 09: value too great for base (error token is "09")
    

    I assume that your numbers aren't 08 or 09 at the moment.

    Here's Dennis's:

    > echo $((10#09))
    9
    > echo $((10#00))
    0
    > echo $((10#00005))
    5
    > echo $((10#))
    0
    

    Admittedly, that last one might be an input validation problem for someone.

    The sed solution has the problem of:

    > echo "0" | sed 's/^0*//'
    
    >
    
    0 讨论(0)
  • 2020-12-08 19:32

    I do it by using

    awk '{print $1 + 0}'
    

    I like this better than the sed approach as it still works with numbers like 0, 000, and 001.

    So in your example I would replace

    nozero=$(echo $machinenumber | sed 's/^0*//')
    

    with

    nozero=$(echo $machinenumber | awk '{print $1 + 0}' )
    

    0 讨论(0)
提交回复
热议问题