LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-22-2010, 12:32 PM   #1
dave247
Member
 
Registered: May 2004
Posts: 206

Rep: Reputation: 30
inputting values into variables for math from the command line?


Hey I am working with shell scripting. I'm using bash under Debian 5.

I am trying to do simple math to find a percentage of files in a folder with a certain name. Anyway to simplify the explanation of what I am looking for, lets say im trying to find the percentage of files with the letter "A" in the name within a folder.

What I thought of to do is this, which finds all the files with "A" in the name, then counts the output lines which gives me the total number of files:
Code:
find . -iname "*A*" | wc -l
This is the only way I can think of to get that number. Now I want to work with that number by dividing it by the total number of files in that directory, which is not a static number; but for this case lets say its 50 files. How can I input that number to a variable that I can use to do the math?

I tried this:
Code:
count << find . -iname "*A*" | wc -l
and then I go into the programming functions of bash where I can write more lines of code, but thats the point I get lost.

Can anyone help me?
 
Old 10-22-2010, 01:05 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You should set variables with equal rather than redirect:
Code:
count=$(find . -iname "*A*" | wc -l)
The above sets count variable to the result of the command line in the parentheses. (It could also be done by using backticks instead and no dollar sign but many say that shouldn't be used any more.)

bash and most shells have built in math functions but typically don't do floating point so may not round where you want. I usually use the bc (binary calculator) command for math unless it is simple addition or subtraction.

So to determine 50% of the number I'd do something like:
Code:
halfcount=$(/bin/echo $count \* .5 |bc -l)
You need the backslash in front of the asterisk (which is multiplication in bc) to prevent it from being seen as a meta character matching all files. The "-l" option of bc tells it to do floating point output.

The above doesn't actually output the value but rather sets a new variable called halfcount. To see the value you simply do "echo $halfcount"

Last edited by MensaWater; 10-22-2010 at 01:06 PM.
 
Old 10-22-2010, 01:50 PM   #3
dave247
Member
 
Registered: May 2004
Posts: 206

Original Poster
Rep: Reputation: 30
wow thanks Mensa, that looks helpful. i will have to toy with that more when I get home from work tonight. i appreciate the help
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to access command line variables from awk puth Programming 5 02-12-2010 12:06 PM
how to write command line arguments as values, into a file which contains variables sathiawathi.m Linux - Newbie 2 07-17-2009 02:27 AM
Adding values to a file and making a graph with it by command line (Spreadsheet?) Romanus81 Programming 4 07-02-2008 09:14 AM
Adding values on command line.. 3saul Linux - Software 1 03-06-2006 03:01 AM
Streaming in values without defined command line arguments liguorir Linux - Software 1 05-07-2004 02:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:53 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