0

I trying to get a value from a command into a var
and then print it our using printf.

Problem: i got the value in the var but with printf it does not appear
or is cut off.

INFO: In my script im calling redis-cli info memory
and to check whats wrong i tried a call on vmstat -s.

Working vmstat test:

format="%-16s | %-16s"
container_name="some_name"
used_memory=$(vmstat -s | sed -n "s/^\(.*\) K used memory.*$/\1/p")
row=$(printf "${format}" "${container_name}" "${used_memory}")
echo "${row}"

Output: some_name | 11841548

The actual script that is not working:

format="%-50s | %-16s"
container_name="totally_secret_container_name_1"
used_memory=$(docker exec -it "${container_name}" redis-cli info memory | sed -n "s/^used_memory_human:\(.*\)$/\1/p")
row=$(printf "${format}" "${container_name}" "${used_memory}")
echo "${row}"

Output: ecret_container_name_1 | 1.08M

Weird is than when i set the format to format="%-50s | %-1s"
then it works - the container name (left value) gets printed correctly.

What happen here?
How can i fix this?

Thanks for your time!

cottton
  • 1,522
  • 14
  • 29
  • There's a carriage return in the output of the command. – Barmar May 05 '22 at 21:12
  • 2
    Pipe the command to `od -c` and look for `\r` – Barmar May 05 '22 at 21:12
  • ```used_memory=$(... | sed -n "s/^used_memory_human:\(.*\)$/\1/p" | od -c)``` brings me ```0000000 1 . 0 8 M \r \r \n``` . – cottton May 05 '22 at 21:43
  • @Barmar So as i read `od -c` is just to see the chars. I now see that i got `\r \r \n` at the end of the string. But im not able to remove them. I even tried to not capture them with ```sed -En 's/used_memory_human:([0-9a-zA-Z\.])/\1/gp'``` but it still captures new line and reset(?). How can i get rid of those chars? – cottton May 05 '22 at 22:19
  • 1
    Pipe to `tr -d '\r'` – Barmar May 05 '22 at 22:59
  • @Barmar Yes! =) ```redis-cli info memory | sed -n "s/^used_memory_human:\(.*\)$/\1/p" | tr -d '\r'``` That works. Thank you! You wanna post an anser so i can accept it? – cottton May 05 '22 at 23:31
  • 1
    See [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/q/39527571/4154375) and [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/4154375). – pjh May 05 '22 at 23:44

1 Answers1

1

You need to remove the \r characters in the output that are causing it to go back to the beginning of the line and overwrite.

used_memory=$(docker exec -it "${container_name}" redis-cli info memory | sed -n "s/^used_memory_human:\(.*\)$/\1/p")
used_memory=${used_memory//$'\r'/}
row=$(printf "${format}" "${container_name}" "${used_memory}")

This uses the bash ${variable//old/new} replacement operator.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This version does not work :/ I dont know what `/$'\r'/` does, but im getting the same cut off string again. – cottton May 06 '22 at 00:26
  • 1
    Sorry, it needs to be `//` to replace all occurrences rather than just the first. – Barmar May 06 '22 at 00:34