For example if I have a python file test.py
.
Running python test.py
in command line crashes with message:
Segmentation Fault(core dumped)
How can I save such message into a txt file with a script? (Imagine I have test1.py, test2.py, test3.py... and they all crash. I want to collect the crash log of them all and do some analysis)
That message is coming from the shell, not the program. If you want to capture it, you need to run the command in a subshell so you can redirect the shell's output. You need to redirect stderr to get error messages.
for script in test*.py; do
(python "$script")
done 2>errors.txt