1

Can someone please let me know if the following commands should work as I am not sure and am getting error?

Just to let you know that I am using this in one of my nagios scripts:

## GET SWAP Warning and Critical values from the Machine
temp=$(swapon -s | tail -n 1 | awk '{print $3}' ) 
SWAP_WARN=$(echo '$(temp)*.20' | bc) 
SWAP_CRIT=$(echo '$(temp)*.40' | bc)

1 Answer 1

1

You did not post the error message but based on your source I figure issues in the way "temp" variable is dereferenced. You need to use braces instead of parentheses and finally wrap up in double quotes.

Try this.

temp=$(swapon -s | tail -n 1 | awk '{print $3}' )
SWAP_WARN=$(echo "${temp}*.20" | bc)
SWAP_CRIT=$(echo "${temp}*.40" | bc)
5
  • Hi Soumen, I have got the below script and now using the same as advised but it is now reporting error for $ in line one .Can you please suggest... ? The script is below: Jul 17, 2015 at 8:24
  • #!/bin/bash # # Returns the nagios native status codes: # Nagios Status # 0 = OK (SWAP usage below WARNING) # 1 = WARNING (SWAP usage between WARNING AND CRITICAL) # 2 = CRITICAL (SWAP usage higher than CRITICAL) # 3 = UNKNOWN (Wrong usage) PROGNAME=basename $0 PROGPATH=echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' . $PROGPATH/utils.sh SWAP_WARN= SWAP_CRIT= SWAPOUT_ACTIVITY_TEST= ## GET SWAP Warning and Critical values from the Machine temp=$(swapon -s | tail -n 1 | awk '{print $3}' ) SWAP_WARN=$(echo '${temp}*.20' | bc) SWAP_CRIT=$(echo '${temp}*.40' | bc) The error is : Jul 17, 2015 at 8:29
  • The error is (standard_in) 1: illegal character: $ (standard_in) 1: syntax error (standard_in) 1: illegal character: $ (standard_in) 1: syntax error Jul 17, 2015 at 8:29
  • I suggest you post the code in your original description. That way it will come out formatted. In its current form, I cannot make out anything. Thanks.
    – soumen
    Jul 17, 2015 at 11:05
  • Well, I see that you have not used double quotes (") around "temp" expression. Please replace '${temp}*.20' with "${temp}*.20" . Also, replace '${temp}*.40' with "${temp}*.40" . Or, you may choose to copy paste the source from my post.
    – soumen
    Jul 17, 2015 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.