Why two numbers not summarized in script? [duplicate] - bash

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" )"

Related

BASH If conditional always returns "True" [duplicate]

This question already has answers here:
How do I do if statement arithmetic in bash?
(5 answers)
When are square brackets required in a Bash if statement?
(3 answers)
Closed 2 years ago.
The code I wrote to solve a challenge keeps giving me a response of "True" and I have no idea why. I've played with the syntax and keep getting "True", so there's probably something I'm not understanding about the way Bash works.
I apologize if this is too simple or if there are similar questions out there. I looked but don't know how to phrase the issue.
The test values input are n=3 x=3 y=4 (says true, should be false) and n=12 x=3 x=4 (does not reach this point of the test.
#!/bin/bash
read n
echo "n is " $n
read x
echo "x is " $x
read y
echo "y is " $y
if [[ ((n%x==0 && n%y==0)) ]]; then
echo "true"
else
echo "false"
fi
Get rid of the unnecessary [[]]. Your new if statement would look like if ((n%x==0 && n%y==0)).

Bash script for loop which the last statement is first calculated [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 3 years ago.
I wrote this bash script
x=64
y=1
ans=$((x-y))
z=`expr $ans`
for i in {1..$z}
do
echo $i
done
Actually, I would like to print i values from 1 to 63, 63 which is first
obtained from the above addition. But it just prints {1..63}
Can someone please help me. Thanks in advance.
You're best of using a C-style for loop:
for ((n=$y; n<$x; n++)); do
echo $n
done
using seq :
for i in $(seq 1 $z)
do
echo $i
done

Brace expansion of {1..$n} [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 4 years ago.
I am trying to loop from 1 to n where n is from user input.
If I do:
read n
echo {1..$n}
I get this output for the input 5
{1..5}
How do I make it expand to
1 2 3 4 5
Keep it simple by trying to do it with a for loop as follows.
echo "enter number..."
read n
for((i=1;i<=n;i++)); do
echo "$i"
done
Or use seq with for loop as follows too.
echo "Enter number:"
read howmany
for i in $(seq 1 $howmany); do
echo "$i";
done
Curly braces don't support variables in bash, though eval could be used but it is evil and have loopholes, why so see this link carefully http://mywiki.wooledge.org/BashFAQ/048

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

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

For loop range in Bash [duplicate]

This question already has answers here:
Sequences expansion and variable in bash [duplicate]
(7 answers)
Closed 7 years ago.
Im trying a code in bash to generate prime nos as follows:
#!/bin/bash
echo "Enter till where u wish to generate"
read num
echo "Generating prime numbers from 2 to $num"
flag="prime"
for i in {2..$num}
do
for j in {2..$((${num}-1))}
do
[ $((${i}%${j})) -eq 0 ] && flag="nprime" || flag="prime"
break
done
[ "$flag" == "prime" ] && echo "$i"
done
Upon execution, it throws an error because the for loop takes the sequence mentioned in the curly braces as it is not as a sequence.
Could you guide me as to where am i going wrong ?
man bash in my version says:
A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer.
You can't use variables in ranges. Try seq instead:
for i in $(seq 2 $num) ; do
Note that incr for seq goes between x and y.
Use:
for ((i=2; i<=$num; i++))

Resources