System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

non-displayable characters returned using "su - logonid -c string"

Scott Lindstrom_2
Regular Advisor

non-displayable characters returned using "su - logonid -c string"

I need to 'su' to a user in a script and use the 'which' command to find which version of a file the user runs. But when I do the following I also get some non-displayable characters returned.

su - patrol -c "/usr/bin/which PatrolAgent"|grep PatrolAgent|cat -vet

returns:

^M^[[3g^M ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H ^[H^M/application/patrol/3.6/PatrolAgent$

I need to just get the path and program name. I have tried various combinations using 'tr' but still am having trouble:

su - patrol -c "/usr/bin/which PatrolAgent"|grep PatrolAgent|tr -cd "[:alnum:]"

gets me:

3gHHHHHHHHHHHHHHHHHapplicationpatrol36PatrolAgent

so that's no good. Any thoughts on another way to extract the contents of the which command?
7 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: non-displayable characters returned using "su - logonid -c string"

Hi Scott:

Don't use 'su -' which causes your profile to be sourced (read). Instead use:

# su patrol -c "..."

Regards!

...JRF...
Jim Walls
Trusted Contributor

Re: non-displayable characters returned using "su - logonid -c string"

Presumably PatrolAgent is located somewhere in the target user's PATH - which is defined in the user's .profile. If this is the case then you would, normally, have to use "su -" otherwise PatrolAgent wont be found.

You could try the following:

su patrol -c '. ~patrol/.profile;/usr/bin/which PatrolAgent'

This will execute the .profile without setting up a whole new environment.

NB: White space between "." and "~patrol"







Jim Walls
Trusted Contributor

Re: non-displayable characters returned using "su - logonid -c string"

Hmmm! Sorry, that doesn't appear to help!

However, the non-printable characters are emitted by the "tabs" command.... which is probably present in the user's .profile to set up the terminal's tab-stops for interactive working.

Try commenting out the "tabs" line in the .profile

This may cause unexpected behaviour if the user logs in to an interactive session.



Steven Schweda
Honored Contributor

Re: non-displayable characters returned using "su - logonid -c string"

> Try commenting out the "tabs" line in the
> .profile

Or making it conditional on talking to a real
terminal. (What does "tty" say in this
environment?)
Scott Lindstrom_2
Regular Advisor

Re: non-displayable characters returned using "su - logonid -c string"

James, Jim - I do need to source the profile so I can set the the PATH.
Scott Lindstrom_2
Regular Advisor

Re: non-displayable characters returned using "su - logonid -c string"

Jim, Stephen - let me look into the profile and see about the tabs.
Scott Lindstrom_2
Regular Advisor

Re: non-displayable characters returned using "su - logonid -c string"

It appears removing the tabs command (which the user does not need) will solve my problem. I will post more after I can test some more.