I use a 3D-printing program that needs several shell commands to be run in sequence. The first command changes the permissions on /dev/ttyACM0 and requires administrator authentication, followed by creating and activating a Python virtual environment, entering the Printrun directory, and launching pronterface.py. The commands are:
sudo chmod 777 /dev/ttyACM0
python -m venv venv
source venv/bin/activate
cd Printrun
./pronterface.py
Would a shell script handle this workflow reliably? In particular, how should the script deal with the administrator password, and do I need to add delays between commands that take a few seconds to finish?
3 Answers
A script can run these commands in order, but you generally do not need to add delays. Shell commands normally wait for a foreground command to finish before moving to the next one. Use && between steps if you want later commands to run only when the previous one succeeds. For the device permissions, avoid chmod 777 if possible; it makes the device readable and writable by everyone. A udev rule is the better long-term solution and can assign the appropriate permissions without requiring sudo each time.
You can ask for administrator authentication from a script, but embedding or manually piping a password into sudo is unsafe. If this is being launched from a graphical desktop, pkexec may provide a graphical authentication prompt, depending on your system configuration. It is still preferable to configure the device access correctly with a udev rule rather than changing permissions every time the program starts.
The virtual-environment commands need a little care. `source venv/bin/activate` changes the current shell, so the script should run the later commands in that same shell. Also, creating the environment every time may be unnecessary; create it once, install the required Python packages into it, and then launch the program using the environment's interpreter. Make sure you understand what each command does before running it with administrator privileges.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures