This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 9 months ago.
I try to write simple script to read temperature:
cpu=$(cat /sys/class/thermal/thermal_zone*/temp)
if [$cpu -ge 50000]; then
echo "OK"
else
echo "Not OK"
fi
echo $cpu is fine: 64000 but script output is:
myscript.sh: 2: [64000: not found
Not OK
What is wrong with line 2?
Thank You.
You want to make a few changes here:
Add a space between the [ and the $cpu, and before the ] (as per the comments above)
Place the $cpu into double quotes, to avoid unexpected results
Handle the possibility of maximum values, by picking up the maximum (or minimum, or average).
cpu="$(cat /sys/class/thermal/thermal_zone*/temp | sort -nr | head -1)"
if [ "$cpu" -ge 50000 ] ; then
echo "OK"
else
echo "NOT"
fi
If you prefer the space-less expressions, you can use the arithmetic expressions
cpu="$(cat /sys/class/thermal/thermal_zone*/temp | sort -nr | head -1)"
if ((cpu>50000)) ; then
echo "OK"
else
echo "NOT"
fi
Related
This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 2 years ago.
I am trying to insert values in to an array in a loop as follows:
branches_to_delete=()
cat branches.txt | while read branch_name
do
BRANCH_COUNT=$(git ls-remote | grep -w $branch_name | wc -l)
if [ $BRANCH_COUNT -eq 0 ]; then
echo "Branch does not exist"
branches_to_delete+=($branch_name)
elif [ $BRANCH_COUNT -eq 1 ]; then
echo "Branch exists"
else
echo "Not valid result"
fi
done
echo "Loop finished"
echo ${branches_to_delete[#]}
But when I printout branches_to_delete it is actually empty.
What am I doing wrong here?
With the pipe from cat branches.txt into the read loop, you create a subshell that cannot access the parent shell's branches_to_delete array.
You can fix this by avoiding the pipe and saving a useless use of cat:
branches_to_delete=()
while read branch_name; do
...
done < branches.txt
(Make sure nothing in ... reads from read's stdin. You'll notice if something is missing).
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.
This question already has answers here:
bash, bc modulo does not work with -l flag
(5 answers)
How do I check whether a variable has an even numeric value?
(3 answers)
Closed 4 years ago.
I haven't been able to find anything related to this nor can my professor explain what's going on. Below is the problem description:
After quite a bit of debugging following is the bash script to print odd or even:
echo $1
odd_even=$(echo "$1 % 2" | bc -l)
echo $odd_even
if [[ $odd_even -eq 0 ]]
then
echo "even"
else
echo "odd"
fi
Following is the output:
$ bash logic_ex2.sh 3
3
0
even
This is weird because the variable odd_even contains 0 while the argument is 3.
We ran the following command to check whats wrong with the echo "3 % 2" | bc -l construction since without using that construction we could get the script working:
$ echo "3 % 2" | bc -l
0
Then we ran bc in the terminal and ran 3 % 2 which gave 1 as the proper output.
Can somebody please explain what is happening here?
Check this explanation as to bc -l calculates a % b differently from plain bc without the -l. The quick solution is to set your scale back to zero:
$ bc -l <<< "scale=0; 3 % 2"
1
But I would probably do this without using bc at all, since bash includes sufficient functionality to calculate integer remainders. If all you need is integer math, bash may be good enough on its own.
#!/usr/bin/env bash
echo "$1"
odd_even=$(($1 % 2))
echo "$odd_even"
if [[ $odd_even -eq 0 ]]; then
echo "even"
else
echo "odd"
fi
My results:
$ bash remtest.sh 3
3
1
odd
$ bash remtest.sh 4
4
0
even
another option:
#!/bin/bash
var=$1
if [[ $((var % 2)) -eq 0 ]];
then echo "$var is even";
else echo "$var is odd";
fi
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 8 years ago.
The result is the one desired; after a bit of trial and error. I don't understand what the "2:-" and "3:-" do/mean. Can someone explain.
#!/bin/bash
pid=$(ps -ef | grep java | awk ' NR ==1 {print $2}')
count=${2:-30} # defaults to 30 times
delay=${3:-10} # defaults to 10 second
mkdir $(date +"%y%m%d")
folder=$(date +"%y%m%d")
while [ $count -gt 0 ]
do
jstack $pid >./"$folder"/jstack.$(date +%H%M%S.%N)
sleep $delay
let count--
echo -n "."
done
It's a parameter expansion, it means if the third argument is null or unset, replace it with what's after :-
$ x=
$ echo ${x:-1}
1
$ echo $x
$
There's also another similar PE that assign the value if the variable is null:
$ x=
$ echo ${x:=1}
1
$ echo $x
1
Check http://wiki.bash-hackers.org/syntax/pe
This question already has answers here:
How can I compare two floating point numbers in Bash?
(22 answers)
Closed 3 years ago.
Hey I would like to convert a string to a number
x="0.80"
#I would like to convert x to 0.80 to compare like such:
if[ $x -gt 0.70 ]; then
echo $x >> you_made_it.txt
fi
Right now I get the error integer expression expected because I am trying to compare a string.
thanks
you can use bc
$ echo "0.8 > 0.7" | bc
1
$ echo "0.8 < 0.7" | bc
0
$ echo ".08 > 0.7" | bc
0
therefore you can check for 0 or 1 in your script.
If your values are guaranteed to be in the same form and range, you can do string comparisons:
if [[ $x > 0.70 ]]
then
echo "It's true"
fi
This will fail if x is ".8" (no leading zero), for example.
However, while Bash doesn't understand decimals, its builtin printf can format them. So you could use that to normalize your values.
$ x=.8
$ x=$(printf %.2 $x)
$ echo $x
0.80
For some reason, this solution appeals to me:
if ! echo "$x $y -p" | dc | grep > /dev/null ^-; then
echo "$x > $y"
else
echo "$x < $y"
fi
You'll need to be sure that $x and $y are valid (eg
contain only numbers and zero or one '.') and,
depending on how old your dc is, you may need to
specify something like '10k' to get it to
recognize non-integer values.
Here is my simple solution:
BaseLine=70.0
if [ $string \> $BaseLine ]
then
echo $string
else
echo "TOO SMALL"
fi
use awk
x="0.80"
y="0.70"
result=$(awk -vx=$x -vy=$y 'BEGIN{ print x>=y?1:0}')
if [ "$result" -eq 1 ];then
echo "x more than y"
fi
The bash language is best characterized as a full-featured macro processor, as such there is no difference between numbers and strings. The problem is that test(1) works on integers.