I am writing a client for doing various tasks on the zimbra server via ssh, and I've hit a bit of a snag. Here's the code:
#!/bin/bash
set -e
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export HISTCONTROL=ignoreboth
# History management
if [ -f $HOME/.zmcli_history ]; then
history -r $HOME/.zmcli_history
else
touch $HOME/.zmcli_history
history -r $HOME/.zmcli_history
fi
trap "history -a $HOME/.zmcli_history && echo -e '\nExiting now.\n'" exit
# Styling
yellow='[33m'
bold='[1m'
off='[0m'
echo
# Client
while :; do
read -ep "$bold${yellow}zimbra_cli[${off}server_domain$bold${yellow}]> $off" comm
history -s "$comm"
done
NOTE: if you execute this script, it will create the history file (.zmcli_history) in your home directory.
Everything works as intended, and the history is all there, but often the part of some (usually long) previous command remains as I continue to scroll through history. That part of command acts as a part of the prompt for the rest of the session, unless I hit CTRL+L which clears the screen and resets it to normal. I have read through many posts that helped people who had this problem in their bash terminal. Their problem was that PS1 was improperly formatted and bash got confused about the length of the prompt string. It is probably something similar here, but those solutions do not work, because read command can not use those escape characters like echo can (\e).
I managed to find a way to escape the color sequences using literal escapes (CTRL+V ESCAPE in vim insert mode). So the colors work, but I am stuck with this history problem.
Things I have tried:
- command substitution :: write and escape the color codes in a way that echo -e can print them, and then echo it in the read command $(echo -e '\e[33m'). Still seeing the colors, but didn't help with history problem
- adding escaped [ and ] around the color codes
- putting the whole prompt with color codes in a separate echo command that comes before the read command. The downside is that I can not turn off formatting until after the read command (in another echo), which means that the whole prompt and command will be bold and in color. Also, if I press CTRL+L now, even the prompt will get cleared, and I will only be left with the cursor
Finally, since I can not drop the history, I tried dropping the colors. But then it gets impossible to quickly differentiate between commands and output.
You can replicate the problem by starting the script and putting in 10-15 long (gibberish) commands. After that, try scrolling up and down through history and you will see what I'm talking about.
Anyone knows a way to make this work?