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

类似于:

for databaseName in listOfNames
then
  # Do something
end

当前回答

本着与4ndrew的回答相同的精神:

listOfNames="RA
RB
R C
RD"

# To allow for other whitespace in the string:
# 1. add double quotes around the list variable, or
# 2. see the IFS note (under 'Side Notes')

for databaseName in "$listOfNames"   #  <-- Note: Added "" quotes.
do
  echo "$databaseName"  # (i.e. do action / processing of $databaseName here...)
done

# Outputs
# RA
# RB
# R C
# RD

B.名称中无空格:

listOfNames="RA
RB
R C
RD"

for databaseName in $listOfNames  # Note: No quotes
do
  echo "$databaseName"  # (i.e. do action / processing of $databaseName here...)
done

# Outputs
# RA
# RB
# R
# C
# RD

笔记

在第二个示例中,使用listOfNames=“RA RB R C RD”具有相同的输出。

其他引入数据的方法包括:

stdin(如下所列),变量,数组(接受的答案),文件。。。

从stdin读取

# line delimited (each databaseName is stored on a line)
while read databaseName
do
  echo "$databaseName"  # i.e. do action / processing of $databaseName here...
done # <<< or_another_input_method_here

可以在脚本中指定bash IFS“字段分隔符到行”[1]分隔符,以允许其他空格(即IFS='\n',或MacOS IFS='\r')我也喜欢接受的答案:)--我将这些片段作为其他有用的方式来回答这个问题。包括#/脚本文件顶部的bin/bash指示执行环境。我花了几个月的时间才弄清楚如何简单地编写代码:)

其他来源(读取循环时)

其他回答

这些答案中没有一个包含计数器。。。

#!/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=0; i<${arraylength}; i++ ));
do
  echo "index: $i, value: ${array[$i]}"
done

输出:

index: 0, value: one
index: 1, value: two
index: 2, value: three

当然,这是可能的。

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

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

我在GitHub更新中使用了这种方法,我发现它很简单。

## declare an array variable
arr_variable=("kofi" "kwame" "Ama")

## now loop through the above array
for i in "${arr_variable[@]}"
do
   echo "$i"


done
   

您可以使用带有三个表达式(C样式)的计数器遍历bash数组值,以读取循环语法的所有值和索引:

declare -a kofi=("kofi" "kwame" "Ama")
 
# get the length of the array
length=${#kofi[@]}

for (( j=0; j<${length}; j++ ));
do
  print (f "Current index %d with value %s\n" $j "${kofi[$j]}")
done

脚本或函数的隐式数组:

除了anubhava的正确答案:如果循环的基本语法是:

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

bash中有一个特殊情况:

当运行脚本或函数时,在命令行传递的参数将被分配给$@数组变量,您可以通过$1、$2、$3等进行访问。

可以通过以下方式填充(用于测试)

set -- arg1 arg2 arg3 ...

这个数组上的循环可以简单地写:

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

请注意,中的保留工作不存在,也没有数组名称!

示例:

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: ....

注意,这与

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

然后进入脚本:

#!/bin/bash

for item ;do
    printf "Doing something with '%s'.\n" "$item"
  done

将其保存在脚本myscript.sh、chmod+x myscript.shh中,然后

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

在函数中相同:

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

Then

myfunc item1 tiem2 time3
Working about 'item1'.
Working about 'tiem2'.
Working about 'time3'.

试试这个。它正在运行和测试。

for k in "${array[@]}"
do
    echo $k
done

# For accessing with the echo command: echo ${array[0]}, ${array[1]}