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 :/)
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
Related
I want to send the path to a bash script which sources some environmental variables as an argument to another bash script to run it and use the environmental variables. It works well with no arguments if I hard coded the path to the bash script to run it works and I can retrieve the environmental variables in the main script. the problem happens when I send the path as an argument it does not want to run it.
for example if the path is /path/script.bash and I send the path as an argument I get the error that /path/env_set: No such a file or directory
I run the script by this line
. $1 (this doesn't work)
. /path/script.bash (this works)
if I use
bash -c $1
the bash file runs but it does not set the environmental variables to use it in the main script
I don't know why env_set replaces the script name when I use arguments. Is there any approach to achieve this or any work around to achieve my goal?
It sounds like the problem could be either with your quoting, or with relative paths.
Quoting isn't just about spaces, it's also about pathname expansion (ie. []?* characters).
Do
. "$1"
(not . $1)
And remember, if you're giving a relative path for the environment script (or that script uses some relative paths), you will have a problem. Those paths are relative to the pwd - which is wherever you happen to be when you execute the main script (not where any of the script files themselves happen to be located, for example).
Finally, you can debug this problem by throwing echo at the start, and running the script (if it's safe to do that):
echo . "$1"
exit # Add exit here if you don't want to run w/o the vars.
Now you can see what you're actually trying to source.
In script 1, in your main code, you can call and run script 2,
. ./script 2
The first . stands for current shell, and the second . for current directory.
which will create the environment variables for you, and configure any other settings as well in the same terminal.
Afterwards when script 2 has finished running, script 1 would continue to run, and your environment variables which was created in script 2, will be accessible for script 1 to use in the same session.
I have a bash script that looks like the below. When I run it in the terminal, it just leaves a blank space. I want to be able to CD into this different location to get to the file I need OR ALTERNATIVELY is there a way of getting a file from a different location?
#!/bin/bash
# My first script
alias location="cd C:/Users/A591024/AppData/Local/Temp/TD_80/hq**/1212*1212/R*****"
do I maybe need to say something like "run location" underneath?
the final goal is to be able to get to a file inside the R****** folder and open up that file inside the window and try and grep from that..
also this is being done inside windows command line not linux
If you run this script, then Linux will create a new shell for you. The new shell will execute the alias command. Since there are no more commands after that, the new shell will terminate.
Since the alias command is executed in a new shell (or subshell), your current shell isn't modified. Hence, after the script ends, you won't notice any difference.
To make the current shell execute the command, use source or .:
source location.sh
. location.sh
Note that this . is a command and shouldn't be confused with the . folder which is an alias for the current folder.
I'm trying to make a configuration file to help running a bash script.
The idea is having a file (script.conf) like this
directory=c:/path/to/a/specific/directory/
logo=y
title=y
hotspots=n
combobox=n
Then, running the script, it will read script.conf and get those variables to use in the script.
How can I do that?
The source command (also known as ., but not to be confused with the directory of the same name) will allow you to run another file in the current shell. Simply make that file contain variable assignments.
foo.sh
#!/bin/bash
. bar.sh
echo "$baz"
bar.sh
baz=42
I used 'change directory' in my shell script (bash)
#!/bin/bash
alias mycd='cd some_place'
mycd
pwd
pwd prints some_place correctly, but after the script finished my current working directory doesn't change.
Is it possible to change my path by script?
You need to source the file as:
. myfile.sh
or
source myfile.sh
Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.
While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.
Another way to do, is to implement your script as a function in bash.
function cdbm() {
cd whereever_you_want_to_go
echo arguments to the functions were $1, $2, ...
}
This technique is used by autojump:
http://github.com/joelthelion/autojump/wiki
to provide you with learning shell directory bookmarks.
The script is run in a separate subshell. That subshell changes directory, not the shell you run it in. A possible solution is to source the script instead of running it:
# Bash
source yourscript.sh
# or POSIX sh
. yourscript.sh
It can be achieved by sourcing. Sourcing is basically execute the script in the same shell whereas normal execution(sh test.sh or ./test.sh) will create sub shell and execute script there.
test.sh
cd development/
ls
# Do whatever you want.
Execute test.sh by
source test.sh
. is shortest notation for source. So you can also do by
. test.sh
This will execute the script and change the directory of current shell to development/.
whenever you run a script on your login shell, a new subprocess is spawned and the script execution is done in a subshell.Once the script completes, the subshell exits and you are returned to the login shell.Hence whenever you do a cd through a script,the directory is changed to the path specified by cd, but by the time script finishes you come back to your login shell to the working directory from where you started the script.
The way to overcome this is use,
source yourscript.sh
what source does is it executes the script as TCL script, i.e it has the same effect as when you typed each line on the command line of your login shell and it executed from there. So this way when the script finishes after cd , it stays in that directory.
Another practical solution is to end your script by opening another shell session.
For instance:
#!/bin/bash
cd some_place
bash
This is useful, in my case, for scripts located in my ~/bin for instance, called from any other place. It is just a bit painful to type source ~/bin/mygoodoldscript instead of mygoo<TAB><ENTER>.
The downside is that the additional shell takes up a few more resources (not much).
Though there are answers. I think the intention of question is to use script to navigate to specific path.
Here is a simple practical solution works here without cancel out existing terminal environment flag.
provide a bash/tch/sh script to work for path generation
/* .goto.sh */
#!/usr/bin/env bash
echo '~/workspace'
add alias to the script output
alias goto 'cd `.goto.sh`'
I have some proxy settings that I only occasionally want to turn on, so I don't want to put them in my ~/.bash_profile. I tried putting them directly in ~/bin/set_proxy_env.sh, adding ~/bin to my PATH, and chmod +xing the script but though the script runs, the variables don't stick in my shell. Does anyone know how to get them to stick around for the rest of the shell session?
Use one of:
source <file>
. <file>
In the script use
export varname=value
and also execute the script with:
source set_proxy_env.sh.
The export keyword ensures the variable is marked for automatic inclusion in the environment of subsequently executed commands. Using source to execute a script starts it with the present shell instead of launching a temporary one for the script.
Did you try this:
. ~/bin/set_proxy_env.sh
Running it by itself opens a separate subshell (I think) and sets the variable there. But then the binding is lost after exiting back into your shell. The dot at the front tells it to run it within the same shell.
Also, don't forget to export the variables you need like so: export MYVAR=value