I'm writing on a huge script-factory script generating a lot of maintenance scripts for my servers.
Until now I write some lines which need to be written in one line with echo -ne
e.g.
echo -n "if (( " | sudo tee -a /usr/local/bin/upgradeAllServers &> /dev/null
# Generate exitCode check for each Server
IFS=" "
COUNT=0
while read -r name ipAddr
do
if(($COUNT != 0))
then
echo -n " || " | sudo tee -a /usr/local/bin/upgradeAllServers &> /dev/null
fi
echo -n "(\$"$name"_E != 0 && \$"$name"_E != 1)" | sudo tee -a /usr/local/bin/upgradeAllServers &> /dev/null
COUNT=$((COUNT+1))
JOBSDONE=$((JOBSDONE+1))
updateProgress $JOBCOUNT $JOBSDONE
done <<< "$(sudo cat /root/.virtualMachines)"
echo " ))" | sudo tee -a /usr/local/bin/upgradeAllServers &> /dev/null
this generates me (if there are e.g. 2 servers in my config file) code like
if (( ($server1_E != 0 && $server1_E != 1) || ($server2_E != 0 && $server2_E != 1) ))
All other code blocks which don't need this inline writing of code I produce with heredocs since I find them way better to write and maintain. E.g. after the upper code I have the rest generated like
cat << EOF | sudo tee -a /usr/local/bin/upgradeAllServers &> /dev/null
then
# Print out ExitCode legend
echo " ExitCode 42 - upgrade failed"
echo " ExitCode 43 - Upgrade failed"
echo " ExitCode 44 - Dist-Upgrade failed"
echo " ExitCode 45 - Autoremove failed"
echo ""
echo ""
fi
EOF
So the final code block looks like
if (( ($server1_E != 0 && $server1_E != 1) || ($server2_E != 0 && $server2_E != 1) ))
then
# Print out ExitCode legend
echo " ExitCode 42 - upgrade failed"
echo " ExitCode 43 - Upgrade failed"
echo " ExitCode 44 - Dist-Upgrade failed"
echo " ExitCode 45 - Autoremove failed"
echo ""
echo ""
fi
My Question
Is there a way having a heredoc behave similar to echo -ne
without the end-of-line-symbol?
sudo
in a script you're doing it wrong: Run the whole script as root instead! – dessert Nov 30 '17 at 09:35sudo -u USERNAME
on that instead, see How do I run a 'sudo' command inside a script?. – dessert Nov 30 '17 at 11:20sudo
without username asks for the password, unless you typed it in there's a delay. It's even worse if you don't run the script in a terminal, then you can't even see the password query and it will just no work. Thesudo -u
approach doesn't have any of these problems. – dessert Nov 30 '17 at 13:13sudo
– derHugo Nov 30 '17 at 14:02rm -rf ${empty_var}
type mistakes. It doesn't require anything installed on the target machines, and if one of the builtin modules doesn't do something you need, you can run your own bash scripts from Ansible to get free parallellism. – Ben Nov 30 '17 at 19:25