Just use two pipes ||
(OR)
!#/bin/bash
python main.py || python3 main.py
If the syntax fails in Python 2 (I believe your goal) then it will execute the python3
In the example the script was:
import sys
version = sys.version
# Python 3
# value = f"Version: {version}"
# Python 2 e 3
value = "Version: " + version
print(version)
See that he printed Version 2.7.16
Now if you change the comments of the lines to:
import sys
version = sys.version
# Python 3
value = f"Version: {version}"
# Python 2 e 3
# value = "Version: " + version
print(version)
The result will be Version 3.7.3
(in my case, your machine may have different versions of Python 2 and 3)
You may have noticed that you still have the error when you tried to run a Python2 syntax in Python3, before trying Python3, if you want to "delete" this type of message in the terminal you can use the 2>/dev/null
in Python 2:
!#/bin/bash
python main.py 2>/dev/null || python3 main.py
In Python3 do not need, after all if fail in both the error is something else.
similar question to this one: https://answall.com/questions/427389/loop-para-rodar-scripts-python/427390#427390
– Lucas