PCARR
35
Q:

bash script loop

#!/bin/bash
for (( c=1; c<=5; c++ ))
do  
   echo "Welcome $c times"
done
4
years=(2018 2019)
days=(74 274)

for year in "${years[@]}"; do
    for day in $(seq -w ${days[0]} ${days[1]}); do
               echo $year
               echo $day
    done
done
5
#!/bin/bash

# A simple bash "for loop" example below...
# This loop will run 5 times, and will echo the number in sequence as it goes.
# Mind the double . between the {}
# Note how the variable is called with the $ prefix, which can be done within the "" quotes!

for i in {1..5}
do
   echo "This is loop number $i"
done

# To achieve the sae in a single line is simple

for i in {1..5};do echo "this is loop number $i";done
2
for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done
4
for VARIABLE in file1 file2 file3
do
	command1 on $VARIABLE
	command2
	commandN
done
3
while [ <some test> ]
do
<commands>
done
2
Use for in bash for iterating words in a string or values in an array as:
for value in {1, 2, 3}; do echo $value; done
for value in $(cat arguments_files.txt); do [some_command]; done
And use while for iterating lines from a pipe output as:
cat arguments_file.txt | while read line; do [some_command]; done
0

New to Communities?

Join the community