简体   繁体   中英

Run python script inside bash file in Windows

I was tasked with running a script developed by someone else. It's quite simple, but it's a bash script and I had almost never touched Linux, so I'm not sure on how to proceed. I was able to install WLS so I can run bash on Windows, but now I have to run a specific python script inside the bash script. The script goes like this:

#!/bin/bash
BASE_DIR=dir

find $BASE_DIR -type f | grep '\.pdf' | while read pdf_filename; do
  filebase=`echo $pdf_filename | cut -d '.' -f 1`
  txt_filename="$filebase.txt"
  echo "Processing $pdf_filename..."
  pdf2txt.py $pdf_filename > $txt_filename
  echo "Done!"

done

It should run the pdf2txt.py script, but I'm getting this error:

convert_all.sh: line 8: pdf2txt.py: command not found

So, I'm not sure how to connect bash to my Python installation, I'm guessing it's not being able to find it. I would ideally like to link it to this project's virtual environment. Any ideas on how to proceed?

Edit:

This is my current error based on what I responded to @DV82XL:

/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 1: $'\r': command not found
/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 2: syntax error near unexpected token `"hello world"'
/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 2: `print("hello world")'

Can you convert the bash script to Python? That way you can easily run in Windows or Linux without WSL.

If you must run the bash script in WSL, make sure Python is installed in WSL:

  • Type type -a python or type -a python3 . This will give you the interpreter path.

If it doesn't show up, you will need to install Python on WSL:

sudo apt update && upgrade
sudo apt install python3 python3-pip ipython3

Then do these things:

  1. Make sure the Python interpreter is in the PATH env var by typing echo $PATH . If it's not there, add it by typing export PATH="$PATH:/usr/bin/python3" or add it to ~/.profile. On Linux, it's usually included by default.
  2. Add the path to the script to PATH env var if you want to run it from anywhere
  3. Type python --version or python3 --version to get versions and make sure python path was set correctly.
  4. Add a shebang with the interpreter path at the start of the python script:
    • #!/path/to/interpreter
    • Typically: #!/usr/bin/python3
    • For specific interpreter version: #./usr/bin/python2.7
  5. Make the script executable: chmod +x pdf2txt.py

Now you should be able to run pdf2txt.py directly instead of python pdf2txt.py .

Hint: In WSL, you can access your Windows files at /mnt/c/Users/<user>/path/to/file if you need to.

If this doesn't work, please let us know which Linux distro/version you are running and what version of Python these scripts require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM