Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script : isolate CR/LF from a variable

Status
Not open for further replies.

DGRFL

Programmer
Dec 28, 2000
36
BE
Hello,
I am using Microsoft Services for UNIX to port Korn shell scripts towards a NT environment.
In order to evaluate an environment variable passed on the command line, I wrote:
hlp=$(echo "$"$1)
result=`eval echo $hlp`
On OSF and HP/UX, the result variable now contains the value of the environment var. whose name is in $1.
On NT, this result variable has a carriage return appended to it.
I can remove this by typing
result2=${result:0:22}
if I know the significant length, but normally I do not know the length.
So how can I check whether there is a CR or LF present in an environment variable, and how can I remove it in a simple way ?

Regards,

Luc
 
There is, I remember, some pattern matching stuff in ksh that uses the same type of construct, ${result:0:22}, as you're already using. Have a look in the ksh docu for pattern matching as I can't remember how to do it.
Mike
michael.j.lacey@ntlworld.com
 
Mike,

There is a construct ${parameter%%pattern} that deletes the longest possible suffix that matches the pattern, but how can I indicate CR or LF inside this pattern ?

It seems that ${parameter%%[\015\012]} does not work, and ${parameter%%['\r''\n']} neither.

Has anybody a suggestion ?

Luc
luc.degraef@cosmosworldwide.com
 
Luc,

This is probably a stupid comment, but shouldn't that be:
[tt]\10\13[/tt]
Or am I just getting my bases mixed up?
Mike
michael.j.lacey@ntlworld.com
 
Hello,

In the meanwhile I did the following investigation :
the above problem arose in a shell script; however when I execute these commands from the command line the carriage return or line feed is NOT appended.

So it had to be a problem with the Shell script interpreter.

I downloaded the MKS toolkit demo software (an older version of which is being used by the Microsoft Services for UNIX), and in this MKS toolkit the problem does not occur.
So it is a bug in the Microsoft attempt to support UNIX commands. (Did I hear someone laughing ?)

Concerning your advice, I tested it but it is not working.


Thanks for the suggestions, but it all ended up in a weird way.
Luc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top