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 I tried this: Code:
count << find . -iname "*A*" | wc -l Can anyone help me? |
You should set variables with equal rather than redirect:
Code:
count=$(find . -iname "*A*" | wc -l) 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) 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" |
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
|
All times are GMT -5. The time now is 02:22 PM. |