How to Compare load average with a threshold value [duplicate] - bash

This question already has answers here:
Floating point comparison with variable in bash [duplicate]
(2 answers)
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 5 years ago.
I am trying to compare load average with a threshold value using shell script as i am new to it, i am able to calculate the load but the script is giving me bad number error during the condition and my condition is not working. here is my script
#!/bin/sh
LOGFILE=/root/sy.log
WHOLEFILE=/root/sys.log
while sleep 1;
do
TOP="$(top -n1)"
CPU="$(cat /proc/loadavg| awk 'BEGIN{t1=t2=t3=0}{t1+=$1;t2+=$2;t3+=$3;} END {print (t1+t2+t3)/3}')"
echo $CPU >> $LOGFILE
CPU=`printf "%d" $CPU`
Threshold=0.2
Threshold=`printf "%d" $Threshold`
if [[ "$CPU" -ge "$Threshold" ]] ;
then
echo $TOP >> $WHOLEFILE
fi
done

Related

How to only accept input that is a valid index of an array in bash [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Difference between single and double square brackets in Bash
(7 answers)
Why is "[[ 10 < 2 ]]" true when comparing numbers in bash? [duplicate]
(1 answer)
Closed 6 months ago.
I am trying some simple input validation in bash. Basically this part of my script outputs "press [index] to select [array element]." However I cannot seem to get it to stop and exit gracefully when an invalid input is entered. From my research so far this SHOULD work:
declare -a scenarios=()
scenarios+=("Scenario_123")
scenarios+=("Scenario_456")
scenarios+=("Scenario_789")
for i in ${!scenarios[#]}; do
echo -e "select $i for ${scenarios[$i]}"
done
read ScenInd
echo ${#scenarios[#]} #[${ScenInd}=~^[0-9]+$, =~ ^[[:digit:]]+
if ! [${ScenInd}=~ ^[[:digit:]]+$ ] || [${ScenInd} < 0] || [${ScenInd} >= ${#scenarios[#]}];
then
echo "INVALID SELECTION"
exit
fi
but when I run it and enter 8, I get '[8=~: command not found'
What have I done wrong and how do I fix this? I have tried this both with [${ScenInd}=~^[0-9]+$ and =~ ^[[:digit:]]+ yet the results are the same.
Thanks in advance

Comparing two variables in IF condition inside While loop in bash script [duplicate]

This question already has answers here:
Bash comparison operator always true
(1 answer)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 2 years ago.
i am trying to execute a IF condition inside a while loop, But the IF condition isn't working as variables aren't expanding! kindly guide me through the proper way to compare two variables in IF condition
Note - if you can see the error log - DATE was expanding thou! problem with mdate
DATE=`date +"%Y-%m-%d"`
cat path/temp_b | while read file
do
echo 'phase2'
mtime=$(stat -c '%y' $Src_Dir/$file)
echo $mtime
mdate= echo $mtime | cut -d ' ' -f1
echo $mdate
echo $DATE
if ["$mdate"=="$DATE"]; then
"$file" > path/tempc
else
echo 'hi'
fi
done
**Error log -
phase2
2020-05-07 05:22:28.000000000 -0400
2020-05-07
2020-07-21
./test1.ksh: line 37: [==2020-07-21]: command not found
hi**
Change your if statement like:
if [ "$mdate" == "$DATE" ]; then
Explanation: if in bash needs to have square brackets, operator, and operand to be space-separated.

Why two numbers not summarized in script? [duplicate]

This question already has answers here:
How do I echo a sum of a variable and a number?
(9 answers)
How can I do basic maths in bash?
(4 answers)
How do I print the result of a command with 'echo'?
(1 answer)
Closed 4 years ago.
I am newer in bash.
I try to create script that summarizetwo numbers:
Here is script:
echo "the result is:" expr $1+$2
Here how I call the script:
./scr3 50 98
And here is result that I get after the script executed:
the result is: expr 50+98
While I get the the string with two summarized numbers I expect to get the summarize of two numbers.
My question is why I don't get the result of summarize of the two numbers?
Why? Because echo prints its arguments, and you're passing expr as an argument.
A best-practice alternative would be:
echo "The result is: $(( $1 + $2 ))"
...though the a smaller change (albeit to very inefficient code; expr is an artifact of the 1970s, made irrelevant with the introduction of $(( )) in the 1992 POSIX sh standard, and should never be used in new development) is simply:
echo "The result is: $( expr "$1" + "$2" )"

How to loop an argument value in bash [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I want to loop over the value of an input argument, something like that
for i in {0..$1} do
echo $i
done
If i call my script: ./my.sh 2
I want
0
1
But i get
{0..2}
How can i do it ?
#!/usr/bin/env bash
last=$(("$1"-1))
for i in $(seq 0 "$last");
do echo "$i";
done

Shell scripting while loop error [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
#!/bin/bash
a=0
while ["$a" -lt 10]
do
a=`expr "$a" + 1`
echo $a
done
This is the error:
a.sh: 3: a.sh: [0: not found
you need to keep spaces around your brackets:
while [ "$a" -lt 10 ]
since bash interpretes strings; it uses spaces to separate words. When you write ["$a"; then bash reads: [0 (after $a is replaced with 0)

Resources