This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 5 years ago.
I'm working a small project which needs using OpenMPI to make "mpicc" work.
I made a file make_cmd:
#!/bin/bash
module load OpenMPI
However, after executing ./make_cmd, I was told:
mpicc: command not found
But if I just type on the command line: module load OpenMPI, then mpicc is working.
Why is that? Thanks!
See this answer on neighbouring site.
Because module is an alias/shell function and not a binary program, it's not necessarily available in the non-interactive sub-shell that is created when you run your script. You could probably run source make_cmd though, as that will just run the commands in your current interactive shell. You could ditch the #!/bin/bash line in that case.
Related
This question already has answers here:
How can I check if a program exists from a Bash script?
(39 answers)
Closed 6 years ago.
I've read in some bash FAQ a while ago (that I don't remember), that which should be avoided and command -v preferred.
Why is that so? What are the advantages, disadvantages of either one?
Well...
command is likely built in to your shell, and with the -v option will tell you how your shell will invoke the command specified as its option.
which is an external binary, located at /usr/bin/which which steps through the $PATH environment variable and checks for the existence of a file.
A reason to select the former over the latter is that it avoids a dependency on something that is outside your shell.
The two commands do different things, and you should select the one that more closely matches your needs. For example, if command is built in to your shell, command -v command will indicate this with its output (through the non-existence of path), but which command will try to point to a file on your path, regardless of how command would be interpreted by your shell.
This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 2 years ago.
I'm not sure I address this problem correctly, but I've search and search over the net and found nothing, yet.
I'm writing a bash script and I want to see what the script is really doing when being executed, like a log of all the commands, one by one, what are executed - this way, I'll be able to see why my script is falling.
Note : I've post similar question in the past, someone told me I run my bash script with sh -xe script.sh but it doesn't give me enough information to debug properly.
Please advise. Thanks!
Adding set -x at the beginning of the script displays, in the terminal, all the commands sent by the script as the terminal received it. It was exactly what I needed and it working perfectly.
This question already has answers here:
How execute bash script line by line?
(5 answers)
Closed 4 years ago.
I work with git-bash in windows. I have found a bash script that I would like to modify on GitHub. I cloned it and opened it in my pycharm editor. There is a plugin https://www.plugin-dev.com/project/bashsupport/#installation which I've added, but from the documentation this does things like syntax highlighting. Is there a way to step through the code line by line, set breakpoints etc. I don't have much shell scripting experience and stepping through the code might speed up my learning .
I usually debug using -x flag (short for xtrace or execution trace) is useful to add execution information Debugging Bash scripts).
You can use it by executing:
bash -x your-script.sh
or adding adding into your script:
set -x
This question already has an answer here:
Running several scripts in parallel bash script [duplicate]
(1 answer)
Closed 7 years ago.
I have the following shell script
#for i in {0..10}
do
run my command that takes about 10 seconds with parameter $i
done
How can I get this to run the commands in parallel without using GNU Parallel as I am not able to install it on my linux box.
Is there a way I can create 10 different shell scripts and call them e.g. script_1.sh, script_2.sh, script_3.sh etc and then launch them one by one from this script?
You could use an ampersand (&) and launch script.sh $1 & ten times. This will make the script run in a fork of the main process. It is an easy way to do parallel processing but definitely not very flexible and doesn't have many features. A simple tutorial can be found here.
This question already has answers here:
Should I use a Shebang with Bash scripts?
(8 answers)
Closed 8 years ago.
Suppose you have a bash script B, that you are sourcing from another bash script A. B has a bunch of variable and function definitions. A is the main driver script. Do you need the #!/bin/bash line on top of both A and B? What happens if you do and if you don't?
The shebang is only mandatory for those scripts, which shall be executed by the operating system in the same way as binary executables. If you source in another script, then the shebang is ignored.
On the other hand. IF a script is supposed to be sourced, then it is convention to NOT put any shebang at the start.
The shebang is used if you run the script directly as an executable file (for example with the command ./script.sh). In this case it tells the operating system which executable to run.
It's not required and has no effect if you for example write bash ./script.sh or source the script.
You should use shebang in all scripts especially those using any non-sh compatible features.
In Debian for example, default shell is dash (not bash). If you use bash-only feature and don't specify that this script should be interpreted by bash it may fail even on linux. It will fail on Solaris or HP-UX almost for sure.
If your file is to be only sources by other script, then you can omit shebang line but do not set executable permission. Also for such files is good to keep /bin/sh compatibility.
I strongly recommend to read DashAsBinSh.