#Is this proper syntax for getting a environment variable value with fallback if it doesnt exist?
1 messages · Page 1 of 1 (latest)
Why don't you post whatever you had in #general to here? there's no context, and no way to backtrace it for others to refer to it in the future by having this thread beginning with just that.
I mean it is related to what I asked in general, but the question itself could be considered a separate question itself no?
It's related to wireguard in which there's no context whatsoever. To you it is related but considered separate, but to others reading it, they have no idea how you came to that.
Added a message link to the start of my questions instead of having copy all messages over here
But I get what you're saying
You mentioned wireguard script being "hardcoded". I am not convinced that is the correct case. It's not a source script that needs to be compiled, into binary. It is doable to just edit the script manually, rather than writing a completely separate script just to handle whatever that "hardcoded" script did.
Basically, a wireguard setup script im using is using appending a bunch of text to a dynamically generated .conf file
One of the lines being added to said file is "ListenPort = 51820"
I just need to substitute every instance of 51820 with a value from a global environment variable if it exists, and if it doesnt, use a fallback value which comes back to my question in the op, if i could do
"ListenPort = (echo ${WGPORT:-51820})" to have the behavior i want it to have
Hopefully that makes sense, tell me if it doesnt, and i will try to explain it better
WGPORT here is a global environment variable i have defined
So the setup script should be the one you need to edit, not create a separate script.
I also do not believe $WGPORT is an environment variable, no script defined variables would exist globally unless it was explicitly set as such during runtime.
This is basically a XY problem.
Hmm.. in /etc/enviroment i have added the line export WGPORT="22963"
shouldnt any bash script be able and fetch that by doing $WGPORT somewhere (i have also done source /etc/environment)
This is pointless… 🤦♂️ so you're creating yourself more problems to solve rather than necessary. If your separate script has issues locating the environment variables, which it shouldn't, then why have it defined elsewhere? You could just have it as part of the script, statically defined.
It also still doesn't explain why all this need for separate script, it's not an elegant method to be resorting measures like this.
Hmm I guess what i want is to be able instead of manually going into each of my scripts (now its only one, but in the future it will be more) and update specific local variables with new values, I instead make sure all these scripts gets their value automatically from my environment variable, and if i need to make changes I just update my environment variable
What is the issue with that?
and wireguard setup script doesn't define that? like you could just source it from the wireguard setup script.
Again, I don't understand what is with this "blackboxing" wireguard setup script. You could have just backed up the original setup script, and hack away to do your bidding instead of all this.
At worst, recreate wireguard setup script so that it matches whatever your needs, rather than manually having to run wireguard setup script then run this fancy script, thus duplicating effort than needed.
It's also unclear what you are doing with this at this point.
If you needed to change the default ports, you just edit the original or whatever.
If you needed to learn shell scripting, which it is pretty evident here, then you should state that, rather than going the roundabout way.
I'd say what im doing right now is me attempting to learn the syntax for shell scripting, and from what it seems I have simultaneously managed to do what i wanted, which was basically upon startup of the wireguard service, use a predefined port that is not the default one
Dont know what else to say, I appreciate your input though
iptables has save and restore function. You can simply dump whatever working config, into a file, and whenever you need to set it up again, you just restore it with that file, wherever you saved it to. It's much simpler than learning shell script for it, more elegant as well.
The next best thing is to edit the wireguard setup script to have it set as the port(s) you want.
hmm me learning shell script as a side effect of trying to achieve what i want is not really an issue, its probably a good skill to have also but yeah i get what you're saying, there is probably (and most likely) better ways to go about this
Then I would enable verbose output, https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html
Alternatively you can simply append echo ${cmd} … which will show you the resultant effect without literally doing it. In fact, backup your iptables config before you run it, just to be extra safe.
and what you're doing with $WGPORT is parameter expansion, https://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion have a good read. Greg's wiki is fairly concise about matters to do with bash scripting.
Alright, seems like a good read for sure