How to loop an argument value in bash [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 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

Related

Why is bash behaving differently when it takes argument in braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
Input: range 6
function range {
echo {0..$1}
echo {0..6}
if [[ $1 =~ 6 ]]
then
echo "Equal"
fi
}
Output:
{0..6}
0 1 2 3 4 5 6
Why is the output different whereas $1 and 6 is equal?
The curly-braces get expanded before $-variables are expanded, and the curly-brace range syntax simply doesn't recognize anything other than a pair of numbers with dots in between.

Check number of arguments in a script [duplicate]

This question already has answers here:
Check that there are at least two arguments given in a bash script
(2 answers)
Closed 1 year ago.
How can I check in my script if I have at least 3 arguments, it must be something like:
if #number_of_arguments < 3
then
echo "Not enough arguments"
if [ $# -lt 3 ]; then
...
fi
In bash an alternative is to use an arithmetic expression.
if (( $# < 3 )); then
...
fi

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

Resources