Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenVPN Source vars not working on debian

I have to create a script to setup an OpenVPN server automatically. In this script I need to source the vars file in /etc/openvpn/easy-rsa/

But when I'm executing the following script in the /etc/openvpn/easy-rsa/ folder (with chmod 775 on the script and vars file) it says "xxxx.sh: 3: xxxx.sh: source: not found:"

#!/bin/bash
source ./vars

When I write . ./vars, it works, but then if I want to do a ./clean-all it says :

Please source the vars script first (i.e. "source ./vars") Make sure you have edited it to reflect your configuration.

When I do the ./clean-all in the same script than the . ./vars, it works.

Thanks for your help (and sorry for my bad english :/)

like image 420
il0venoobs Avatar asked Oct 21 '22 09:10

il0venoobs


1 Answers

When you source (or .) a file, all the commands inside it are read and executed - this includes variable assignments. However, when a variable assignment takes place, it takes place only for the current shell. When you run a script, a subshell is created - so any variables inside the script are only visible within the subshell, not the parent (calling) shell. This is why it works when you have run source and clean-all within the same script, it should also work if you do both from the command line, ie:

$ . /etc/openvpn/easy-rsa/vars
$ /etc/openvpn/easy-rsa/clean-all
like image 73
Josh Jolly Avatar answered Nov 03 '22 00:11

Josh Jolly