I can't use the terminal in an external file

Thanks guys, I already managed to solve the problem.

Please share your solution and code to help others who may have a similar situation.