3

Is it possible to split a config command over multiple lines instead of one long string?

E.g.

PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

When using more commands the line gets very long and unreadable. I would like to split it so that I can use one line per command. Something like:

PostUp = iptables -A FORWARD -i %i -j ACCEPT; \
   iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

The man page says it's based on the ini format but has no help on this topic.

4
  • 1
    try it and see. did it work? you have your answer. did it not work? you also have your answer.
    – cas
    May 31, 2021 at 4:17
  • @cas your answer does not make sense. My example was to show what I am looking for. If I knew the correct format I would not have asked.
    – laktak
    May 31, 2021 at 9:08
  • @A.B are you implying it works for you? It's a made up example. No it does not work for me.
    – laktak
    May 31, 2021 at 9:12
  • @A.B I really thought that was clear. I'll keep it in mind next time.
    – laktak
    May 31, 2021 at 21:58

1 Answer 1

4

The wg-quick command is a bash script. The loop reading keys (such as the PostUp key) is using read -r which prevents any use of \ to separate lines. But reading this same script shows:

POST_UP=( )

to declare it as an array, and:

          PostUp) POST_UP+=( "$value" ); continue ;;

to add an element to the array.

It's then executed later with (there's an eval command in the function execute_hooks):

execute_hooks "${POST_UP[@]}"

Thus one can split multiple commands (but not arbitrary lines) by using multiple times the same key entry. So OP's example would work like this:

PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Actually the man page already tells it:

• PreUp, PostUp, PreDown, PostDown — script snippets which will be executed by bash(1) before/after setting up/tearing down the interface, most commonly used to configure custom DNS options or firewall rules. The special string `%i' is expanded to INTERFACE. Each one may be specified multiple times, in which case the commands are executed in order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.