LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 05-24-2011, 10:55 PM   #1
TuxGeek
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: Disabled
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?
 
Old 05-24-2011, 11:23 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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
 
1 members found this post helpful.
Old 05-24-2011, 11:26 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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/
 
Old 05-25-2011, 04:37 AM   #4
codemaniac
Member
 
Registered: Feb 2010
Location: INDIA
Distribution: Slackware,Suse,Mint
Posts: 39

Rep: Reputation: 17
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))
 
Old 05-25-2011, 04:03 PM   #5
TuxGeek
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-25-2011, 08:42 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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
 
Old 05-25-2011, 08:53 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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.
 
Old 05-25-2011, 10:50 PM   #8
TuxGeek
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you I thought the exit command was needed. The vmstat awk method works. been fun learnin this.
 
  


Reply

Tags
bash, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM


All times are GMT -5. The time now is 12:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration