How to iterate in a range determined by TWO variables? - bash

I have a code:
echo "the range's starting number:"
read -r a #it was 10
echo "the range's ending number:"
read -r b #it was 20
for (( c=$a; c<=$b; c++ ))
do
echo $c
done
Question: what is the working syntax? I found a similar question where c=1; c<=$b; c++ worked. I want to iterate between $a (example $a=10) and $b (example $b=20) and not between 1 and $b=20. Thanks for the help. (the output is blank here, the expected output was:
10
11
12
..
20
I tried my code, also closed terminal and started a new one because of possible caching issues, but there was still a blank output.
Update: in the comments accdias's answer was working. With for ((c=a; c<=b; c++)) i got the expected output. Thanks all for the help and comments!

I took #accdias's comment and figured I would just drop it here as an answer for easy reference:
START=12
END=24
for ((i=START; i<=END; i++)); do
echo $i
done

Related

for i in {1..$number} acting wierd [duplicate]

This question already has answers here:
Using a variable in brace expansion range fed to a for loop
(5 answers)
Closed 1 year ago.
I have a for loop like this:
for i in {1..$number}
do
echo "Chose a file"
git apply $(zenity --file-selection --file-filter='patch files (patch) | *.patch' --title="Select your patch file")
done
The purpose of the code is for the user to input a file, and it will patch that file. It is supposed to do it multiple times, but it only does it once. I will not post the output, since the error is from the "git apply" and not the for i in {1..$number"}
I can't figure out what is wrong. Can anyone help?
For-in loop with variable is something complex, not intuitive at all:
for i in $( eval echo {0..$number} )
do
echo "Chose a file"
done
source:
https://stackoverflow.com/a/17181832/3957754
If you want something more readable use this:
for (( c=1; c<=$number; c++ ))
do
echo "Welcome $c times"
done
In this you will find more loop samples
https://www.cyberciti.biz/faq/bash-for-loop/

seq - invalid floating point argument error

I'm currently writing a small bash file to search specific information on a file.
I need a for structure and I'm using "seq" but I keep getting "invalid floating point argument error : 4" and I do not know how to solve it.
When I try to do some arithmetic operation on my variable nmbretry, I get a arithmetic operator not available.
If you have any ideas how to solve it!
Here my code:
#!/bin/bash
nmbretry=`grep -c 'retry for the 1 times' /home/leconte/dossierpartage/business.log`
echo "Number of retry is $nmbretry"
let $nmbretry + 1
for i in `seq 0 $nmbretry`; do echo $i
done;
Thanks a lot!
increment nmbretry with let ++nmbretry - bash also has a counted for loop:
for((i=0; i < nmbretry; ++i)); do
echo $i
done
Thanks for your help.
The error was that I used notepad on Windows than use it on Unix. It did not like the "transfer".
I do everything on Unix and now it's work!
Thanks again.

How to pass start variable to for loop, BASH

Good day,
I was wondering how to properly pass a variable to a for loop. Doesn't matter the syntax, I just want to pass the variable and count by two.
The issue:
when I write down:
r=0 ; for i in {"$r"..10..2}; do echo "Welcome $i times" ;done
I get:
Welcome {0..10..2} times
and not:
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times
Thanks in advance for any clue
The general format for a for loop that utilizes variables for loop boundaries is:
#!/bin/bash
a=2
b=10
increment=2
for ((i=$a; i<=$b; i+=$increment)); do
## <something with $i>
echo "i: $i"
done
output:
$ bash forloop.sh
i: 2
i: 4
i: 6
i: 8
i: 10
For the sake of completeness,
In stead of
for i in {"$r"..10..2};
you can try
for i in $(eval echo {$r..10..2});
However, I highly discourage you to use this solution, but go for David's solution.
You can not use variable in {a....b} syntax. But you can use seq.
see this

BASH Arithmetic Issues

I'm working in BASH and I'm having an idiot moment right now. I've got a project I'm working on that I'm going to need to use some very basic arithmetic expressions and I just realized that a lot of my problems with it are because my variables are not updating. So I threw together a basic algorithm that increments a variable by another variable with a while loop until a certain number is reached.
counter=1
counter2=0
while [[ counter2 < 10 ]]; do
counter2=$(($counter2+$counter))
echo $counter
echo $counter2
done
I run the script. Does nothing. I set the < to > just for kicks and an infinite loop occurs with a repeated output of:
1
0
1
0
Forever and ever until I stop it. So it's obvious the variables are not changing. Why? I feel like such an idiot because it must be something stupid I'm overlooking. And why, when I have <, it also isn't an infinite loop? Why doesn't it print anything at all for that matter? If counter2 is always less than 10, why doesn't it just keep going on forever?
Thanks folks in advance.
EDIT: Well, I realize why it wasn't outputting anything for when the check is <... I should have been using $counter2 instead of just counter2 to get the actual value of counter2. But now it just outputs:
1
2
And that's it... I feel like such a derp.
If this is all bash (100% sure) then you could use declare -i in order to explicitly set type of your variables and then your code will be as simple as :
declare -i counter=1
declare -i counter2=0
while [[ $counter2 -lt 10 ]]; do
counter2=$counter2+$counter
echo $counter
echo $counter2
done
EDIT:
In bash, you can do arithmatic comparison with double paranethesis. So, your while can be written as:
while (($counter2 < 10)) ; do
Inside the $((...)), don't use the sigil ($).
counter2=$((counter2+counter))
In bash, you can use c-like for loops:
for (( counter2=0; counter2<10; counter2+=counter ))
do
echo $counter": "$counter2
done
Often you will find this construct more appealing to use:
for counter2 in {0..9}
do
echo $counter": "$counter2
done

Output of command in Bash script to Drop-down box?

First off, I appreciate any and all help in answering this question.
I have a command in a bash script that will output the following:
255 254 253 252 ... 7 6 5 4 3 2 1
It is a specific list of numbers, beginning with the largest (which is what I would like), then going to the smallest. The dataset is space-delimited. The output above (except including all numbers), is what you would see if you ran this command in the terminal on a linux machine, or through a bash script.
I have configured my apache2 server to allow for cgi/bash through the cgi-bin directory. When I run this command in a bash file from the web, I get the expected output.
What I'm looking for is for a way to be able to put these numbers each as a separate entry in a drop-down box for selection, meaning the user can select one point of data (254, for example) from the drop down menu.
I'm not sure what I'm doing with this, so any help would be appreciated. I'm not sure if I need to convert the data into an array, or what. The drop down menu can be on the same page of the bash script, but wherever it is, it has to update it's list of numbers from the command every time it is run.
Thank you for your help.
I've always found this site useful when fiddling with shell scripts: http://tldp.org/LDP/abs/html/
you'll have to get your output into an array using some sort of string manipulation using the spaces as delimiters, then loop over that to build some html output - so the return value will basically just output your select box on the page where you execute your cgi/bash script.
-sean
Repeating the answer (since the original question was marked as duplicate):
you can write a bash for loop to do everything. This just prints out the elements:
for i in `seq 1 "${#x[*]}"`; do
echo "|${x[i]} |"
done
To get the alignment correct, you need to figure out the max length (one loop) and then print out the terms:
# w will be the length
w=0
for i in `seq 1 "${#x[*]}"`; do
if [ $w -lt ${#x[$i]} ]; then w=${#x[$i]}; fi
done
for i in `seq 1 $((w+2))`; do printf "%s" "-"; done
printf "\n"
for i in `seq 1 "${#x[*]}"`; do
printf "|%-$ws |\n" ${#x[$i]}
done
for i in `seq 1 $((w+2))`; do printf "%s" "-"; done
printf "\n"

Resources