title: Bash script to check which serial port is connected
body: <p>I have a device I connect to with the serial port. I have a python program that talks to it. But this must be launched via a bash script, which will first check which serial port it is connected to, and provide necessary permission. Then it will launch the python script, which should have a mechanism to understand which port it is connected to (like an argument being passed to it by bash script).</p>
  
  <p>Currently my bash script is</p>
  
  <pre><code>#script
  sudo chmod 666 /dev/ttyUSB0
  sudo chmod 666 /dev/ttyUSB1
  sudo chmod 666 /dev/ttyUSB2
  </code></pre>
  
  <p>If the device is connected to ttyUSB1, I get the error <code>chmod: cannot access ‘/dev/ttyUSB0’: No such file or directory</code> for the first line. That's how I know it's connected to USB1. The python program has the line <code>ser=serial.Serial('/dev/ttyUSB1',115200, timeout=1)</code> which I change manually once I know which is the correct serial port.</p>
  
  <p>How do I check which port the device is connected to, and inform to the python program?</p>