LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-05-2011, 01:24 PM   #1
mcfc1900
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
Bash BC problem


My line of code which isn't working is the following:

variable=$(ibase=16; $hex | bc)

I have also tried:

variable=$(echo "ibase=16; $hex" | bc)

Neither will work and gives me the error "(Standard_in) 1: parse error".

$hex has been defined two lines above this piece of code...

Can anyone see where I am going wrong please?

Thanks
 
Old 04-05-2011, 02:38 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
The error message could be caused by the letters in $hex not being in upper case.
Lower case letters are used for variable names in bc.
Code:
hex=45de
echo "ibase=16; $hex" | bc
(standard_in) 1: parse error

hex=45DE
echo "ibase=16; $hex" | bc
17886
 
1 members found this post helpful.
Old 04-05-2011, 02:49 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Question

Quote:
Originally Posted by mcfc1900 View Post
variable=$(ibase=16; $hex | bc)
I can't see how you expect that to work. Assuming $hex holds a valid hexadecimal value, ibase=16; $hex needs to be supplied to bc on its standard input. The | (pipe) implies that some other program will be emitting the program from its standard output.

This works fine for me.

Code:
foo$ hex=10
foo$ variable=$(echo "ibase=16; $hex" | bc)
foo$ echo $variable
16
foo$
I think we will need to see more of your program to determine the cause of your error.

Edit
Quote:
Originally Posted by Kenhelm View Post
The error message could be caused by the letters in $hex not being in upper case.
Lower case letters are used for variable names in bc.
Nice catch!

Last edited by Telengard; 04-05-2011 at 02:52 PM.
 
Old 04-05-2011, 03:52 PM   #4
mcfc1900
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you both

The problem was that the hex value was lower case, soon as I changed that it started working.

Cheers
 
Old 04-05-2011, 03:57 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by mcfc1900 View Post
variable=$(echo "ibase=16; $hex" | bc)
Why not use
Code:
variable=$[0x$hex]
 
  


Reply

Tags
bash, bc, command substitution, pipe


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
a problem about bash naihe2010 Linux - Enterprise 1 10-15-2005 11:06 AM
Problem with BASH Pheidon Linux From Scratch 5 06-24-2005 02:34 AM
bash problem cL4YmAN Linux - Newbie 1 06-07-2004 01:32 PM


All times are GMT -5. The time now is 07:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration