LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-07-2009, 11:51 PM   #1
shurjo
LQ Newbie
 
Registered: Jan 2009
Location: Rockville, MD
Distribution: CentOS
Posts: 3

Rep: Reputation: 0
bash shell script question


Hi,

I'm trying to use a shell script to (i)multiply the values in the second and third columns of each line in a file, (ii)store that in a variable and (iii) write the value of the first column and the value of the variable to a new file. Like so:

A1BG 3.386 0.64
NAT2 1.276 0.00
ADA 1.532 4.62
CDH2 4.108 59.72
AKT3 3.856 1.27

would become in the output file

A1BG 2.167
NAT2 0.00
ADA 7.078
CDH2 245.330
AKT3 4.897

Here's my take on it:

#!/bin/sh
#sh.readcounts
echo Enter file name:
read fname
while read line
do
echo $1
$2 * $3 = $readcounts
echo $readcounts
done >> "readcounts.out"

I'm pretty sure I goofed up in multiple ways (in case you can't tell already, this is my first shell script) and would be grateful for any advice. Thanks in anticipation, and wish everyone a Happy New Year 2009.
 
Old 01-08-2009, 12:38 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Here's a start: bash can't do non-integer arithmetic (natively), call 'bc' as here : http://www.faqs.org/docs/abs/HTML/mathc.html
Also, assignment is right to left, IOW

result=a*b

Here's a really good bash guide: http://www.tldp.org/LDP/abs/html/index.html
 
Old 01-08-2009, 09:46 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
I you don't have to use pure Bash then awk will do this very simply - to convert the file startfile to a file endfile:

Code:
awk '{ print $1,$2 * $3}' startfile > endfile
 
Old 01-08-2009, 10:26 AM   #4
shurjo
LQ Newbie
 
Registered: Jan 2009
Location: Rockville, MD
Distribution: CentOS
Posts: 3

Original Poster
Rep: Reputation: 0
My sincere thanks to both of you. I learned two new things today: cool!
 
Old 01-09-2009, 10:07 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
http://www.perl.org ...

Use the right tool for the job. When you have a cornucopia of general-purpose text manipulation languages available in the Linux environment ... which you do not have in Windows and therefore might not be used to ... there's frankly very little reason (if any, IMHO) to use "shell scripts."

Continuing most-briefly on this soapbox, "bash scripting may be fine for stringing simple commands together," and it certainly can be used to do many things. Well, you can probably open a can of sardines with a (non-Swiss Army) pocket knife, too... but you're likely to cut your hand while doing it, and meanwhile your camping partner who thought to bring a can-opener with him has already finished eating.

The Unix/Linux environment was first conceived-of in the 1970's by "tool makers," and it can take years to discover the richness of tools that are "right there, free." Now, I'm not a Perl fanboy, but I know a good idea when I see one, and Perl is a prime example of this kind of reasoning. A guy named Larry Wall, who had a problem more-or-less exactly like yours, found an existing tool (called awk ... and which you also have at your disposal...) to be deficient. So, he wrote a better tool and dubbed it "perl." He put what he thought needed to be in it, into it, into it. Since then, a whole bunch of other nerds, also with problems to solve, ripped out what seemed to be bad-parts and put what they thought needed to be in it, into it. They're all still busy at it.

So the bottom line is that, today, you've got this incredibly powerful tool right at your fingertips, with literally millions of lines of well-tested software that you can just plunk right in to whatever you happen to be doing. And, this being Unix/Linux, there are so many other powerful tools to choose from ... Ruby, PHP, Python, Scheme ... all free.

So... don't monkey around with "bash scripting" too much. Learn how to use it competently, to be sure, but also bear in mind that it not designed to be a general-purpose programming language.
 
Old 01-12-2009, 12:22 AM   #6
shurjo
LQ Newbie
 
Registered: Jan 2009
Location: Rockville, MD
Distribution: CentOS
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks, sundialsvcs. I am plugging away at my Perl book, one chapter at a time, and hopefully by the end of this month I should be able to start using it for real-life things instead of the excercises.
 
Old 01-12-2009, 04:03 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by sundialsvcs View Post
http://www.perl.org ...

Use the right tool for the job. When you have a cornucopia of general-purpose text manipulation languages available in the Linux environment ... which you do not have in Windows and therefore might not be used to ... there's frankly very little reason (if any, IMHO) to use "shell scripts."

... (preaching clipped)

So... don't monkey around with "bash scripting" too much. Learn how to use it competently, to be sure, but also bear in mind that it not designed to be a general-purpose programming language.
Actually bash was designed to be a rather general-purpose scripting language.

I think you are leaving out the issue of portability. You need to think about your users before you go out and use all those languages to make a Frankenstein monster with hundreds of dependencies and maintainability issues.

Contrary to what you might think there exist general-purpose languages, and most languages can fit multiple roles just fine. So, quit your preaching.
 
Old 01-12-2009, 04:26 AM   #8
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

Rep: Reputation: 17
For this specific example the previous answer should do, but if you appreciate an extra tip here it is

For simple arithmetic the program expr should be a bit faster than bc. Invoke it like this:
Code:
result=`expr $2 * $3`
Don't forget to put spaces before and after the asterisk.

However the awk solution posted by bgeddy may be even faster since it won't start a separate process for each calculation.
 
  


Reply



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
bash shell script CheeSen Programming 4 09-07-2008 10:02 AM
Bash shell script question yao_man Linux - Newbie 18 05-19-2008 07:17 AM
help with bash shell script !! taiwf Linux - Newbie 5 06-11-2006 06:07 PM
bash shell script question noisewater Programming 3 11-14-2005 08:39 PM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 AM.

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