我想写一个循环15个字符串的脚本(可能是数组?)这可能吗?

类似于:

for databaseName in listOfNames
then
  # Do something
end

当前回答

我循环浏览一系列项目以进行git pull更新:

#!/bin/sh
projects="
web
ios
android
"
for project in $projects do
    cd  $HOME/develop/$project && git pull
end

其他回答

当然,这是可能的。

for databaseName in a b c d e f; do
  # do something like: echo $databaseName
done 

有关详细信息,请参阅、while和until的Bash循环。

这也很容易阅读:

FilePath=(
    "/tmp/path1/"    #FilePath[0]
    "/tmp/path2/"    #FilePath[1]
)

#Loop
for Path in "${FilePath[@]}"
do
    echo "$Path"
done

每个Bash脚本/会话的可能第一行:

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

使用例如:

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

可以考虑:echo在此处将-e解释为选项

您可以这样使用:

## 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

也适用于多行数组声明

declare -a arr=("element1" 
                "element2" "element3"
                "element4"
                )

您可以使用${arrayName[@]}的语法

#!/bin/bash
# declare an array called files, that contains 3 values
files=( "/etc/passwd" "/etc/group" "/etc/hosts" )
for i in "${files[@]}"
do
    echo "$i"
done