Operating System - HP-UX
1748244 Members
3973 Online
108760 Solutions
New Discussion юеВ

Re: Problems "echoing" variables values

 
Rafael Mendon├зa Braga
Regular Advisor

Problems "echoing" variables values

Guys.. A doubt...

When I attribute a value to a variable like this:

TEST1="35"
TEST1="d"

and do this: echo TEST1$TEST2

The result is: 35d

When I grep this values from a text file and attribute it to the same variables, this way:

TEST1=`grep NUM_RELEASE versao.h |cut -f5`

***It puts the value "35" inside the variable TEST1

But when I do again: echo TEST1$TEST2, the result is: d5

Why???

How can I do it in a way that the output is the same for both forms???

Thanks in advance,

Rafael M. Braga
8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: Problems "echoing" variables values

Shalom Rafael,

TEST1="35"
TEST1="d"

echo TEST1$TEST2


Result should be TEST1

Note that your code echos the characters TEST1 fillowed by a variable that is not set $TEST2

If your screen results are 35d then there is code that has been run or this value is already in the environment variable.

This post is missing information or perhaps contains a typeo.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Rafael Mendon├зa Braga
Regular Advisor

Re: Problems "echoing" variables values

Sorry and many thanks Steven

The right is $TEST1$TEST2

Thanks again!!!
Mel Burslan
Honored Contributor

Re: Problems "echoing" variables values

Raphael,

since your commands had some typographical mistakes (like repeating TEST1= assignment twice and missing $ sign on the echo commands) I am not quite sure where your problem lies. If you can include the output of

grep NUM_RELEASE versao.h

and the exact command/commands that you are trying to run, here in your post, you can get a better answer in my opinion
________________________________
UNIX because I majored in cryptology...
Peter Godron
Honored Contributor

Re: Problems "echoing" variables values

Rafael,
first I assume the second assign of TEST1 should be TEST2.
TEST1="35"
TEST2="d"
echo ${TEST1}${TEST2}
should give you: 35d

Also what shell : echo $0
harry d brown jr
Honored Contributor

Re: Problems "echoing" variables values


It must be getting control characters from versao.h, so try this:

grep NUM_RELEASE versao.h |cut -f5 | od -bc

and post the output here.

live free or die
harry d brown jr
Live Free or Die
Rafael Mendon├зa Braga
Regular Advisor

Re: Problems "echoing" variables values

Harry, here is the output;

$ grep NUM_RELEASE versao.h |cut -f5 | od -bc
0000000 063 065 015 012
3 5 \r \n
0000004


And the simple output:

$ grep NUM_RELEASE versao.h
#define NUM_RELEASE 35
Hein van den Heuvel
Honored Contributor

Re: Problems "echoing" variables values

Rafael,

As others suggested, you are being pestered by control characters.
What is going to your screen is: 3, 5, Carriage-Return ( = \r = 015 ), d
The VISUAL effect of this is d5.
On an oldfashioned hardcopy terminal you would see the overpunch, but screens just give the last character in a given position.

Your input file probably came from a WINDOZE box and has CR-LF as line terminators, not the LF Unix expects.

Just push your file through DOS2UX and all will be well.

Note... the Cut -f5 works because you apparently have TABs in the input. To forum does not display that. It may be more save to parse for 'whitespace', or just pick up the last field for example with awk:

awk '{print $NF}' x

Of course awk (or perl) will also happily remove the CR for you:

awk '{x=$NF; sub(/\r/,"",x); print x }' x | od -bc
0000000 063 065 012
3 5 \n

Hth,
Hein.


$ perl -e 'print "#define NUM_RELEASE 35\r\n"' > x
$ grep NUM x | cut -d' ' -f3 | od -bc
0000000 063 065 015 012
3 5 \r \n
0000004

$ y=d
$ x=`grep NUM_RELEASE x |cut -d' ' -f3`
$ echo $x$y
d5
$ echo $x$y > xx
bash-2.04$ od -bc xx
0000000 063 065 015 144 012
3 5 \r d \n
0000005
Mark Greene_1
Honored Contributor

Re: Problems "echoing" variables values

Instead of "echo $TEST1$TEST2", use "print -r $TEST1$TEST2".

If that still is a problem, do the grep to TEST1 as above, then do this:

TEST3=`print -r $TEST1`

which should give you just the "35" in TEST3 without any control characters.

mark
the future will be a lot like now, only later