Home > other >  Comparing value string and fixed value
Comparing value string and fixed value

Time:02-05

I wrote small script under Debian Linux 11 that should check how many instances of application is currently running and what is power usage of GPU cards. I save it under name test , and she is started every time I access instance over SSH

script

clear
echo "*** SYSTEM STATUS ***"
echo
hostname | awk '{print "HOSTNAME: "$1}'
echo
lscpu | grep 'BIOS Model name' | cut -c54-73 | awk '{print "CPU MODEL: Intel I5" $1}'
echo
now="$(date " %d-%m-%Y %H:%M:%S")"
printf "DATE: %s\n" "$now"
echo
cat /proc/meminfo | grep MemTotal | awk '{print"TOTAL MEMORY: "$2/1048576" GB"}'
echo
sensors | grep -A 0 'Package id 0:' | cut -c16-25 | awk '{print "CPU TEMPERATURE: " $1 }'
echo
awk '{print "SYSTEM UP TIME: " int($1/3600)":"int(($1600)/60)":"int($1`)}' /proc/uptime
echo
ip -4 addr show enp3s0 | grep -oP '(?<=inet\s)\d (\.\d ){3}' | awk '{print "IP ADDRESS: " $1 }'
echo
ps aux | awk {'sum =$3;print"CPU USAGE: " (sum/400)*100" %"'} | tail -n 1
echo
worker=$( echo `ps -C genefer22g_linux64_22.12.02 --no-headers | wc -l` )
echo "NUMBER OF WORKERS :" $worker  
if [ $worker = 4 ]; then 
echo
else
echo "WARNING: WORKERS NOT WORKING "   
fi
a=$( echo `nvidia-smi -q -i 0 | grep "Power Draw" | cut -c45-50` )
b=$( echo `nvidia-smi -q -i 1 | grep "Power Draw" | cut -c45-50` )
c=$( echo `nvidia-smi -q -i 2 | grep "Power Draw" | cut -c45-50` )
d=$( echo `nvidia-smi -q -i 3 | grep "Power Draw" | cut -c45-50` )
total=$( echo $a   $b   $c   $d  | bc -l )
power=$150
echo "SYSTEM DRAW :" $total
if [ $total > $power ] ; then          -------------------> problem in this line
echo "WARRNING - SYSTEM DRAW LOW"                                       
else
echo "OK"  
fi
sleep 7
exit

Problem: I tray countless combination of using if then option (line I marked as problem): and sometimes works perfect, sometimes throw many different errors

Sometime script throw error (standard_in) 1: syntax error

if [ $total > $power ] ; then -------------------> problem in this line

What I do wrong?

CodePudding user response:

You could add set -x right before the part you want to debug, which will show you a debug of what is happening in bash. and stop it by inserting after that set x

like:

set -x
power=$150
echo "SYSTEM DRAW :" $total
if [ $total \> $power ] ; then # escape > otherwise it redirects output

I do not think you are setting the value of $150

The script might be failing if one of the compared values is not set.. so you should initialize your variables to be let' say equal to 0 as a default at the beginning of your script, or via bash

like:

power=${150:-10} # if $150 does not have a value or empty, the default value of $power will be set to `10`

CodePudding user response:

So many possible issues but you can compare possibly decimal values using bc:

if [ "$(echo "$total > $power" | bc)" = 1 ]; then

CodePudding user response:

As the comments recommend, use shellcheck, however, I think your intention is not what you wrote.

Try this, create a script (i.e. myscripy)

#! /bin/bash

power=$150
echo "power=$power"

then run it

./myscript abc

and prints

power=abc50

which is probably very different than what you expect.

That is because power will take the first argument's value ($1) and append 50.

If you wanted argument number 150 (also very unlikely), you should write

power=${150}

but if you want just the number

power=150

CodePudding user response:

One problem is that [ (and [[) do string comparisons, and you want a numeric comparison. For that you want to use ((, so something like

if (( $total > 150 )); then
    echo "WARNING..."
else
    echo "OK"
fi

will work better. Otherwise a total of 1000 would print ok, and 90 would give a warning.

Other problems:

  • $150 gives you the value of a variable called 150 -- you probably want to remove the $
  • Outside of special forms like [[ and ((, a > will do an output redirection, rather than being a 'normal' argument to a command such as [.
  • Related