I am running a script which sends an output as an email; I am having issues with the spacing being not uniform in the message. Snippet of the code and email message below:
if [[ $Backup == '' ]] then echo "$Hostname\tMISSING\tHMCBackup" >> $BackupMsg
else if [[ $Backup > $Current ]] then echo "$Hostname\tGood\tHMCBackup" >> $BackupMsg
else if [[ $Backup < $Current ]] then echo "$Hostname\tStale\tHMCBackup" >> $BackupMsg; fi; fi; fi; done < $SupportedHosts
while read Hostname Backup Current
do
if [[ $Backup == '' ]] then
printf "$Hostname\tMISSING\tHMCBackup\n"
else
if [[ $Backup > $Current ]]
then
printf "$Hostname\tGood\tHMCBackup\n"
else
if [[ $Backup < $Current ]]
then
printf "$Hostname\tStale\tHMCBackup\n"
fi
fi
fi
done < $SupportedHosts | expand > $BackupMsg
your issue is with the number of characters in your hostname (i.e, fcop23 has 6 and ecopc22 has 7) that is throwing off the tab spacing since tab is placed in relation to the last character of the word preceding it.
while read Hostname Backup Current
do
if [[ $Backup == '' ]] then
printf "$Hostname\tMISSING\tHMCBackup\n"
else
if [[ $Backup > $Current ]]
then
printf "$Hostname\tGood\tHMCBackup\n"
else
if [[ $Backup < $Current ]]
then
printf "$Hostname\tStale\tHMCBackup\n"
fi
fi
fi
done < $SupportedHosts | expand | sed 's/^/<pre>/; s/$/<\/pre>/;' > $BackupMsg
Can I just check that there are actually commas in the raw output. It doesn't look like it to me and so the tr will have no effect.
Am I missing something?
Perhaps you need to use printf instead of echo, making sure that you add in the required newlines, e.g. printf "Hello\tworld.\n"
Surely it's not that simple?
Another possibility is that your tr has fields wrapped in single quotes, which forces the content to be literally the characters as typed. If you use double quotes, that may help it interpret \t as a tab character.