Loop through an array of strings in Bash?

前端 未结 19 2807
情深已故
情深已故 2020-11-22 03:59

I want to write a script that loops through 15 strings (array possibly?) Is that possible?

Something like:

for databaseName in listOfNames
then
  # D         


        
相关标签:
19条回答
  • 2020-11-22 04:10

    Implicit array for script or functions:

    In addition to anubhava's correct answer: If basic syntax for loop is:

    for var in "${arr[@]}" ;do ...$var... ;done
    

    there is a special case in bash:

    When running a script or a function, arguments passed at command lines will be assigned to $@ array variable, you can access by $1, $2, $3, and so on.

    This can be populated (for test) by

    set -- arg1 arg2 arg3 ...
    

    A loop over this array could be written simply:

    for item ;do
        echo "This is item: $item."
      done
    

    Note that the reserved work in is not present and no array name too!

    Sample:

    set -- arg1 arg2 arg3 ...
    for item ;do
        echo "This is item: $item."
      done
    This is item: arg1.
    This is item: arg2.
    This is item: arg3.
    This is item: ....
    

    Note that this is same than

    for item in "$@";do
        echo "This is item: $item."
      done
    

    Then into a script:

    #!/bin/bash
    
    for item ;do
        printf "Doing something with '%s'.\n" "$item"
      done
    

    Save this in a script myscript.sh, chmod +x myscript.sh, then

    ./myscript.sh arg1 arg2 arg3 ...
    Doing something with 'arg1'.
    Doing something with 'arg2'.
    Doing something with 'arg3'.
    Doing something with '...'.
    

    Same in a function:

    myfunc() { for item;do cat <<<"Working about '$item'."; done ; }
    

    Then

    myfunc item1 tiem2 time3
    Working about 'item1'.
    Working about 'tiem2'.
    Working about 'time3'.
    
    0 讨论(0)
  • 2020-11-22 04:11

    You can use it like this:

    ## declare an array variable
    declare -a arr=("element1" "element2" "element3")
    
    ## now loop through the above array
    for i in "${arr[@]}"
    do
       echo "$i"
       # or do whatever with individual element of the array
    done
    
    # You can access them using echo "${arr[0]}", "${arr[1]}" also
    

    Also works for multi-line array declaration

    declare -a arr=("element1" 
                    "element2" "element3"
                    "element4"
                    )
    
    0 讨论(0)
  • 2020-11-22 04:11

    This is also easy to read:

    FilePath=(
        "/tmp/path1/"    #FilePath[0]
        "/tmp/path2/"    #FilePath[1]
    )
    
    #Loop
    for Path in "${FilePath[@]}"
    do
        echo "$Path"
    done
    
    0 讨论(0)
  • 2020-11-22 04:12

    What I really needed for this was something like this:

    for i in $(the_array); do something; done
    

    For instance:

    for i in $(ps -aux | grep vlc  | awk '{ print $2 }'); do kill -9 $i; done
    

    (Would kill all processes with vlc in their name)

    0 讨论(0)
  • 2020-11-22 04:13

    None of those answers include a counter...

    #!/bin/bash
    ## declare an array variable
    declare -a array=("one" "two" "three")
    
    # get length of an array
    arraylength=${#array[@]}
    
    # use for loop to read all values and indexes
    for (( i=1; i<${arraylength}+1; i++ ));
    do
      echo $i " / " ${arraylength} " : " ${array[$i-1]}
    done
    

    Output:

    1  /  3  :  one
    2  /  3  :  two
    3  /  3  :  three
    
    0 讨论(0)
  • 2020-11-22 04:16

    Possible first line of every Bash script/session:

    say() { for line in "${@}" ; do printf "%s\n" "${line}" ; done ; }
    

    Use e.g.:

    $ aa=( 7 -4 -e ) ; say "${aa[@]}"
    7
    -4
    -e
    

    May consider: echo interprets -e as option here

    0 讨论(0)
提交回复
热议问题