1

I have following code that prints

  1. Disk Usage: 78/223GB (35%)
  2. CPU Load: 2.00
  3. Memory Usage: 5650/16302MB(34.66)

But I want to use out as variables in bah script insated of printing them. How can i do that ?

What I want:

#!/bin/sh

# I want 3 variables which have values from awk 
# How can i convert awk command below to do that ?
$disk_usage
$CPU_usage
$Memory_usage

CODE:

awk 'BEGIN {
    while("df -hP " | getline) {
        if ( $NF == "/" ) {
            printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
        }
    }
    while( getline  < "/proc/loadavg" ) {
        printf "CPU Load: %.2f\n", $(NF-2)
    }

    while( "free -m"| getline) {
        if( $0 ~ /Mem:/) {
        printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
        }
    }
}'

3 Answers 3

3

with bash

#!/usr/bin/env bash

disk=
cpu_usage=
memory_usage=

while true; do
  read -r disk
  read -r cpu_usage
  read -r memory_usage
  break
done < <(awk -f tst.awk)

printf '%s\n' "$disk" "$cpu_usage" "$memory_usage"

where tst.awk

BEGIN {
    while("df -hP " | getline) {
       if ( $NF == "/" ) {
          printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
       }
    }
    while( getline  < "/proc/loadavg" ) {
       printf "CPU Load: %.2f\n", $(NF-2)
    }

    while( "free -m"| getline) {
      if( $0 ~ /Mem:/) {
       printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
       }
    }
}

Instead of a separate script for awk, one can add the awk code inside the Process substitution.

done < <(awk 'BEGIN {
    while("df -hP " | getline) {
       if ( $NF == "/" ) {
       printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
       }
    }
    while( getline  < "/proc/loadavg" ) {
       printf "CPU Load: %.2f\n", $(NF-2)
    }

    while( "free -m"| getline) {
       if( $0 ~ /Mem:/) {
       printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
       }
    }
  }'
)

Without the while loop, using { } for command grouping.


{
  read -r disk_usage
  read -r cpu_usage
  read -r memory_usage
} < <( awk 'BEGIN {
  while("df -hP " | getline) {
   if ( $NF == "/" ) {
      printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
    }
  }
   while( getline  < "/proc/loadavg" ) {
     printf "CPU Load: %.2f\n", $(NF-2)
  }
  while( "free -m"| getline) {
     if( $0 ~ /Mem:/) {
       printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
     }
   }
  }'
)

printf '%s\n' "$disk_usage" "$cpu_usage" "$memory_usage"
Sign up to request clarification or add additional context in comments.

3 Comments

Thankx. What i wanted. Can I make it a single .sh file insated of separate tst.awk
Yes, You can embed the awk code inside the Process substitution.
I seem to have issue embeeding it. can you share how ?
2

Could you please try following, rather than using bash commands(multiple commands) in single awk you could make it easy by making their different sections of commands like:

cat script.bash
#!/bin/bash
FILESYSTEM="/"
##For disk space
DISK_USAGE=$(df -hP "$FILESYSTEM" | awk '{printf("Disk Usage: %s %s\n",$3,$2,$5)}')
##For Cpur
CPU_DETAILS=$(awk '{printf "CPU Load: %.2f\n", $(NF-2)}' /proc/loadavg)
##For Memory
MEMORY_DETAILS=$(free -m | awk '/Mem/{printf("Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2)}')

Comments

1

This will define 3 variables and print to screen:

#!/bin/bash

DISK_USAGE=$(awk 'BEGIN {
    while("df -hP " | getline) {
        if ( $NF == "/" ) {
            printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
        }
    }
}')

CPU_LOAD=$(awk 'BEGIN {
    while( getline  < "/proc/loadavg" ) {
        printf "CPU Load: %.2f\n", $(NF-2)
    }
}')

MEMORY_USAGE=$(awk 'BEGIN {
    while( "free -m"| getline) {
        if( $0 ~ /Mem:/) {
        printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
        }
    }
}')

echo $DISK_USAGE
echo $CPU_LOAD
echo $MEMORY_USAGE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.