LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2007, 08:01 AM   #1
dziadgba
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Rep: Reputation: Disabled
converting string in integer in bash script


hello everybody,
I tried the following bash script

Code:
 #get number of lines of file nodesAndCoordinates
  lines= wc -l nodesAndCoordinates | gawk  '{print   $1}'

  echo $lines

  echo "done"
  lines=`expr $lines + 1`
  echo $lines
OUTPUT
- 23
-
-done
-1

why does it not give the right value (24)
I suppose that the number 23 is somehow stored in the variable lines as string and not as number. Can that be true? How can I convert variable lines in a number?

any help is appreciated
dzia

Last edited by dziadgba; 06-11-2007 at 06:33 AM.
 
Old 06-10-2007, 08:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

the lines= wc -l....... part isn't syntactically correct, it should be:

lines="`wc -l nodesAndCoordinates | gawk '{print 23}'`"

Besides that, the above line is strange. The output of the wc -l part is discarded (it is given to gawk, but gawk only prints 23) and 23 is printed. I don't know if the output of the wc -l line is actually needed, if not do something like this: lines="23".

Hope this helps.
 
Old 06-11-2007, 06:35 AM   #3
dziadgba
LQ Newbie
 
Registered: Jun 2007
Posts: 2

Original Poster
Rep: Reputation: Disabled
I am sorry: the correct code is

lines= wc -l nodesAndCoordinates | gawk '{print $1}'

and the output would be 23.
The problem however remains the same.
thanks
 
Old 06-11-2007, 06:50 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You are still using the wrong syntax:

It should be: lines=`wc -l nodesAndCoordinates | gawk '{print $1}'`
There should be backticks (`) around the statement.

I always surround a declaration with double quotes. I.e:

lines="`wc -l nodesAndCoordinates | gawk '{print $1}'`"
 
Old 08-31-2009, 05:25 PM   #5
tlinux17
LQ Newbie
 
Registered: Feb 2009
Location: Italy
Distribution: Mandriva - Suse - Fedora - (PcBsd)
Posts: 9

Rep: Reputation: 0
for increase a numeric variable, you can use this sintax
lines=$(($lines + 1))
Ciao
 
Old 08-31-2009, 05:59 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Actually, you don't require the '$' inside the parentheses, in fact you can even do

(( a++ )) # Post-increment 'a', C-style.

http://www.tldp.org/LDP/abs/html/dblparens.html
 
  


Reply

Tags
bash, conversion, integer, string



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
Converting extended ascii (ë,ô) in bash script Hko Programming 4 12-29-2012 03:42 AM
converting bash script to csh DJOtaku Programming 8 02-13-2006 05:35 AM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM
bash script for converting ps to pdf juergenkemeter Linux - General 3 10-10-2005 04:35 PM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration