Unix Script -- Unable to perform division


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Script -- Unable to perform division
# 1  
Old 07-02-2012
Unix Script -- Unable to perform division

I'm new to scripts, i wrote the below script to capture the percentage of FreeMemory available in Linux box. Output of UsedfreeMemory is displayed as '0'.

I'm expecting the output like 0.89 (or) .89 --- ( 0.89 perferable)
Anyone can help me on this pls.

=============================================
Code:
#!/bin/bash

MemTotal=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
echo "MemTotal" $MemTotal

Active=`cat /proc/meminfo | grep "^Active:" | awk '{print $2}'`
echo "Active" $Active

UsedFreeMemory=$[$Active/$MemTotal] 
echo "UsedFreeMemory" $UsedFreeMemory

AvailableFreeMemory=$[100-$UsedFreeMemory]
echo "AvailableFreeMemory" $AvailableFreeMemory

=================================================

Output:
=======
Code:
MemTotal 32824336
Active 26197888
UsedFreeMemory 0
AvailableFreeMemory 100

--- Murali.

Last edited by Franklin52; 07-02-2012 at 10:32 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-02-2012
Try with the below ..
Code:
MemTotal=$(awk '/MemTotal:/ {print $2}' /proc/meminfo)
Active=$(awk '/^Active:/ {print $2}' /proc/meminfo)
UsedFreeMemory=$(echo $Active/$MemTotal | bc -l)
AvailableFreeMemory=$(echo 100-$UsedFreeMemory|bc -l)

# 3  
Old 07-02-2012
UsedFreeMemory=$(echo "scale=2; $Active/$MemTotal" | bc)
# 4  
Old 07-03-2012
Thanks alot Naresh and Jay -- That works Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. Red Hat

Unable to perform yum update

Hi, I run RHEL 5.2 as a server in my lab. Now the issue I am facing is that I am unable to update my machine using yum update. It shows an error of missing dependencies though dependencies are already installed. Please help me to do that. Error screen shot is also attached. (9 Replies)
Discussion started by: Ankur Goyal
9 Replies

3. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

4. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

5. UNIX for Dummies Questions & Answers

UNIX script unable to edit

Hello every one, I am a newbie , I am trying to run the script .sh file , but when I run this, I need this backup file to store at some location called " /A/B " ,but this location should only consist the latest backup file. which mean at any time this location only consists the latest backup... (2 Replies)
Discussion started by: karthik25
2 Replies

6. Solaris

Unable to perform IPMP

Hi, I have perform IPMP on server. but its showing following errors, root@DEVTEST10 # dladm show-dev ce0 link: up speed: 100 Mbps duplex: full ce1 link: up speed: 100 Mbps duplex: full root@DEVTEST10 # root@DEVTEST10 # ifconfig -a lo0: flags=2001000849 mtu 8232 index 1 inet 127.0.0.1... (0 Replies)
Discussion started by: tiger09
0 Replies

7. Shell Programming and Scripting

How to perform floating division in shell script?

I want to perform the below division operation in shell script and round the value. val1=6000 val2=5000 res=val1/val2 ----> 1.2---> Round to 2 Please help. (3 Replies)
Discussion started by: vel4ever
3 Replies

8. Shell Programming and Scripting

Division in bash script

I use this simple command result=$(echo "7 / 9" |bc -l) echo $result .77777777777777777777 .... but I like to get 0.7777777777777777777 How can I do in order to get also the 0 ? How to use code tags when posting data and code samples. (3 Replies)
Discussion started by: emi65
3 Replies

9. HP-UX

Unable To Perform A "Passwordless" SSH Login To A Server

Greetings! I am trying to perform a passwordless SSH login from a HPUX 11.31 client to a HPUX 11.31 server. Whenever I do a "ssh -l root serverA" from the client, I am prompted for a password. Giving the password, I am able to successfully login. However I am trying to accomplish a... (9 Replies)
Discussion started by: Rob Sandifer
9 Replies

10. Solaris

Unable to perform FTP from Window to OpenSolaris

Hi All, I installed OpenSolaris and I try to do FTP from window to opensolaris via WinSCP 3.7.6 then I am getting the following error-- Couldn't agree a client-to-server cipher(available: aes128-ctr,aes192-ctr,aes256-ctr,arcfour128,arcfour256,arcfour)Please provide me your valuable... (3 Replies)
Discussion started by: smartgupta
3 Replies
Login or Register to Ask a Question