我想写一个循环15个字符串的脚本(可能是数组?)这可能吗?
类似于:
for databaseName in listOfNames
then
# Do something
end
我想写一个循环15个字符串的脚本(可能是数组?)这可能吗?
类似于:
for databaseName in listOfNames
then
# Do something
end
当前回答
我在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'.
很惊讶还没有人发布这个消息——如果在遍历数组时需要元素的索引,可以这样做:
arr=(foo bar baz)
for i in ${!arr[@]}
do
echo $i "${arr[i]}"
done
输出:
0 foo
1 bar
2 baz
我发现这比“传统”for循环样式(for((I=0;I<${#arr[@]};I++))要优雅得多。
(${!arr[@]}和$i不需要引用,因为它们只是数字;有些人会建议引用它们,但这只是个人偏好。)
我在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
每个Bash脚本/会话的可能第一行:
say() { for line in "${@}" ; do printf "%s\n" "${line}" ; done ; }
使用例如:
$ aa=( 7 -4 -e ) ; say "${aa[@]}"
7
-4
-e
可以考虑:echo在此处将-e解释为选项
Yes
for Item in Item1 Item2 Item3 Item4 ;
do
echo $Item
done
输出:
Item1
Item2
Item3
Item4
保留空间;单引号或双引号列表条目和双引号列表扩展。
for Item in 'Item 1' 'Item 2' 'Item 3' 'Item 4' ;
do
echo "$Item"
done
输出:
Item 1
Item 2
Item 3
Item 4
在多行上制作列表的步骤
for Item in Item1 \
Item2 \
Item3 \
Item4
do
echo $Item
done
输出:
Item1
Item2
Item3
Item4
简单列表变量
List=( Item1 Item2 Item3 )
or
List=(
Item1
Item2
Item3
)
显示列表变量:
echo ${List[*]}
输出:
Item1 Item2 Item3
循环浏览列表:
for Item in ${List[*]}
do
echo $Item
done
输出:
Item1
Item2
Item3
创建一个函数来遍历列表:
Loop(){
for item in ${*} ;
do
echo ${item}
done
}
Loop ${List[*]}
使用declare关键字(命令)创建列表,技术上称为数组:
declare -a List=(
"element 1"
"element 2"
"element 3"
)
for entry in "${List[@]}"
do
echo "$entry"
done
输出:
element 1
element 2
element 3
创建关联阵列。词典:
declare -A continent
continent[Vietnam]=Asia
continent[France]=Europe
continent[Argentina]=America
for item in "${!continent[@]}";
do
printf "$item is in ${continent[$item]} \n"
done
输出:
Argentina is in America
Vietnam is in Asia
France is in Europe
列表中的CSV变量或文件。将内部字段分隔符从空格更改为所需的分隔符。在下面的示例中,它被更改为逗号
List="Item 1,Item 2,Item 3"
Backup_of_internal_field_separator=$IFS
IFS=,
for item in $List;
do
echo $item
done
IFS=$Backup_of_internal_field_separator
输出:
Item 1
Item 2
Item 3
如果需要编号:
`
这被称为倒勾。将命令放在反记号内。
`command`
它位于键盘上的数字1旁边,或者在标准美式英语键盘上的tab键上方。
List=()
Start_count=0
Step_count=0.1
Stop_count=1
for Item in `seq $Start_count $Step_count $Stop_count`
do
List+=(Item_$Item)
done
for Item in ${List[*]}
do
echo $Item
done
输出为:
Item_0.0
Item_0.1
Item_0.2
Item_0.3
Item_0.4
Item_0.5
Item_0.6
Item_0.7
Item_0.8
Item_0.9
Item_1.0
更加熟悉害羞的行为:
在文件中创建列表
cat <<EOF> List_entries.txt
Item1
Item 2
'Item 3'
"Item 4"
Item 7 : *
"Item 6 : * "
"Item 6 : *"
Item 8 : $PWD
'Item 8 : $PWD'
"Item 9 : $PWD"
EOF
将列表文件读入列表并显示
List=$(cat List_entries.txt)
echo $List
echo '$List'
echo "$List"
echo ${List[*]}
echo '${List[*]}'
echo "${List[*]}"
echo ${List[@]}
echo '${List[@]}'
echo "${List[@]}"
BASH命令行参考手册:shell中某些字符或单词的特殊含义。