Was trying to create a text block that I could just copy and paste into a Terminal window to accomplish something (in this case, create a systemd script to do maintenance on MySQL dbs, create a timer file to run that script weekly, and to enable the script).
When pasting it into a Terminal, it displays half of the script (up until the first EOF), prompts for the password through read, and then copies the typed password, along with the rest of the script in the spot where $pass is in the first file.
This is the block:
sudo sync && echo 'Enter MySQL password for Maintenance user' && read pass && sudo bash -s -c 'cat > "/usr/lib/systemd/system/db-m.service"' << "EOF"
[Service]
Type=oneshot
ExecStart='/usr/bin/mysqlcheck' --auto-repair --optimize --all-databases --force -u'maintenance' -p'$pass'
ExecStart='/usr/bin/sync'
EOF
sudo bash -c 'cat > "/usr/lib/systemd/system/db-m.timer"' << EOF
[Unit]
Description=Weekly database repair and maintenance
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload && sudo systemctl enable 'db-m.timer' && sudo systemctl start 'db-m' 'db-m.timer' && sudo systemctl status 'db-m' -l
I recently added the "read pass" and the '$pass' variable that occurs 3 lines later; without those two things, I can copy and paste the entire block no problem.
expect
, as that can enter one command, check and wait for the result, do the next command, etc. Pasting will run into all sorts of fun interactions depending on what reads or writes what when, buffer sizes, etc.what you should do is put this in a script, with '#!/bin/sh' at the top
. don't paste it directly into your shell.