OVH Cloud OVH Cloud

Chaine de caractère et espace

3 réponses
Avatar
joulio
Bonjour,

j'ai des variables qui contiennent des espaces, et lorsque ceux-ci sont
=E0 la fin ils sont toujours supprim=E9s, ce que je ne souhaite pas.

Ainsi:
>TOTO=3D"TOTO "
>expr length $TOTO
4

Et non 5.

Comment conserver cet espace de fin pour que la longueur de chaine soit
=E0 5 ?

merci

3 réponses

Avatar
Laurent Wacrenier
écrit:
j'ai des variables qui contiennent des espaces, et lorsque ceux-ci sont
à la fin ils sont toujours supprimés, ce que je ne souhaite pas.

Ainsi:
TOTO="TOTO "
expr length $TOTO
4


Et non 5.

Comment conserver cet espace de fin pour que la longueur de chaine soit
à 5 ?


expr length "$TOTO"


Avatar
Stephane Chazelas
On 7 Oct 2005 01:14:32 -0700, wrote:
Bonjour,

j'ai des variables qui contiennent des espaces, et lorsque ceux-ci sont
à la fin ils sont toujours supprimés, ce que je ne souhaite pas.

Ainsi:
TOTO="TOTO "
expr length $TOTO
4


Et non 5.

Comment conserver cet espace de fin pour que la longueur de chaine soit
à 5 ?
[...]


Leaving a variable unquoted when in a list context (here the
arguments of a command) has a very special meaning to the shell.
You should never do that unless you know what you are doing and
why. Also note that "length" is not a standard operator to expr.

expr "X$TOTO" : '.*' - 1 || :

The "X" is because "expr" has a badly designed syntax. If $TOTO
were any known operator to "expr", "expr" would fail without
"X". Fortunately (so far) expr has no operator beginning with a
"X".

"..." is to prevent the shell doing word splitting (which
resulted in the removal of spaces in your case) and filename
generation. The '...' is because "*" is a special character to
the shell. "|| :" is because "expr" would return a "false" exit
status if $TOTO is empty while there's no reason it should.

--
Stephane


Avatar
Stephane Chazelas
On 07 Oct 2005 08:28:21 GMT, Stephane Chazelas wrote:
[...]
expr length $TOTO
4


Et non 5.

Comment conserver cet espace de fin pour que la longueur de chaine soit
à 5 ?
[...]


Leaving a variable unquoted when in a list context (here the
arguments of a command) has a very special meaning to the shell.
You should never do that unless you know what you are doing and
why. Also note that "length" is not a standard operator to expr.

expr "X$TOTO" : '.*' - 1 || :
[...]


hrummm, j'imagine que ca devait arriver. Ca m'apprendra a poster
dans les news avant mon premier café. Sur le point de vue
technique, sinon du langage, je pense que c'est correct, sinon.


Désolé,
Stephane