I tried out shell scripting because I wanted to automate some Linux commands. But I quickly realized that the code is written very differently from what I was used to and that I don't have the same capabilities and an ocean of tools as I do with high-level programming languages.
So I started researching how to execute commands inside of python. But I ran into a big problem. There are several methods of running commands through python, but from what I know they all execute the commands separately (each time in a new shell).
For example, if I run:
import os
os.system("pwd")
The result would be something like this: /home/username/some/path/
Now if I execute:
os.system("cd ..")
os.system("pwd")
The expected output would be: /home/username/some/
But the reality is that I am still in the same directory (because the commands were executed separately).
I know that there are things like os.chdir()
or that I could chain the commands like this cd .. | pwd
. But it is not only about changing the directory, and I want to put logic in between those commands while retaining the shell instance.
My question is: Is there any way to do what I want? Is there at least some workaround? Do you know some other language that has these capabilities?