How to see bash script executed command? [duplicate] - bash

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.

Related

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

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

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.

Get path of where a script was called from [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 5 years ago.
If I have a bash script in my home directory and call for it to execute from my Desktop, is it possible for the script to know the request to execute came from the Desktop? I've figured out how to get the path of where the script file is, but I want to know where the request came from.
Thanks!
Try this:
srcdir=$(pwd)
which gets you the current directory. That is, where you are calling your script from, which should be Desktop something in your example.
As duly discussed in the comments, this alternative is pretty good too:
srcdir=$PWD

Regarding operation flow in a bash script [duplicate]

This question already has answers here:
bash script order of execution
(2 answers)
Closed 7 years ago.
I was just wondering, in a bash script, if I have a command on one line to say rsync a large file (~10GB) then the next command on the next line is meant to rename then move that same file, will the script know to wait for the rsync to complete before attempting the rename and move?
Is there a flag or something I can put on each line to make it wait before executing the next command?
Sorry if this seems like a total noob question, but alas I am a noob!
Thanks in advance for any info!
All commands are executed in sequence, so, a command will wait for the previous command to complete. Only when you add a & to the end of a command will it run in the background, and thus not wait for completion before executing the next.

Finding location of command executed [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can a Bash script tell what directory it's stored in?
Is there a command-line function (bash) to find the location of the command you've executed?
I understand I could check my $PATH for things I've imported manually but there must be some way to do this. A tricky echo command or something?
Besides whereis, see also which and type.

Resources