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 7724105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:36:35+00:00 2026-06-01T04:36:35+00:00

How can i comapre the CPU usage value using shell script, i get a

  • 0

How can i comapre the CPU usage value using shell script, i get a error as [: =: unary operator expected at the line if [ $message -ne "" ]

   #!/bin/sh

    expected_cpuusage="95"
    cpu_usage=`top -n 1 -b|grep Cpu|awk '{print $2}'|cut -d"%" -f1""`
    message=""
    if [ $cpu_usage -gt $expected_cpuusage ]    ##{x%?}
    then
       echo "CPU usage exceeded";
       if [ $message -ne "" ]
       then
         message="$message\n\nCPU usage exceeded configured usage limit \nCurrent CPU usage is $cpu_usage\nConfigured CPU usage is $expected_cpuusage";
       else
         message="CPU usage exceeded configured usage limit \nCurrent CPU usage is $cpu_usage\nConfigured CPU usage is $expected_cpuusage";
       fi ;
    fi
  • 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-01T04:36:36+00:00Added an answer on June 1, 2026 at 4:36 am

    > is a redirection operator. You want -gt

    if [ $cpu_usage -gt $expected_cpuusage ]
    

    This requires that $cpu_usage look like an integer and will fail and generate an error if it does not. (ie, if it contains characters other than 0-9. -gt does not compare floating point values, so strings that contain ‘.’ will not work.) For floating point comparisons, use expr:

    if expr $cpu_usage \> $expected_cpuusage > /dev/null; then 
    

    There are slicker ways to do this comparison using bash builtins like [[, but these constructs limit the portability of the script.
    The other error you are seeing occurs when you use an empty string. Try using quotes:

    if [ "$message" -ne "" ]
    

    but it is much clearer to use:

    if [ -n "$message" ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out the problem in the following short script which should compare
How can I be sure the data that is written by multiple CPU cores
I have a need to get the number of CPU cycles used by a
I'm having a funny issue with map_async that i can't figure out. I'm using
I know that using Applescript I can change the size and position of any
How can I compare the dates only, not the time. I am using NSDateFormatter
I'm looking for a way to measure the cpu usage for different methods in
I'm getting an weird error using queues/multiprocessing. I basically have a function that takes
I'm using the Perl mod XML::SemanticDiff which can compare two XML documents. I want
How can I compare dates using where statement in sqlite? I don't know how

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.