You are not logged in.

#1 2012-01-04 00:54:09

ViruSzZ
Member
From: The Streets
Registered: 2010-10-14
Posts: 202
Website

[SOLVED] - Need some help with printf loosing the formatting

I want to add some color to a variable (string) in a script I wrote only if an expression is true and then I'm passing it to printf to echo it out with a format for the variable and once it's colored the format is not working.

I can't really figure it out why is it behaving like that. The script runs under a vServer patched kernel with a few containers running on the machine and fetch some interesting information like CID/LoadAvg/IP/Mem/Proc/Thread stats so to reproduce this, an example like below can be used:

#!/bin/bash
#
COL_RESET=$(tput sgr0)
COL_RED=$(tput setaf 1)

format=" %-5s %-10s %-16s\n"

[[ $1 == 'color' ]] && colored="${COL_RED}colored${COL_RESET}" || colored="color"

printf "$format" "foo" "$colored" "bar"

Format is normal.

┌─[23:57]-[root@stavrovski]-[~]
└─› bash /tmp/printf_col
 foo   color      bar

Format lost.

┌─[23:57]-[root@stavrovski]-[~]
└─› bash /tmp/printf_col color
 foo   colored bar

Any inputs?

Last edited by ViruSzZ (2012-01-04 02:11:16)


Their Momma Made Em, Their Momma Gave em & now she can`t even SAVE`em | My WebLog

Offline

#2 2012-01-04 01:34:59

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] - Need some help with printf loosing the formatting

I think I know the problem, but not the solution.

The coloring adds escape characters that are being counted.  This shows up in colored bash prompts as well where it can interfere with readline or history.  The solution in a bash prompt is to enclose the color escape sequences in \[ and \].  That does not seem to work here, though some variant of that may.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-01-04 01:43:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] - Need some help with printf loosing the formatting

If suitable for your situation, a work around would be to use tputs cursor movement rather than width specifiers in a format string.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2012-01-04 01:50:05

ViruSzZ
Member
From: The Streets
Registered: 2010-10-14
Posts: 202
Website

Re: [SOLVED] - Need some help with printf loosing the formatting

Thanks for enlightening me.

Hmmm, I may look into using tput instead but for now I'm looking to finding a solution to this (if any? don't know). Anyhow, I'm just having fun with this and fetching some information about the vm's running on my home pc.


Their Momma Made Em, Their Momma Gave em & now she can`t even SAVE`em | My WebLog

Offline

#5 2012-01-04 02:10:46

ViruSzZ
Member
From: The Streets
Registered: 2010-10-14
Posts: 202
Website

Re: [SOLVED] - Need some help with printf loosing the formatting

Ok, when I'm using the color within the format then it seems to work. So, instead adding a color to a variable, I will add it to the printf's format.

@Trilby, thanks for your explanation on this as I was not quite sure why was acting like that. That helped me figure it out.

Marking as [SOLVED].

Take care.


Their Momma Made Em, Their Momma Gave em & now she can`t even SAVE`em | My WebLog

Offline

Board footer

Powered by FluxBB