Removing leading zeros before passing a shell variable to another command

前端 未结 14 1312
长情又很酷
长情又很酷 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

    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

提交回复
热议问题