Writing a Bash For-Loop with a Variable Top-End

Writing a bash for-loop with a variable top-end

You can use for loop like this to iterate with a variable $TOP:

for ((i=1; i<=$TOP; i++))
do
echo $i
# rest of your code
done

Writing a for loop in bash using a variable

You are running the script with sh, not bash.
Try:

bash split_history_file_test.sh

How to use variables in a bash for loop

One way is using eval:

for i in $( eval echo {0..$length} )
do
echo "do something right $i"
done

Note what happens when you set length=;ls or length=; rm * (don't try the latter though).

safely, using seq:

for i in $( seq 0 $length )
do
echo "do something right $i"
done

or you can use the c-style for loop, which is also safe:

for (( i = 0; i <= $length; i++ )) 
do
echo "do something right $i"
done

Store for loop results as a variable in bash

It's really easy, you can just redirect the output of the whole loop to a variable (if you want to use just one variable as stated):

VARIABLE=$(for time in ...; do ...; done)

your example:

var=$(for time in ${seconds_list}; do
echo "scale=2; (${cur_time}-${time})/3600" | bc
done)

Just enclosing your code into $().

How do I iterate over a range of numbers defined by variables in Bash?


for i in $(seq 1 $END); do echo $i; done

edit: I prefer seq over the other methods because I can actually remember it ;)

Increment in bash loop by set amount

If you want to print the ranges within 773, you can do like this

#!env bash
start=1
end=19
for counter in {1..773}
do
echo $counter. "\$start = " $start " and \$end = " $end
if [[ $start -eq 1 ]];
then
start=0
fi
start=$(($start+20))
end=$(($end+20))
if [[ $end -ge 773 ]];
then
break
fi
done

Output

1. $start =  1  and $end =  19
2. $start = 20 and $end = 39
3. $start = 40 and $end = 59
4. $start = 60 and $end = 79
5. $start = 80 and $end = 99
6. $start = 100 and $end = 119
7. $start = 120 and $end = 139
8. $start = 140 and $end = 159
9. $start = 160 and $end = 179
10. $start = 180 and $end = 199
11. $start = 200 and $end = 219
12. $start = 220 and $end = 239
13. $start = 240 and $end = 259
14. $start = 260 and $end = 279
15. $start = 280 and $end = 299
16. $start = 300 and $end = 319
17. $start = 320 and $end = 339
18. $start = 340 and $end = 359
19. $start = 360 and $end = 379
20. $start = 380 and $end = 399
21. $start = 400 and $end = 419
22. $start = 420 and $end = 439
23. $start = 440 and $end = 459
24. $start = 460 and $end = 479
25. $start = 480 and $end = 499
26. $start = 500 and $end = 519
27. $start = 520 and $end = 539
28. $start = 540 and $end = 559
29. $start = 560 and $end = 579
30. $start = 580 and $end = 599
31. $start = 600 and $end = 619
32. $start = 620 and $end = 639
33. $start = 640 and $end = 659
34. $start = 660 and $end = 679
35. $start = 680 and $end = 699
36. $start = 700 and $end = 719
37. $start = 720 and $end = 739
38. $start = 740 and $end = 759

descending loop with variable bash

You should specify the increment with seq:

seq $FROMHERE -1 1

BASH: How to write values generated by a for loop to a file quickly

It looks like the seq calls are fairly punishing since that is a separate process. Try this just using shell math instead:

for ((k=0;k<=$nk-1;k++)); do
for ((i=0;i<=$nb-1;i++)); do
for ((j=0;j<=$nb-1;j++)); do
echo -e "$k\t$i\t$j"
done
done
done > file.dat

It takes just 7.5s on my machine.

Another way is to compute the sequences just once and use them repeatedly, saving a lot of shell calls:

    nk=1152
nb=24

kseq=$(seq 0 $((nk-1)))
bseq=$(seq 0 $((nb-1)))

for k in $kseq; do
for i in $bseq; do
for j in $bseq; do
echo -e "$k\t$i\t$j"
done
done
done > file.dat

This is not really "better" than the first option, but it shows how much of the time is spent spinning up instances of seq versus actually getting stuff done.

Bash isn't always the best for this. Consider this Ruby equivalent which runs in 0.5s:

#!/usr/bin/env ruby

nk=1152
nb=24

nk.times do |k|
nb.times do |i|
nb.times do |j|
puts "%d\t%d\t%d" % [ k, i, j ]
end
end
end

How can I loop over the output of a shell command?

Never for loop over the results of a shell command if you want to process it line by line unless you are changing the value of the internal field separator $IFS to \n. This is because the lines will get subject of word splitting which leads to the actual results you are seeing. Meaning if you for example have a file like this:

foo bar
hello world

The following for loop

for i in $(cat file); do
echo "$i"
done

gives you:

foo
bar
hello
world

Even if you use IFS='\n' the lines might still get subject of Filename expansion


I recommend to use while + read instead because read reads line by line.

Furthermore I would use pgrep if you are searching for pids belonging to a certain binary. However, since python might appear as different binaries, like python2.7 or python3.4 I suggest to pass -f to pgrep which makes it search the whole command line rather than just searching for binaries called python. But this will also find processes which have been started like cat foo.py. You have been warned! At the end you can refine the regex passed to pgrep like you wish.

Example:

pgrep -f python | while read -r pid ; do
echo "$pid"
done

or if you also want the process name:

pgrep -af python | while read -r line ; do
echo "$line"
done

If you want the process name and the pid in separate variables:

pgrep -af python | while read -r pid cmd ; do
echo "pid: $pid, cmd: $cmd"
done

You see, read offers a flexible and stable way to process the output of a command line-by-line.


Btw, if you prefer your ps .. | grep command line over pgrep use the following loop:

ps -ewo pid,etime,cmd | grep python | grep -v grep | grep -v sh \
| while read -r pid etime cmd ; do
echo "$pid $cmd $etime"
done

Note how I changed the order of etime and cmd. Thus to be able to read cmd, which can contain whitespace, into a single variable. This works because read will break down the line into variables, as many times as you specified variables. The remaining part of the line - possibly including whitespace - will get assigned to the last variable which has been specified in the command line.

Using a variable in brace expansion range fed to a for loop

You should use a C-style for loop to accomplish this:

for ((i=1; i<=$1; i++)); do
echo $i
done

This avoids external commands and nasty eval statements.



Related Topics



Leave a reply



Submit