How to use for cycle in bash script?

2

I have a little bash script that does not work the way I want to

Here is the code

#! /bin/bash

echo "Escriba la cantidad de letras"
read x
for i in $x;do
echo "Letra $i"
read y
echo $y
done
#export Mensaje
#./recibir.sh

Image of the logical error

It is supposed that I should do a repeat of the echo the number of times I entered in the upper echo, but only do it once and I do not understand very well the logic of the for bash

    
asked by David 10.11.2016 в 21:45
source

3 answers

2

The bas_%% co_ iterates over a set of values ; is similar to for or for ... in of other languages.

It looks better with an example:

for idx in *.sh;
 do echo $in;
done;
  • Bash creates a sequence that contains all the files that match the pattern.

    [file1.sh file2.sh file3.sh]

  • foreach iterates over the elements of the generated sequence.

  • idx = file1.sh - > ...

    idx = file2.sh - > ...

    For for numeric%, you should do

    for i in $(seq 1 $END)
    
        
    answered by 10.11.2016 / 21:58
    source
    1

    To complement @Trauma's response.

    "for" does a little more than iterate over a set of values, or rather, that set of values is generated to then iterate over it, also operating under premises, as would be done in c and it does in several Ways:

  • for name in < secuencia separada por espacios >;do lista de comandos;done
  • for ((expr1;expr2;expr3));do lista de comandos;done
  • This can be found in man 1 bash .

    I mention this because after in, in example 1, the for loop works on expansions, that is:

    for archivo in carpeta/*
    do
        echo $archivo
    done
    #es equivalente a
    for archivo in "$(ls carpeta)"
    do
        echo $archivo
    done
    

    They are equivalent but they work with different types of expansions, the first was on pathname expansion and the second was on command substitution, but it can also be done on brace expansion, that is:

    for i in {1..4}
    do
      echo $i
    done
    1
    2
    3
    4
    

    Or a slightly more complex example in which you can combine them:

    for archivo in ca1/sc{2,3}/*foo
    do
        echo $archivo
    done
    

    It will print all the files that end with foo and that are inside the sc2 and sc3 folders that are also inside the ca1 folder.

    You can also see the expansions in man bash , in the EXPANSIONS section. It is important to note that the order in which the chains are expanded has an order of priority.

    I say this about the expansions because I think it is necessary to clarify it since there are questions like these:

    In the second example that @Alexlex put, you can see that you can also iterate on the expansion of brace expansion on an array.

        
    answered by 25.07.2018 в 20:17
    0

    If what you want is to make a for typical of 0 ax where x is read from a variable and represents the limit of repetitions you can use the for with syntax very similar to C language.

    #!/bin/bash
    echo "Ingrese el número de repeticiones para el ciclo for: "
    read r
    echo "Iniciando el for: "
    for((i=0; i<r; i++)); do
            echo $i;
    done
    

    On the other hand bash supports the foreach in the same structure for, the foreach is very useful to read arrays I leave you a practical example:

    #!/bin/bash
    r=("one two" "two" "3" "5")
    for row in "${r[@]}"; do
            echo $row
    done
    

    "$ {r [@]}"

    • Refers to the array $ r
    • If the position is $ r [ @ ] "@" then you reference all the elements of the array ie ("one two" "two" "3" "5"), positions (0, 1, 2, 3) = @
    • When quoted "$ {r [@]}" each resulting element will be a single string, this will prevent "one two" from being interpreted as two different strings.
    • Finally the for once the resulting data is sorted iterates the elements of the arrangement and prints them.
    answered by 25.07.2018 в 22:25