Is there a way to step through Bash Scripts [duplicate] - bash

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

Related

How to see bash script executed command? [duplicate]

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.

why bash shell does not make any difference after executed? [duplicate]

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.

What does #! operator mean in Linux? [duplicate]

This question already has answers here:
How does the #! shebang work?
(3 answers)
Closed 5 years ago.
while following this tutorial I found a command
#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f
I don't understand the meaning of #!/bin/sh. I tried to search it but google removes the ! symbol from search results.
What does #!/bin/sh mean here?
Please help.
#! specifies the program with which the script should be executed if you not explicitly call it which any
in your case, if you call your script with: <scriptname.sh> Linux will execute it as /bin/sh <scriptname.sh>

Launch shell script from another script [duplicate]

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.

Bash - force error if variable not defined [duplicate]

This question already has answers here:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.

Resources