我想写一个循环15个字符串的脚本(可能是数组?)这可能吗?
类似于:
for databaseName in listOfNames
then
# Do something
end
我想写一个循环15个字符串的脚本(可能是数组?)这可能吗?
类似于:
for databaseName in listOfNames
then
# Do something
end
当前回答
循环遍历数组的方式取决于换行符的存在。使用分隔数组元素的换行符,数组可以称为“$array”,否则应称为“${array[@]}”。以下脚本将明确说明:
#!/bin/bash
mkdir temp
mkdir temp/aaa
mkdir temp/bbb
mkdir temp/ccc
array=$(ls temp)
array1=(aaa bbb ccc)
array2=$(echo -e "aaa\nbbb\nccc")
echo '$array'
echo "$array"
echo
for dirname in "$array"; do
echo "$dirname"
done
echo
for dirname in "${array[@]}"; do
echo "$dirname"
done
echo
echo '$array1'
echo "$array1"
echo
for dirname in "$array1"; do
echo "$dirname"
done
echo
for dirname in "${array1[@]}"; do
echo "$dirname"
done
echo
echo '$array2'
echo "$array2"
echo
for dirname in "$array2"; do
echo "$dirname"
done
echo
for dirname in "${array2[@]}"; do
echo "$dirname"
done
rmdir temp/aaa
rmdir temp/bbb
rmdir temp/ccc
rmdir temp
其他回答
如果您使用的是Korn shell,则有“set-A databaseName”,否则有“declare-A databaseName”
要在所有外壳上编写脚本,
set -A databaseName=("db1" "db2" ....) ||
declare -a databaseName=("db1" "db2" ....)
# now loop
for dbname in "${arr[@]}"
do
echo "$dbname" # or whatever
done
它应该在所有外壳上工作。
我循环浏览一系列项目以进行git pull更新:
#!/bin/sh
projects="
web
ios
android
"
for project in $projects do
cd $HOME/develop/$project && git pull
end
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中某些字符或单词的特殊含义。
声明数组不适用于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
这与user2533809的答案类似,但每个文件都将作为单独的命令执行。
#!/bin/bash
names="RA
RB
R C
RD"
while read -r line; do
echo line: "$line"
done <<< "$names"