Running bash commands written in a file [duplicate] - linux

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Why doesn't a shell get variables exported by a script run in a subshell?
(2 answers)
Closed 5 years ago.
I have a file which contains the following commands,
export PATH=/gpfs/home/spandrekar/cse504/build_llvm/bin:$PATH
export LIBRARY_PATH=/gpfs/home/spandrekar/cse504/build_llvm/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/gpfs/home/spandrekar/cse504/build_llvm/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/gpfs/home/spandrekar/cse504/build_llvm/projects/openmp/runtime/src:$C_INCLUDE_PATH
export CC=clang
export CXX=clang++
I need to run these everytime I login to start my project. To make things easier, I am trying to run them using the command,
bash filename
However, this doesn't seem to work. When I type these commands on the commandline, it all works fine.
What am I missing here? How can I run the commands written in the file.

Related

Unable to copy files using Shell Script [duplicate]

This question already has answers here:
How to assign a glob expression to a variable in a Bash script?
(8 answers)
Wildcard within quotations
(3 answers)
Closed 8 months ago.
I am unable to copy files using Shell script. I validated in shellcheck.net no issues found in syntax.
It showing error as file not fount but I am using exact file name.
Below is my Shell script.
#! /bin/bash
export SOURCE_PATH="/home/la1122/export"
export SOURCE_TARGET="/home/la1122/export/backup"
FILE="*.dat"
cd $SOURCE_PATH
echo "Current location: $(pwd)"
cp "$FILE" "$SOURCE_TARGET"

Bash Script: `cd` with command substitution [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Change the current directory from a Bash script
(17 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
Summary:
I am trying to have a script that allows easy one-touch access to files that are generated by the crontab.
The hourly generated files go by the path as: /home/mouse/20210126/0900.
Of course as the date and time changes, the path will change like so:
/home/mouse/20210126/1000
/home/mouse/20210126/1100
/home/mouse/20210127/1000
/home/mouse/20210128/1300
I tried using an alias, but once .bashrc is loaded, it only takes the present date/time, which means that I cannot "refresh" the date and time.
So by using a script, I could update the date and time like so:
currentdate=$(date +%Y%m%d)
currenttime=$(date -d '1 hour ago' "+%H00")
collect=cd /home/mouse/$currentdate/$currenttime
echo $currentdate
echo $currenttime
$collect
However, I realized that the cd script is not working because, from my understanding, it works on a subshell and once the script has finished executing, it does not affect the main shell.
I tried source / . but I get /home/mouse/20210126/1000 is a directory.
What is the recommended action I should take to resolve this?

Setting PATH environment variable in .bashrc does not work? [duplicate]

This question already has answers here:
what's the difference between ./script.sh and bash script.sh
(5 answers)
Difference between ./ and ~/
(6 answers)
Closed 3 years ago.
I downloaded IntelliJ IDEA for my linux system. It says to add the location of the bin folder of IntelliJ to the PATH environment variable in order to run IntelliJ from any folder.
So, I opened .bashrc and put in the address -
'export PATH=$PATH:/Documents/IntelliJ/bin' and then 'source ~/.bashrc'.
Then, to check I echoed by PATH and it showed the changes.
But, when I try to run './idea.sh' from some other directory it does not work. It says file not found. I have no idea what's hapenning?

Bash script output not showing up during execution? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).

How can I set a "global" bash variable (cache the variable in parent shell)? [duplicate]

This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
Is there a way to change the environment variables of another process in Unix?
(12 answers)
Closed 5 years ago.
I have a script that has:
global_npm_modules_path="$(npm root -g)";
this npm command is slow, and I am looking to speed up this script. One thing I could do is "cache" the result of the npm command, something like this:
export global_npm_modules_path=${global_npm_modules_path:-"$(npm root -g)"}
but my question is - how can I set global_npm_modules_path so that it's held in store by the parent shell / parent process?
The problem is the export command will only be relevant for this shell and its children.

Resources