Removing leading zeros before passing a shell variable to another command

前端 未结 14 1309
长情又很酷
长情又很酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    

提交回复
热议问题