How to use variable as range upper bound [duplicate] - bash

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 have the following problem in a bash script. I'm trying to use a variable N as the upper bound in a simple range, like
#!/bin/bash
N=10
for n in {1..$N};
do
echo $n
done;
However, instead of displaying each number in the loop, the code above literally displays {1..10}. If I change $N to its value, i.e. 10, things work as expected. How can I overcome this and use the variable as the upper limit of the range?

You can't do this, instead, do that :
n=10
for ((i=0; i<=n; i++)); do
echo $i
done
or :
n=10
for i in $(seq 0 $n); do
echo $i
done

Related

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

For loop over sequence of large numbers in Bash [duplicate]

This question already has answers here:
Bash command line and input limit
(4 answers)
Closed 4 years ago.
In a Bash script I am using a simple for loop, that looks like:
for i in $(seq 1 1 500); do
echo $i
done
This for loop works fine. However, when I would like to use a sequence of larger numbers (e.g. 10^8 to 10^12), the loop won't seem to start.
for i in $(seq 100000000 1 1000000000000); do
echo $i
done
I cannot imagine, that these numbers are too large to handle. So my question: am I mistaken? Or might there be another problem?
The problem is that $(seq ...) is expanded into a list of words before the loop is executed. So your initial command is something like:
for i in 100000000 100000001 100000002 # all the way up to 1000000000000!
The result is much too long, which is what causes the error.
One possible solution would be to use a different style of loop:
for (( i = 100000000; i <= 1000000000000; i++ )) do
echo "$i"
done
This "C-style" construct uses a termination condition, rather than iterating over a literal list of words.
Portable style, for POSIX shells:
i=100000000
while [ $i -le 1000000000000 ]; do
echo "$i"
i=$(( i + 1 ))
done

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

For loop running only once in Bash [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 7 years ago.
I've just started shell scripting.
Here is my code:
read totalNumbers
for i in {1..$totalNumbers..1}
do
echo "Welcome $i times"
done
When the input is given as 100, I'm getting the output as
Welcome {1..100..1} times
But I need something like
Welcome 1 times
Welcome 2 times
Welcome 3 times
.
.
.
Welcome 100 times
You cannot use variable names inside {..} in shell.
You can use ((..)) for arithmetic in BASH:
for (( i=1; i <= $totalNumbers; i++ )); do
echo "Welcome $i times"
done
Even for (( i=1; i <= totalNumbers; i++ )) would work.
If you want to use
{1..$totalNumbers..1}
anyway, replace it by
$(eval echo {1..$totalNumbers..1})
$(...) runs a subshell.

Resources