How can I run a command in a shell script function that requires access to the current shell instance? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am using a custom package manager called spack, which allows me to load installed modules using the spack load command. It is similar to the familiar module load command in many ways. I am using zsh.
I have set up a shell script with a function that I would later like to insert into my .zshrc file. It is currently located in a standalone file for testing purposes, which looks as following:
#!/bin/bash
load-standard () {
echo "loading $1"
spack load $1
}
load-standard $1
When I run this script with source ./script_name package_name, I get an error message that says
`spack load` requires Spack's shell support.
To enable Spack's shell support, a file called setup-env.sh must be run which enables the user to make use of the spack command.
However, directly typing in the commands spack load package_name works with no problem.
I always assumed that running a command from a shell script is the same as typing it into the current shell. How can I make my shell interpret the spack load commands exactly as if I had directly typed them in?
EDIT: Placing the function in my .zshrc file solved this problem.

I'm not familiar with spack, but likely spack is a shell function which modifies the current shell environment. That is how module works. type spack to check.
You can't modify the shell environment from a script, you can from a shell function.
Copy and paste the function load-standard to "$ZDOTDIR/.zshrc" (for current user, /etc/zshrc for all users), source .zshrc (. "$ZDOTDIR/.zshrc") and you should be fine (no need to restart).
You can also create a list of functions in a file, and add . /path/to/functions to zshrc, to source it.

Related

How to run bash file with one word [duplicate]

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
Issue
I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.
Instead of typing ~/folder/file.sh in the terminal,
I only have to type a_word to run the file.
Is this possible with bash?
And also, this is on RPiOS's terminal, not sure if it differs.
Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).
When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).
You have to define a so called alias.
Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.

Issue with setting environment variables permanently in linux bash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to set up a global environment variable permanently into the shell, so i do not set it every time i open another shell or another log-in session.
I have set the variable using export as following:
$ export pass='my_pass'
However when i use another active shell to restore this variable using echo as following:
$ echo $pass
The variable does not exist, so it only exist in the local shell of setup.
I have tried putting it into the .bash_profile but this also did not work.
~/.bash_profile is only sourced on login (i.e. after you've typed your username & password) - ~/.bashrc is sourced for interactive non-login shells.
So I'd add the variables into ~/.bashrc (don't forget to source it first if you're running the python script afterwards from the same shell). This way, when you open a new shell, bashrc will be sourced and your environment variables will be available.
Edit:
As others have said in comments .. running an export command in one shell, won't make that variable availble in another shell - you need to add it to your ~/.bashrc to make it avaiable in other shells

How to know the default shell process for my terminal? [duplicate]

This question already has answers here:
How to get the default shell
(5 answers)
Closed 6 years ago.
I am composing a bash script to serve as a utility tools.
The challenges I am facing now is:
- user using my tool will be running in bash environment
- however, some of them might default using krcsh or tcsh. they might have aliases or configurations set in there.
So, I need to prompt/guide user to resolve this during installation. My first challenge: How am I suppose to know the user's default shell within my install.sh?
Knowing the "default" shell, I can prompt and guide the user to do necessary transfer to bash.
my testing code:
my result:
1/ is fault obviously. It return the current shell which is my install.sh (bash)
2/ I am doubtful. It seems to be the history of what I have run before. It does not show me my default configured shell. My case, my terminal default shell is bash, and I run tsch for testing purpose. So the script parsed wrong information and will though my default shell is tcsh. It will then assist me to port configurations from tcsh to bash during the installation process.
If you want to check the shell you are using, you can use the following methods:
echo $0 in terminal will show you the program running if you want to check the shell you are currently using.
echo $SHELL - with this command you can read the user's default shell in the terminal you are running.
If you want to prompt, easily you can put the echo $SHELL in the part of your script where you need to show the current shell you are using.
Don't forget to put #!/bin/bash if your script is designed to run in a bash shell!

Execute the shell script that is in the current directory, not the one in $PATH [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have defined the path in .bash_profile file for /usr/share/totalscripts as:
PATH="${PATH}:/usr/share/totalscripts"
export PATH
Where all my shell scripts programs are present - /usr/share/totalscripts
Specifically, I need to execute the scripts in the current directory, not the one available in my $PATH environment variable. That is, executing a specific script rather than any script that matches that is found in the PATH.
For example, it might make sense to not call script1.sh (which will execute against any script1.sh found in the PATH), but instead call ./script1.sh (which will only execute the script that is found in the local directory).
This because I have the same script under /usr/share/totalscripts/script1.sh and /home/script1.sh (same names)
If you are working in a script, then you can define your environment variables at the start of it, like:
MY_VAR="/my/path/to/scripts"
And then call your script from within that script using:
${MY_VAR}/my_script.sh
Kind of the same as running the script using its full path.
I hope this helps!
Edit 1: - If you want to run a specific script as you mentioned in the comments, under a specific directory, you can try changing directories to the one that contains the file, and then running the script like ./script1.sh.
cd /path/containing/your/scripts/
./script1.sh
Edit 2: - Maybe you can create a wrapper for your script1.sh, placing the following if-block in there to check if the script is under the current directory or not.
if [ -e script1.sh ]; then
./script1.sh # runs the script under your current directory
else
script1.sh # runs the script searching for it in $PATH first
fi

Store frequently used commands in Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I use some Linux commands more frequently like going to a specific directory, grep for some text etc. I assigned a variable for each command and kept them in a script file and I run the script file every time when I login to my Linux box so that I can use the variables instead of typing lengthy commands.
Is it possible that I can make sure that my script file runs everytime when I login to my Linux box so that I need not run it everytime?
Is there an alternate way of storing my frequently used commands so that they will be available when I open my Linux box?
If you are using bash (probably are), add it to your .bashrc. You will find it in your home directory.
Other shells have corresponding startup scripts.
Adding commands to .bashrc for a non-login shell, or to .bash_profile for login shells (assuming, of course, that you're using bash).
From the bash manual entry:
When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and
executes commands from the file /etc/profile, if that file exists.
After reading that file, it looks for ~/.bash_profile, ~/.bash_login,
and ~/.profile, in that order, and reads and executes commands from
the first one that exists and is readable. The --noprofile option may
be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
You have to put your script into your .bashrc file, it is in your home directory
nano ~/.bashrc
It only works when you are using bash.
What is about alias?
You can store them in the ~/.bashrc, when I am right.
You can use a .bashrc file but this script is executed when you open an interactive Bash shell. That is every time you connect to a server with a terminal (if Bash is your default shell) or open another shell that opens an interactive shell (like su - $USER).
If you work locally with X-Window GUI on Linux (Unix) the script will be executed every time you open a terminal program (like Konsole in KDE or gnome-terminal). It maybe not what you expected. In this case you can hack a .xinit script or use your display manager or desktop environment way to execute a script upon start. It is hard to tell how because it is specific to your environment (Linux/ Unix distribution or desktop environment (KDE, GNOME, ...) ) .

Resources