0

I'm working on a vpn script I've forked and I want to add a dialogue option so that it takes user input separated by space, and then echo the input with some nested settings text into the vpn server.conf file.

I'm not a programmer, but I really like bash and this is driving me nuts.

The script part

They're the same.

https://paste.ee/r/FKRnv

http://pastebin.com/raw/U0tSNaCP

The bottom two lines are just for echoing the result while testing.

Now, the issue I'm having is this part:

echo 'push "dhcp-option DNS $(echo $i)"'  >> ./server.conf;

I've also tried the following and its variants with moving quotes around:

echo 'push "dhcp-option DNS $i"'  >> ./server.conf;

But that doesn't work as it echoes $i as text. No matter how I try to nest the $i variable, bash only echoes it as text, instead of user's input.

Examples of openvpn's configuration:

openvpn.net/index.php/open-source/documentation/howto.html#examples

;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"

So in the end, all I would like is a result like this:

push "dhcp-option DNS 1.2.3.4"
push "dhcp-option DNS 5.6.7.8"

where 1.2.3.4 and 5.6.7.8 and so on are user input. However, the quotes around the dhcp-option make nesting difficult.

Anyone got a better way to do this?

I've tried many and searched for nesting before asking this.

Thank you.

Community
  • 1
  • 1
  • "nesting" is the wrong search search term -- there's nothing nested about this; quotes end and are replaced by a different quoting style -- absent command substitution (which isn't relevant here), they don't nest. "bash echo variable inside single quotes", by contrast, comes up with numerous on-point examples. – Charles Duffy Feb 28 '16 at 15:25
  • Possible duplicate of [Difference between single and double quotes in bash](http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Benjamin W. Feb 28 '16 at 20:52

1 Answers1

2

Single quotes inhibit variable expansion and command substitution. With few exceptions, characters within single quotes are just plain characters.

However, you can just concatenate strings together:

echo 'push "dhcp-option DNS '"$i"'"' >> ./server.conf
# ..........................ab..bccc
# a => close the opening single quote
# b => quote the expansion of $i
# c => the closing literal double quote
glenn jackman
  • 238,783
  • 38
  • 220
  • 352