kopalvich
kopalvich

Reputation: 444

Linux bash command incorrectly handles zeros in output.

I'm using Debian 6-64. When i'm running a command

echo -n `cat /proc/$(ps -o pid --no-header -C x-session-manager | tr -d ' ')/environ 2>/dev/null | tr '\0000' '\n'|grep XA|cut -d '=' -f 2`

to acquire XAUTHORITY for the current user logged in, I expect it to return at the moment my actual xauthority path, which is:

/var/run/gdm3/auth-for-alex-g5t0xM

but what it actually returns is

/var/run/gdm3/auth-for-alex-g5t

the part 0xM is missing.

Apparently it somehow takes 0 as '\0', truncating the output.

What can I do to receive the correct output?

Upvotes: 0

Views: 55

Answers (2)

Adrian Frühwirth
Adrian Frühwirth

Reputation: 45576

This should work as well and is a little more concise:

tr '\000' '\n' </proc/$(pgrep x-session-manager)/environ | awk -F= '/XAUTHORITY/{print $2}'

Upvotes: 0

keltar
keltar

Reputation: 18399

tr manual:

\NNN   character with octal value NNN (1 to 3 octal digits)

You've given four digits. Fourth one handled as separate symbol ('0') and gets replaced too.

Upvotes: 4

Related Questions