[tutorial] Combining Shell Script and Python for Efficient Automation

πŸš€ Example-1

1
2
# demo-1.py
print("This is demo 1.")

How write and run shell script

  1. Write a shell script similar to the example below and save it as run.sh
    1
    2
    # run.sh
    python demo-1.py
  2. Open Git Bash in the Terminal
  3. bash run.sh

πŸš€ Example-2

1
2
3
4
5
6
7
8
# demo-2.py
string = input("type something.....")

print(f"""

String = {string}

""")
1
2
3
# run.sh
echo "hello world - 1" | python demo-2.py
echo "hello cat - 2" | python demo-2.py

πŸ’‘Error run.sh: line 1: python: command not found

  1. Configure the path to your Python executable
    • To find the path of your Python exe, you can use the command where python
      1
      2
      3
      # run.sh
      PYTHON=/mnt/c/Miniconda3/python.exe
      "$PYTHON" demo-1.py

[tutorial] Combining Shell Script and Python for Efficient Automation

https://hsiangjenli.github.io/blog/tutorial_shell_script_and_python/

Author

Hsiang-Jen Li

Posted on

2023-04-05

Updated on

2024-05-19

Licensed under