Mo.*_*Mo. 1371 arrays bash shell
我想编写一个循环遍历15个字符串的脚本(可能是数组?)这可能吗?
就像是:
for databaseName in listOfNames
then
# Do something
end
Run Code Online (Sandbox Code Playgroud)
anu*_*ava 2189
你可以像这样使用它:
## 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
Run Code Online (Sandbox Code Playgroud)
也适用于多行数组声明
declare -a arr=("element1"
"element2" "element3"
"element4"
)
Run Code Online (Sandbox Code Playgroud)
4nd*_*rew 724
当然,这是可能的.
for databaseName in a b c d e f; do
# do something like: echo $databaseName
done
Run Code Online (Sandbox Code Playgroud)
cak*_*tux 181
这些答案都不包括反...
#!/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
Run Code Online (Sandbox Code Playgroud)
输出:
1 / 3 : one
2 / 3 : two
3 / 3 : three
Run Code Online (Sandbox Code Playgroud)
Fir*_*Sky 121
是
for Item in Item1 Item2 Item3 Item4 ;
do
echo $Item
done
Run Code Online (Sandbox Code Playgroud)
输出:
Item1
Item2
Item3
Item4
Run Code Online (Sandbox Code Playgroud)
多行
for Item in Item1 \
Item2 \
Item3 \
Item4
do
echo $Item
done
Run Code Online (Sandbox Code Playgroud)
输出:
Item1
Item2
Item3
Item4
Run Code Online (Sandbox Code Playgroud)
简单列表变量
List=( Item1 Item2 Item3 )
Run Code Online (Sandbox Code Playgroud)
要么
List=(
Item1
Item2
Item3
)
Run Code Online (Sandbox Code Playgroud)
显示列表变量:
echo ${List[*]}
Run Code Online (Sandbox Code Playgroud)
输出:
Item1 Item2 Item3
Run Code Online (Sandbox Code Playgroud)
循环列表:
for Item in ${List[*]}
do
echo $Item
done
Run Code Online (Sandbox Code Playgroud)
输出:
Item1
Item2
Item3
Run Code Online (Sandbox Code Playgroud)
创建一个函数来浏览列表:
Loop(){
for item in ${*} ;
do
echo ${item}
done
}
Loop ${List[*]}
Run Code Online (Sandbox Code Playgroud)
保留空间; 单引号或双引号列表条目和双引号列表扩展:
List=(' Item 1 '
' Item 2'
' Item 3'
)
for item in "${List[@]}";
do
echo "$item"
done
Run Code Online (Sandbox Code Playgroud)
输出:
Item 1
Item 2
Item 3
Run Code Online (Sandbox Code Playgroud)
使用declare关键字(命令)创建列表,技术上称为数组:
declare -a List=(
"element 1"
"element 2"
"element 3"
)
for entry in "${List[@]}"
do
echo "$entry"
done
Run Code Online (Sandbox Code Playgroud)
输出:
element 1
element 2
element 3
Run Code Online (Sandbox Code Playgroud)
创建关联数组.一本字典:
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
Run Code Online (Sandbox Code Playgroud)
输出:
Argentina is in America
Vietnam is in Asia
France is in Europe
Run Code Online (Sandbox Code Playgroud)
CVS变量或文件到列表中.
将内部字段分隔符从空格更改为您想要的任何内容.
在下面的示例中,它将更改为逗号
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
Run Code Online (Sandbox Code Playgroud)
输出:
Item 1
Item 2
Item 3
Run Code Online (Sandbox Code Playgroud)
如果需要编号:
`
Run Code Online (Sandbox Code Playgroud)
这被称为后退.将命令放在后面.
`commend`
Run Code Online (Sandbox Code Playgroud)
它位于键盘上的第一位和标签键上方.在标准的美国英语键盘上.
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
Run Code Online (Sandbox Code Playgroud)
输出是:
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
Run Code Online (Sandbox Code Playgroud)
更熟悉bashes行为:
在文件中创建列表
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
Run Code Online (Sandbox Code Playgroud)
将列表文件读入列表并显示
List=$(cat List_entries.txt)
echo $List
echo '$List'
echo "$List"
echo ${List[*]}
echo '${List[*]}'
echo "${List[*]}"
echo ${List[@]}
echo '${List[@]}'
echo "${List[@]}"
Run Code Online (Sandbox Code Playgroud)
BASH命令行参考手册:shell中某些字符或单词的特殊含义.
use*_*809 106
本着与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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
笔记
listOfNames="RA RB R C RD"
相同的输出.其他引入数据的方法包括:
从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
Run Code Online (Sandbox Code Playgroud)
IFS='\n'
,对于MacOS IFS='\r'
)#!/bin/bash
在脚本文件的顶部表示执行环境.其他来源(读取循环时)
Fiz*_*han 41
你可以使用的语法 ${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
Run Code Online (Sandbox Code Playgroud)
Kof*_*ofi 36
我在 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
Run Code Online (Sandbox Code Playgroud)
您可以使用具有三个表达式(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
Run Code Online (Sandbox Code Playgroud)
小智 20
这也很容易阅读:
FilePath=(
"/tmp/path1/" #FilePath[0]
"/tmp/path2/" #FilePath[1]
)
#Loop
for Path in "${FilePath[@]}"
do
echo "$Path"
done
Run Code Online (Sandbox Code Playgroud)
Doo*_*nob 20
很惊讶没有人发布这个 - 如果你在循环数组时需要元素的索引,你可以这样做:
arr=(foo bar baz)
for i in ${!arr[@]}
do
echo $i "${arr[i]}"
done
Run Code Online (Sandbox Code Playgroud)
输出:
0 foo
1 bar
2 baz
Run Code Online (Sandbox Code Playgroud)
我觉得这比"传统的"循环风格(for (( i=0; i<${#arr[@]}; i++ ))
)要优雅得多.
(${!arr[@]}
并且$i
不需要引用,因为它们只是数字;有些人会建议引用它们,但这只是个人偏好.)
除了anubhava的正确答案:如果循环的基本语法是:
for var in "${arr[@]}" ;do ...$var... ;done
Run Code Online (Sandbox Code Playgroud)
有一个特殊的情况下的bash:
当运行一个脚本或函数,参数在命令行通过将被分配给$@
数组变量,你可以访问$1
,$2
,$3
,等等.
这可以填充(用于测试)
set -- arg1 arg2 arg3 ...
Run Code Online (Sandbox Code Playgroud)
一个循环在这个阵列可以简单地写成:
for item ;do
echo "This is item: $item."
done
Run Code Online (Sandbox Code Playgroud)
请注意,保留的工作in
不存在,也没有数组名称!
样品:
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: ....
Run Code Online (Sandbox Code Playgroud)
请注意,这与
for item in "$@";do
echo "This is item: $item."
done
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
for item ;do
printf "Doing something with '%s'.\n" "$item"
done
Run Code Online (Sandbox Code Playgroud)
在脚本保存此myscript.sh
,chmod +x myscript.sh
话
./myscript.sh arg1 arg2 arg3 ...
Doing something with 'arg1'.
Doing something with 'arg2'.
Doing something with 'arg3'.
Doing something with '...'.
Run Code Online (Sandbox Code Playgroud)
myfunc() { for item;do cat <<<"Working about '$item'."; done ; }
Run Code Online (Sandbox Code Playgroud)
然后
myfunc item1 tiem2 time3
Working about 'item1'.
Working about 'tiem2'.
Working about 'time3'.
Run Code Online (Sandbox Code Playgroud)
listOfNames="db_one db_two db_three"
for databaseName in $listOfNames
do
echo $databaseName
done
Run Code Online (Sandbox Code Playgroud)
要不就
for databaseName in db_one db_two db_three
do
echo $databaseName
done
Run Code Online (Sandbox Code Playgroud)
这与 user2533809 的答案类似,但每个文件将作为单独的命令执行。
#!/bin/bash
names="RA
RB
R C
RD"
while read -r line; do
echo line: "$line"
done <<< "$names"
Run Code Online (Sandbox Code Playgroud)
简单方法:
arr=("sharlock" "bomkesh" "feluda" ) ##declare array
len=${#arr[*]} # it returns the array length
#iterate with while loop
i=0
while [ $i -lt $len ]
do
echo ${arr[$i]}
i=$((i+1))
done
#iterate with for loop
for i in $arr
do
echo $i
done
#iterate with splice
echo ${arr[@]:0:3}
Run Code Online (Sandbox Code Playgroud)
小智 5
该声明数组不适用于Korn shell。将以下示例用于Korn shell:
promote_sla_chk_lst="cdi xlob"
set -A promote_arry $promote_sla_chk_lst
for i in ${promote_arry[*]};
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
尝试这个。它正在工作并经过测试。
for k in "${array[@]}"
do
echo $k
done
# For accessing with the echo command: echo ${array[0]}, ${array[1]}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1119240 次 |
最近记录: |