Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:28:50+00:00 2026-06-10T06:28:50+00:00

I am writing the top command output to a text file. I am trying

  • 0

I am writing the top command output to a text file.
I am trying to write a simple bash script to calculate the percentage of used memory and send an
email if the memory used percentage exceeds, say 90%.

Here is the bash script I have thus far.

#!/bin/bash
top -n 1 -b | grep "Mem" > /home/modadm/top-output.txt
MAXMEM=/home/modadm/top-output.txt | grep "Mem" | cut -c 7-14 
USEDMEM=/home/modadm/top-output.txt | grep "Mem" | cut -c 25-31
$USEDPCT='echo $USEDMEM / $MAXMEM * 100 | bc'
$USEDPCT | mail -s "Test Email from MOD Server" test@test.com

When I save and execute the script, I get the error “No such file or directory”:

-bash-3.2$ ./memcheck.sh
./memcheck.sh: line 4: =echo $USEDMEM / $MAXMEM * 100 | bc: No such file or directory
Null message body; hope that's ok
-bash-3.2$ 

Can someone assist? I am a newbie to bash scripting and this is my first script.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:28:51+00:00Added an answer on June 10, 2026 at 6:28 am

    You have a few problems here.

    First, this doesn’t do what you want it to do.

    USEDMEM=/home/modadm/top-output.txt | grep "Mem" | cut -c 25-31
    

    You can’t pipe a filename into a command. You actually want to pipe the contents of the file into the command. You can do that with ‘cat’. However, grep is actually designed to search within a file so you can do

    USEDMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 25-31)
    

    Note that $(cmd) is how you execute a command in a subshell. i.e., you can run some commands to compute the value of a variable in your script. You can also use `cmd` (backticks; usually on the tilde key) but that syntax is less clear.

    Again, you probably want to calculate this result in a subshell. Also, don’t use $ when assigning to variables.

    $USEDPCT='echo $USEDMEM / $MAXMEM * 100 | bc'
    

    This can be rewritten as

    USEDPCT=$(echo "scale=3; $USEDMEM / $MAXMEM * 100" | bc)
    

    Finally, you want to pipe the contents of the variable into the mail program. The pipe is expecting a program to be on the left hand side. You do this by echo’ing the value of the variable into the pipe.

    echo "$USEDPCT" | mail -s "Test Email from MOD Server" test@test.com
    

    To put everything back together:

    #!/bin/bash
    top -n 1 -b | grep "Mem" > /home/modadm/top-output.txt
    MAXMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 7-14)
    USEDMEM=$(grep "Mem" /home/modadm/top-output.txt | cut -c 25-31)
    USEDPCT=$(echo "$USEDMEM / $MAXMEM * 100" | bc -l)
    echo "$USEDPCT" | mail -s "Test Email from MOD Server" test@test.com
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple way (i.e. without writing a script or elaborate keymap sequence)
Hi I am writing a small jQuery script which will toggle a hidden Top-Panel
Writing an HTTP simple server on top of Net node.js module, not using HTTP
I'm writing a simple forum in ASP.NET that sits on top of an Entity
So I was writing some queries today and was using top 10 on a
I am writing a Chrome Extension that inserts a DIV on top of an
I am writing a web project that uses Spring Framework 2.5 on top of
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I am trying to dynamically write some xml for a webservice. I have recently
I'm writing a quick server app for something so don't really want to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.