0

Code below is supposed to check the memory for user and if its greater than 1000, print message I keep getting error- line 4: impala: command not found

#!/bin/bash

while [ true ] ;do
used= `ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" 
"int(a[i]/1024+0.5);}' | grep user`

if [[ $used > 1000 ]]; then
echo "user memory is $used"

fi
sleep 5
done

I have tried used= ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);} | grep user'

and used= 'ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | grep user' I need a fresh eye on this. Please help.

3
  • 2
    remove the space Commented Jun 27, 2017 at 16:09
  • 2
    Remove the space after the equals sign? Commented Jun 27, 2017 at 16:10
  • 1
    Jesus.. Thank you Commented Jun 27, 2017 at 16:10

1 Answer 1

1

In bash, as mentioned [ here ], putting spaces around the equal sign would cause errors, So the right format is

variable_name=value;

Moreover, you may change

while [ true ] 

to

while true

Edit

If used has the form impala 600 and you're only interested in the number at the end, then you may do

used="${used##* }"
#Do this just after the your first command.

Finally do

#use -gt for integer comparisons and > for string comparisons
if ! [ -t $used ] && [ $used -gt 1000 ]
then
  echo "user memory is $used"
fi

Note: Though the syntax errors in the script is resolved there is no guarantee that the program logic is right

Sign up to request clarification or add additional context in comments.

2 Comments

my output from ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | grep impala is impala 600. Im trying to remove "impala" Ive tried ps hax -o rss,user | awk '{a[$2]+=$1;}END{for(i in a)print i" "int(a[i]/1024+0.5);}' | grep impala | sed 's/\D+/g' <<< "$s" with no luck
Thank you. another thing is im still getting output when I shouldnt. since 600 is not > 1000

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.