Removing leading zeros before passing a shell variable to another command

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

    If you don't have sed or awk or perl then you could use cut and grep

    mn="$machinenumber"
    while echo "$mn"|grep -q '^0';do mn=$(echo "$mn"|cut -c2-);done
    iptables -t nat -I POSTROUTING -s 10.($machinetype).($mn).0/24 -j MASQUERADE
    

    Works with both bash and dash shells.

提交回复
热议问题