LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-04-2022, 07:07 PM   #1
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Rep: Reputation: 0
Get free memory numbers as variables in script


I want to use all number outputs from
Code:
free -m
as variables in a script.

My script looks like this

Code:
#!/bin/bash

READ_MEM=$(free -m)
USED_MEM_VALUE=$READ_MEM (|awk '{print $9}')
echo $READ_MEM
echo $USED_MEM_VALUE
but the output is as follows

Code:
./mem.sh
./mem.sh: line 4: READ_MEM: command not found
total used free shared buff/cache available Mem: 1845 287 322 3 1235 1370 Swap: 0 0 0
What am I doing wrong? (i.e. the value of USED_MEM_VALUE should be 287 in this example, but I get an empty line)
 
Old 12-04-2022, 07:35 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
There must be a million ways to get that value, that would be one of the more esoteric.

You want to use the value of READ_MEM as input - just like you use it in the next line.
 
Old 12-04-2022, 08:17 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
And that would be...

Code:
USED_MEM_VALUE=$(echo $READ_MEM | awk '{print $9}')
 
1 members found this post helpful.
Old 12-04-2022, 09:17 PM   #4
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
And that would be...

Code:
USED_MEM_VALUE=$(echo $READ_MEM | awk '{print $9}')
Thanks!
 
Old 12-06-2022, 12:39 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You can get all values in one go
Code:
#!/bin/bash
{
read x
read x memtotal memused memfree memshared membuf memavail x
read x swptotal swpused swpfree x
} < <( free -m )
echo "memused $memused"
Each read command reads one line.
Each field reads into a variable. Extraneous fields are read into the last variable.

The <( ) is a "process substitution"; the { code } runs in the main shell.

In contrast, a pipe:
Code:
free -m |
{
read x
read x memtotal memused memfree memshared membuf memavail x
read x swptotal swpused swpfree x
echo "memused $memused"
}
echo "memused $memused"
Here the { code } runs in a subshell; the variables are not copied back to the main shell.

Last edited by MadeInGermany; 12-08-2022 at 12:48 PM.
 
2 members found this post helpful.
  


Reply

Tags
script



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 09:51 AM
manipulating 64 bit numbers using 32 bit numbers. rajesh_b Programming 3 09-15-2006 09:03 AM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration