I'm learning Python while converting some bash scripts to Python shell scripts. One thing I don't yet understand is how to deal with the heredocs used in these scripts. Here are two examples of how the bash scripts use heredocs:
The most important thing I need to know how to do in Python is this first case where the heredoc is used to provide standard responses to commands so the command can run non-interactively:
sudo command << 'EOF'
prompt_response1
prompt_response2
EOF
Second, tee is used like to this to create a file for which sudo permissions are required:
sudo tee /etc/xdg/autostart/updateNotificationChecker.desktop > /dev/null << 'EOF'
[Desktop Entry]
Name=Update Notification
Exec=bash /usr/local/bin/updateNotification.sh
Terminal=false
Type=Application
NoDisplay=true
EOF
How would I do these things in Python?
sudo command <<'EOF'as opposed to launching a subprocess as would be done in bash. likely, there's a module or library available that you'd like to explore for that situation, so you could do it in pure python instead of fork() out or god-forbidsystem()sudoI think shell scripting is best. Such things are hard in Python and when you usesystem()orpopen2you result in an unnecessarily complicated program. There's things better done in the shell than in Python. On the issue of heredocs, in Python you can use multiline strings (usingtextwrap.dedent()to make them look better).