Bash variable substitution when using a command line argument file - bash

FILEDIR=/home/myuserdir/audit
FILE=auditreport.csv
The above variable is in my configuration file.
I have this bash script that runs with a configuration file I have:
for file in `ls ${FILEDIR}/${FILE}`
It does see my path, it is going into the root or / directory instead of going into my /home/myuserdir/audit dir. But if I declare the variable in the script instead of the configuration file, it works perfectly and the right directory is found. What am I doing wrong? Unfortunately, I have to run the script with the configuration file for easy customization going forward.
I have tried to wrap the statement in “”, but it still does not work.
The expected results is I would like for the script to go to /home/myuserdir/audit instead of the home directory.

You must source your configfile, not only start it!
my.config
ConfigFileDir=/home/myuserdir/audit
ConfigFile=auditreport.csv
your script
# source my.config at the top of your script
source my.config
# and use the variables
echo "$ConfigFileDir/$ConfigFile"

Related

scp error when defining a "PATH" variable in a bash script

So this is my script
#!/bin/bash
PATH=/SomeFolder/file2.txt;
scp -3 user#server1:/SomeFolder/file.txt user#server2:$PATH;
I get this error
main.sh: line 3: scp: command not found
If I put /SomeFolder/file2.txt in place of of "$PATH" it still doesn't work - same error. It's only after I remove entire second line (PATH definition) does it work.
I simplified my script, the PATH is defined by executing a script inside another server but that doesn't matter. I tested it like what you see and I concluded that the error is due to PATH being defined in the first place.
It is happening because PATH is a system variable that defines directories where the programs and scripts should be looked for. You can view its value by executing echo $PATH. In your script you are setting PATH to /SomeFolder/file2.txt so the program scp that is usually in /usr/bin/ can't be found. Just change the name of variable PATH in your script to something else.

How to use the CD command in a bash scipt?

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.

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

Running R script from Shell using CygWin: error "Rscript not found"

This is the first time I am trying to run the R file from CygWin terminal.
I have a file named linreg.R and I am in the same directory as file in CygWin terminal.
There is a shell script in the same directory that take in input linreg.R and another data.txt (located at some other place).
When I am running the bash with appropriate inputs, its again and again giving me the same error:
$ ./build_model_from_directory.sh linreg.R /workdir/workdir/prod_data_v.txt lm_try
./build_model_from_directory.sh: line 27: type: Rscript: not found
Rscript is needed for linreg.R. Exiting
When I put something like this:
$./build_model_from_directory.sh linreg.Rscript /workdir/workdir/prod_data_v.txt lm_try
Script assumes linreg.Rscript in same directory
This is the first line of the linreg.R
#!/usr/bin/env Rscript
I have tried setting path to PATH=$PATH:C:\\ProgramFiles\\R\\R-3.0.1\\bin
but of no use. It has changed the PATH but still the script is not running.
Any help will be highly appreciated.
it might be worth your time to add it to your .bashrc file:
echo 'PATH=$PATH:/cygdrive/c/Program\ Files/R/R-3.2.3/bin' >> .bashrc
I figured out the mistake that I was making again and again.
cygpath is used to find out the actual representation of the path of the the directory in UNIX environment
Example:
$ cygpath 'C:\Program Files\R\R-3.0.1\bin'
/cygdrive/c/Program Files/R/R-3.0.1/bin
So we need to make sure that PATH variable has Program Files and not ProgramFiles.
Since UNIX does not understand special characters we need to backscape the space between Program Files
$ PATH=$PATH:/cygdrive/c/Program\ Files/R/R-3.0.1/bin
It started recognizing R files after that.

Bash - get variables from an external file

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

Resources