Pergunta

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 :/)

Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top