LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script help (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-help-882555/)

TuxGeek 05-24-2011 11:55 PM

Bash script help
 
Hi I would like to create a bash script that gives the current ram use like conky shows so i can run it in my prompt. My problem is i cant get it to work right. What i have may be a rudimentary method but here it is.

#!/bin/bash
FREE='vmstat | grep 3 | cut -c 14-18'
CACHED='vmstat | grep 3 | cut -c 27-32'

$FREE + $CACHED
exit0


What did i do wrong in my script?

grail 05-25-2011 12:23 AM

Firstly, please use [code][/code] tags around your code.

Next, please explain what you were trying to do with the following line:
Code:

$FREE + $CACHED

chrism01 05-25-2011 12:26 AM

You could add 'set -xv' to debug like this
Code:

#!/bin/bash
set -xv

FREE='vmstat | grep 3 | cut -c 14-18'
CACHED='vmstat | grep 3 | cut -c 27-32'

$FREE + $CACHED
exit0

but the first thing that comes to mind is you are using single quotes (') instead of backquotes (`). Visually easy to mix up.
Try the more modern style of $(cmd) eg
Code:

#!/bin/bash
set -xv

FREE=$(vmstat | grep 3 | cut -c 14-18)
CACHED=$(vmstat | grep 3 | cut -c 27-32)

$FREE + $CACHED
exit0

The last 2 lines also have issues; try these references

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

codemaniac 05-25-2011 05:37 AM

Quote:

"$FREE + $CACHED"
this is confusing to bash !

I am not now on a unix now box though can advise you to try the below .

Quote:

mem=(( $FREE + $CACHED))

TuxGeek 05-25-2011 05:03 PM

Thankyou all for your help. I was trying to get the ram to display by what i thought was aliases for the 2 at the bottom. Im trying to show the ram used in my bash PS1 prompt. Nothing has worked right so far but Im really new to any kind of programing. Most ive done is one java program.

grail 05-25-2011 09:42 PM

Well my first point would be that your cut commands don't actually return the right values (at least not for me). I am guessing that as the fields can
fluctuate in size this is not an accurate method. As the fields are white space delimited may I suggest you either use the -d and -f options in cut or
maybe try awk to retrieve the data:
Code:

vmstat | awk 'NR == 3{print $4 + $6}'
I would also suggest you have a look at the file /proc/meminfo as It contains a lot of well named and formatted data.

Lastly, as Chris pointed out, your exit command is incorrect, although it may just be a typo :)

chrism01 05-25-2011 09:53 PM

To extend on the 'exit0' comment, by default a shell script exits with the status value of the last cmd run.
Unless you are doing error checking & status setting in the file and then checking the final status from outside, there's no point in using the exit cmd.

TuxGeek 05-25-2011 11:50 PM

Thank you I thought the exit command was needed. The vmstat awk method works. been fun learnin this.


All times are GMT -5. The time now is 11:13 PM.