Por que minhas guias não estão expandindo em TIMEFORMAT?

1

Eu preciso medir o tempo de execução de um aplicativo, por isso estou mexendo com o TIMEFORMAT para imprimir um formato curto com a máxima precisão disponível.

No Manual de referência da Bash:

TIMEFORMAT

The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The ‘%’ character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

%% A literal ‘%’.

%[p][l]R The elapsed time in seconds.

%[p][l]U The number of CPU seconds spent in user mode.

%[p][l]S The number of CPU seconds spent in system mode.

%P The CPU percentage, computed as (%U + %S) / %R.

The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p is not specified, the value 3 is used.

The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.

If this variable is not set, Bash acts as if it had the value

$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'

If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.

Mas, sempre que eu definir TIMEFORMAT, as guias e as novas linhas não serão expandidas:

rafael@lip ~/time-tests $ time ls

real    0m0.099s
user    0m0.000s
sys 0m0.002s
rafael@lip ~/time-tests $ TIMEFORMAT='\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
rafael@lip ~/time-tests $ time ls
\nreal\t0m0.002s\nuser\t0m0.000s\nsys\t0m0.002s

Por quê? e como resolver isso?

    
por RSFalcon7 24.03.2014 / 19:27

2 respostas

3

O shell não interpreta nenhuma barra invertida entre aspas simples. Se você quiser que barras invertidas sejam interpretadas, use a construção $'...' , como em:

TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'

De man bash :

   Words of the form $'string'  are  treated  specially.   The  word
   expands  to string, with backslash-escaped characters replaced as
   specified by the ANSI C standard.  Backslash escape sequences, if
   present, are decoded as follows:
          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \     backslash
          \'     single quote
          \"     double quote
          \nnn   the  eight-bit  character  whose value is the octal
                 value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadec‐
                 imal value HH (one or two hex digits)
          \uHHHH the  Unicode  (ISO/IEC 10646) character whose value
                 is the hexadecimal value HHHH (one to four hex dig‐
                 its)
          \UHHHHHHHH
                 the  Unicode  (ISO/IEC 10646) character whose value
                 is the hexadecimal value HHHHHHHH (one to eight hex
                 digits)
          \cx    a control-x character

   The  expanded  result is single-quoted, as if the dollar sign had
   not been present.

Por outro lado, com aspas simples simples, nenhum caractere recebe tratamento especial, pois man bash explica:

   Enclosing characters in single quotes preserves the literal value
   of  each  character  within  the  quotes.  A single quote may not
   occur between single quotes, even when preceded by a backslash.

Assim, dentro de aspas simples simples, uma barra invertida é apenas uma barra invertida.

    
por 24.03.2014 / 19:35
1

Você deve usar um sinal dolar $ em atribuição para TIMEFORMAT :

TIMEFORMAT=$'\n\nreal_test\t%5lR\nuser_test\t%5lU\nsys_test\t%5lS'  
time ls
....

real_test   0m0.006s
user_test   0m0.000s
sys_test    0m0.004s
    
por 24.03.2014 / 19:38