What's new

How do I force a variable to process as variable and not string in shell script

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Deplorable

Occasional Visitor
I am really really really hard pressed to keep this post from being laced with expletive after expletive after expletive I have been at it so long with no success and nothing useful on Google.

pc_insert "bind-address-ipv6" ' "bind-address-ipv4": "$VPNADDR", ' $CONFIG

All I want to do is use this function to replace a line in a config file with the current VPN address. I can't even get to that part because the $VPNADDR WILL NOT process as a variable. It takes it literally and outputs the var name. If I remove the outside quotes and make it instead

pc_insert "bind-address-ipv6" " "bind-address-ipv4": "$VPNADDR", " $CONFIG

It processes the variable but removes crucial items like commas and other double quotes. It won't do both, one or the other. Is shell scripting really this *@&#&@ useless?? Please help, this is so infuriating I'm on the verge of going and stomping on the router I'm working on which would be expensive.
 
Last edited:
From the wiki:
pc_replace "original string" "new string" "config filename"
pc_insert "string to locate" "string to insert after" "config filename"
pc_append "string to append" "config filename"
pc_delete "string within line to delete" "config filename"
So, with insert you look for a string in dnsmasq.conf and when found to insert after it.
If I had "no-poll" in dnsmasq.conf and I want to insert after it I would do this:
Code:
pc_insert "no-poll" "bind-address-ipv4: $VPNADDR," $CONFIG
Which correctly gives this, $VPNADDRESS is defined as holla here for testing purposes:
Code:
no-poll
bind-address-ipv4: holla,

What you are after is to replace a line, so you'd have to use the replace command.
Can you post the original line and what it should look like after?
 
File is /opt/etc/transmission/settings.json with line

Code:
"bind-address-ipv4": "0.0.0.0",

Line should be same but with my VPN IP address so let's say it becomes


Code:
"bind-address-ipv4": "173.239.210.10",

I can't just replace the 0.0.0.0 as once it changes I can no longer search based on 0.0.0.0 and have to pull the IP address from the json then replace it somehow that way. That is why I was trying to just delete the line and replace it.
 
How about using sed to replace it instead?
Code:
sed -i "/bind-address-ipv4/c\\\"bind-address-ipv4\": \"$VPNADDR\"," /opt/etc/transmission/settings.json
First "bind-address-ipv4" finds this string in the file and replaces it with the second part. Make sure $VPNADDR is defined before it runs.
 
How about using sed to replace it instead?
Code:
sed -i "/bind-address-ipv4/c\\\"bind-address-ipv4\": \"$VPNADDR\"," /opt/etc/transmission/settings.json
First "bind-address-ipv4" finds this string in the file and replaces it with the second part. Make sure $VPNADDR is defined before it runs.

I did some testing and that replaces the line perfectly so should be gtg. Need to work out a way to pull current VPN address now.
 
BTW: The posted sed one-liner does not need the helper file included, so if this is all you want, only put this in the script.
... And no expletives were used to find a solution...
 
I did some testing and that replaces the line perfectly so should be gtg. Need to work out a way to pull current VPN address now.
Is it stored in NVRAM?
Post the line containing it.
 
BTW: The posted sed one-liner does not need the helper file included, so if this is all you want, only put this in the script.
... And no expletives were used to find a solution...

I said a few before you showed up so you're covered. I already commented everything out except the line where I was filling the var and then the sed line you posted. Thanks
 
Is it stored in NVRAM?
Post the line containing it.

VPNADDR=`ifconfig | grep -A 5 "tun" | grep "inet" | cut -f2 -d" "`

if [ -z "$VPNADDR" ]; then
VPNADDR=127.0.0.1
fi

I had not really gotten further than grabbing someone's code they posted for something similar they were trying to do. I never had the chance to go through and see if it actually works or rather why it doesn't work. Was side tracked on the actual replacement line you gave me the sed code for.
 
I had not really gotten further than grabbing someone's code they posted for something similar they were trying to do. I never had the chance to go through and see if it actually works or rather why it doesn't work. Was side tracked on the actual replacement line you gave me the sed code for.
See if it is in NVRAM.
Enter this and then open the file /jffs/nvram.txt to search for your current VPN address and post the line containing it:
Code:
nvram show | sort > /jffs/nvram.txt
 
See if it is in NVRAM.
Enter this and then open the file /jffs/nvram.txt to search for your current VPN address and post the line containing it:
Code:
nvram show | sort > /jffs/nvram.txt

I updated to 68_2 tonight so murphy's law the pos vpn isn't working so may have to redo it. I'll try but the interface is unresponsive at the moment under that tab.
 
If it is in NVRAM, you can use this one-liner to define VPNADDR. If the nvram value "nvram_variable" is empty, 127.0.0.1 is used, else the actual value.
Replace both instances of "nvram_variable" with the actual name of it.
Code:
[[ -z "$(nvram get nvram_variable)" ]] && VPNADDR=127.0.0.1 || VPNADDR=$(nvram get nvram_variable)
Place this before the sed one-liner in your script.
 
Last edited:
If it is in NVRAM, you can use this one-liner to define VPNADDR. If the nvram value "nvram_variable" is empty, 127.0.0.1 is used, else the actual value.
Replace both instances of "nvram_variable" with the actual name of it.
Code:
[[ -z "$(nvram get nvram_variable)" ]] && VPNADDR=127.0.0.1 || VPNADDR=$(nvram get nvram_variable)
Place this before the sed on-liner in your script.
I'll try that and see what happens. In the interim I got VPN connected after redoing settings and I see this in the nvram.txt you had me run.
vpn_client2_rip so likely that is what I want, it's the external IP for the VPN. The other is the internal network IP.
 
If it is in NVRAM, you can use this one-liner to define VPNADDR. If the nvram value "nvram_variable" is empty, 127.0.0.1 is used, else the actual value.
Replace both instances of "nvram_variable" with the actual name of it.
Code:
[[ -z "$(nvram get nvram_variable)" ]] && VPNADDR=127.0.0.1 || VPNADDR=$(nvram get nvram_variable)
Place this before the sed on-liner in your script.

I was able to update settings.json with the appropriate listening address so I should be good to go from that standpoint. Just have to get the port_forwarding.sh going and I will be golden. Thanks for all the help!
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top