Execute the shell script that is in the current directory, not the one in $PATH [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 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

Related

How can I run a command in a shell script function that requires access to the current shell instance? [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 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.

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 do I make a script or program to run certain commands? [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 am trying to get into writing scripts that execute in the terminal. I wanted to know what the best way to do this was. I want to start by making a simple script that will run four or five commands that will update a certain program on my computer and have that run every day at a certain time. I have a programming background, but I am unfamiliar with this kind of scripting. I would appreciate any advice or input such as what language to use.
First of all, you need to open a Terminal (such as Terminal, Terminator, etc) and then you run this:
touch myScript.sh
chmod 755 myScript.sh
The first command creates an empty file and then you give 755 permissions to it. It means that it will be readable and executable by any user in your machine. If you need more details about those permissions, you can refer to the documentation here. But, believe me, those permissions will work for the moment.
Now you can insert instructions into the file using several methods: You can open it with a text editor such as vi, etc; Also, you can echo those commands this way:
echo "ls /tmp" >> myScript.sh
echo "echo 'hello'" >> myScript.sh
echo "pwd" >> myScript.sh
If you open that file, you can find that it is simply a list of commands one at each line. Then, when you run the script, each command will be executed in order from top to bottom.
You can run the script using the following sintax:
./myScript.sh
Voilá!
crontab -e
Then add following line beside the above command:
30 10 * * * script.sh
It will run everyday at 10:30 AM

Crontab that executes shell script every minute [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 10 years ago.
Improve this question
I have made a shell script on my red hat server that needs to be run every minute. My script is located in /media. I have edited the crontab like so:
* * * * * /media/statusshellscript.sh
My script is definitely in the location above and I know that 5 stars means run every minute.
oh.. and my script definintely works! because when I do a ./statusshellscript it works fine. Here is my script anyway, it basically just runs a php script I made which made life easier.
#!/bin/bash
# Script to execute the PHP Script
cd ~
cd /media/PHPServerTest
php -f index.php
Crontab is doing absolutely nothing at the moment. Not sure what to try next?
Also.. permissions shouldn't be a problem as i've done chmod 777 statusshellscript.
if its not running though cronjob but by command its working fine then there can be two reasons
1) you never made your file executable , that you can resolve my using the command
sudo chmod +x filename
2) your path is not correct , for finding absolute path you can use command
realpath(filename)
if realpath is not already installed it will mention you a command how to install it
by checking these points it should work fine.
The PATH for a crontab is not the same as in a shell.
Make sure that you define a PATH in your crontab that includes everything that is needed by the script.
Also, make sure that the script starts with a valid #! marker that points at the desired shell.
Or, use the full path for all commands in the script.
As others have said, my bet would be a misconfigured PATH. Try putting this into your path:
"* * * * * /media/statusshellscript.sh"
Go check that output file to see the PATH when the script is run. And rather than defining your PATH in the crontab, just define it in your script.

Resources