![]() |
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? |
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 |
You could add 'set -xv' to debug like this
Code:
#!/bin/bashTry the more modern style of $(cmd) eg Code:
#!/bin/bashhttp://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ |
Quote:
I am not now on a unix now box though can advise you to try the below . Quote:
|
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.
|
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}'Lastly, as Chris pointed out, your exit command is incorrect, although it may just be a typo :) |
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. |
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 04:41 AM. |