How to run bash file with one word [duplicate] - linux

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.

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.

Shell script to replace long predefined arguments [duplicate]

This question already has answers here:
Propagate all arguments in a Bash shell script
(12 answers)
How to write a bash script that takes optional input arguments?
(9 answers)
Closed 2 years ago.
This is probably the worst question I've asked, I wasn't entirely sure how to formulate it and google results are best when the search is short and concise.
So, I keep finding myself constantly running a more or less identical list of commands in a Linux terminal. For example:
./some_script -argument1 -argument2 -argument3 [varying list of parameters that differ in type]
Now, the script and first 3 arguments are always the same. I was thinking if there is a way, such that a new script could be written, so that my entire input is much shorter. Like:
./new_script [varying list of parameters that differ in type]
This seems like a perfect place for an alias:
alias new_script='./some_script -argument1 -argument2 -argument3'
Create a file new_script in some directory in your PATH, for example in /usr/local/bin/. I usually create ~/bin directory and add it to PATH. Some people like to follow XDG specifications and add ~/.local/bin to PATH.
The file needs to have executable rights and following contents:
#!/bin/sh
./some_script -argument1 -argument2 -argument3 "$#"

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 run my script from anywhere(outside the current directory) in my system? [duplicate]

This question already has answers here:
Add a bash script to path
(5 answers)
Closed 4 years ago.
I have my script in /home/testing/program. My script name is test.sh. I want to run my script from other directory. I changed the permissions of the file. In my script I have redirected the output to some(output-test.sh) file. I need to run all my script from other directory. If i run the script in the current directory it works fine. Please help me to do this. Thanks in advance.
In Unix systems, absolute paths start with / . So you can use absolute path to run a script. In your case all you have to do is run this command from the terminal
sh /home/testing/program/test.sh . It does not matter from which directory you run the command from until you use the absolute path of the script.
Also the file in which the output is written should be defined with absolute path in script file. For example, in your case
/home/centos/rr/email-body.txt
so that the file is recognized by the OS even when the script is executed from the other directory or from any directory whatsoever.

executing windows command line operation from python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call external command in Python
I want to execute a windows command line operation using python. To execute the command I have to go to a particular directory within my system and then execute the command.
for example
1) Go to a particular directory c:\some\directory
2) then use command somecommand -x -y
I saw some posts on this topic but I was not able to figure them out properly.
Thanks
I assume you want to change the working directory then execute a command. So:
os.chdir(DIRECTORY);
os.system(COMMAND);
os.chdir - Set the current working directory.
os.system - Execute a "system" command.
If setting the working directory is not required you could just specify the full path to os.system.
Also, you might want to check out subprocess as it might be more what you're looking for.

Resources