Solved

bash Variables  with hex 0a on the end

Posted on 2014-11-24
5
318 Views
Last Modified: 2014-12-08
I am getting data in bash and storing it in Variables.

Half the time I get the string, the other half of the time I get the string terminated with hex 0a on the end if I
pipe it to hexedit -C

is there an easy way to strip this from the variable  with a $(varname. ) or something like it ?

I am having this issue a lot

I am using the later version of redhat :)
0
Comment
Question by:TIMFOX123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 40463209
Hi Tim,

Hex 0A is a newline (AKA line-feed) character, which is the standard UNIX/Linux line terminator.

Q1. Are you able to post your code so we can see how that's happening?

> "Half the time I get the string, the other half..."
Q2. When you say "half the time", do you mean in half of the variables, or do you get different results with different data, or what?

> "I am using the later version of redhat :)"
Q3. Later than what?
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 40463288
Which way do you "pipe" the variable to the hex editor? If you use something like

A="string"
echo $A | od -tcx1

you will always see something like this (x"0a" at the end):

0000000    s   t   r   i   n   g  \n
          73  74  72  69  6e  67  0a
0000007

That's because "echo" adds a linefeed automatically.

Instead, if you use the "-n" flag of "echo ("do not output the trailing newline")

A="string"
echo -n $A | od -tcx1

then you'll see this:

0000000    s   t   r   i   n   g
          73  74  72  69  6e  67
0000006

i. e. no trailing x"0a".

I used "od" because my hexedit doesnt support a "-C" flag.

If your variable actually contains a linefeed at the end:

A="string
"
then

echo -n "$A"

will preserve this linefeed, while

echo -n $A

will not.

As tel2 suggested above, please give us more detail!
0
 

Author Comment

by:TIMFOX123
ID: 40467200
thank you both

example, If I:

grep root /etc/passwd | hexdump -C
you will see the line ends with a hex 0a

if you want to get rid of the hex 0a  you can:
 grep root /etc/passwd | tr -d '\n' | hexdump -C

There is a part of bash that I have used but to not understand where you can modify stuff with ${}

what I would like to do is to take a varable that has a hex 0a an the end and when I use it I can "chomp" it off

echo ${varname xxxxxx }
where the xxxx is code that I do not know.

also when I was saying about "later version of Linux" I was not being clear.  I should have said "currently supported" Linux like 5,6,7.  

thank you so much
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 40467273
You see the linefeed just because "grep" adds it.
 The variable itself does not contain x'0a'.

varname=$(grep root /etc/passwd)
echo -n ${varname} | hexdump -C

Try this:

A=1234
echo ${#A}

#varname is the length of the variable varname.

The above will give 4, not 5

Or, if you want "grep"

echo 1234 > testfile
A=$(grep 1234 testfile)
echo ${#A}
4

With "${varname xxxxxx }" you probably mean variable editing (aka "parameter expansion"), using e.g. ${varname%string} (or %%), ${varname#string} (or ##) or ${varname/old/new}.

I'm not aware of a way specifying hex values with the above.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40467363
I could tell you how to strip the last character which might be a linefeed (or might not):

A="1234
"
echo ${#A}
-> 5
A="${A:0:$((${#A}-1))}"
echo ${#A}
-> 4
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux SFTP 8 77
UNIX Script. Send email if failure 8 57
WordPress: Debugging from my Windows 10 Desktop 6 61
Is Fedora an appropriate distro for the environment. 7 88
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question