Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash or Shell Script does not handle floating point values. It can handle only integers. bc is Basic Calculator and in itself is a programming language. Use scale with it as shown above.
I could acheive this as little bit advanced. Thanks for the commands, Since I am not an expert in shell scripting I don't know how to minimize the arrays, but working great!
Code:
# cat check_usage
#!/bin/bash
sum=0
while read num; do
sum=$(($sum + $num));
done < isp1_duration
###Print the total usage in seconds###
echo " =================="
echo " || ISP1 Details ||"
echo " =================="
echo "Total usage in seconds : $sum seconds of usage."
###Check if duration is equal/greater than 1 minute###
if [ $sum -ge 60 ]; then
minutes=`echo "scale=2;$sum/60" | bc`
only_minutes=`echo "scale=2;$sum/60" | bc | cut -d. -f 1`
seconds_per_minute=`echo "scale=2;$only_minutes*60" | bc`
remaining_seconds=`echo "scale=2;$sum-$seconds_per_minute" | bc`
echo "Usage in minutes and seconds: $only_minutes minutes $remaining_seconds seconds of usage."
fi
###Check if duration is equal/greater than 1 hour###
if [ $sum -ge 3600 ]; then
hours=`echo "scale=2;$sum/60/60" | bc`
only_hours=`echo $hours | cut -d. -f 1`
minutes_per_hour=`echo "scale=2;$only_hours*60" | bc`
remaining_minutes=`echo "scale=2;$sum/60-$minutes_per_hour" | bc | cut -d. -f 1`
echo "Usage in hours and minutes : $only_hours hours $remaining_minutes minutes of usage."
fi
###Check if duration is equal/greater than 1 day###
if [ $sum -ge 86400 ]; then
days=`echo "scale=2;$sum/60/60/24" | bc`
only_days=`echo $days | cut -d. -f 1`
hours_per_day=`echo "scale=2;$only_days*24" | bc`
remaining_hours=`echo "scale=2;$sum/60/60-$hours_per_day" | bc | cut -d. -f 1`
echo "Usage in days and hours : $only_days days $remaining_hours hours of usage."
fi
See the output
Code:
# bash check_usage
==================
|| ISP1 Details ||
==================
Total usage in seconds : 2427773 seconds of usage.
Usage in minutes and seconds: 40462 minutes 53 seconds of usage.
Usage in hours and minutes : 674 hours 22 minutes of usage.
Usage in days and hours : 28 days 2 hours of usage.
The above output is very good than that which I had previously expected i.e
.
Please guide me if anything needs to be modified to minimize the commands/arrays. At last one more thing I would like to print as below only if all conditions(seconds,minutes,hours,days) matches
Code:
Total usage details : 28 days 2 hours * minutes * seconds of usage.
Once again thanks a ton for your kind guidense.
Last edited by niharikaananth; 08-09-2012 at 01:57 PM.
The above arrays is very good instead of my multiple arrays and I am getting the output that what I had really expected!
But still I am unable to calculate and get the correct output using GUI calculator.
So Could you please show me the steps to calculate using GUI calcalator to get the correct output as 2 hours, 22 minutes and 53 seconds for above 2 arrays?
Thanks for your kind help.
Last edited by niharikaananth; 08-09-2012 at 10:38 PM.
The above arrays is very good instead of my multiple arrays and I am getting the output that what I had really expected!
But still I am unable to calculate and get the correct output using GUI calculator.
So Could you please show me the steps to calculate using GUI calcalator to get the correct output as 2 hours, 22 minutes and 53 seconds for above 2 arrays?
Thanks for your kind help.
well, that's called math and is quite simple (once you know it)
It goes like:
Code:
sum=2427773
ELAPSEDHRS=$((($sum%86400/3600)))
expands to:
2427773/86400= 28,099224537037037037037037037037
now "%" (modulo division, prints out the remainder from float number - takes integer part away)
in bash takes the integer 28 away and you get "0,099224537037037037037037037037"
which you then multiply with 86400 to get the "8573"
this 8573/3600 gives "2,381388888..." and again it takes only the integer part "2"
which prints out.
ELSECNDS=$(($sum%3600))
this is the same (% = modulo division)
2427773/3600=674,381388888... - 674 = 0,381388888 * 3600= 1373
you are basically "converting" the "1373" to minutes and remaining seconds... just the logic in BASH is needed to understand what "/" and "%" print out in BASH (here is an example and here the operators in bash )
For example:
Code:
$ echo $((2427773/86400))
28
the "/" (division operator) prints out only integer part of number
How?
I had really confused and got headache by thinking of it with GUI calculator and at last you could solve the problems by giving good explainations. And also after reading those links I came to know how it is happening.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.