Bash迭代数组示例

时间:2020-01-09 10:37:23  来源:igfitidea点击:

如何在Bash脚本下遍历数组?

Bash Shell支持一维数组变量。
对数组的大小没有最大限制,也没有对成员进行连续索引或分配的任何要求。
数组使用整数索引,并且从零开始。

创建一个数组

如果使用以下语法将任何变量分配给数组,则会自动创建一个数组:

array=( value1 value2 )
users=( www Hyman ftp chroot )
ns=( 192.168.1.10 192.168.1.12 )

或者

var[subscript]=value

访问下标

下标被视为必须等于或大于零的数字的算术表达式。
您可以使用以下语法显示值:

echo "${var[0]}"
echo "${ns[0]}"

Bash for循环迭代数组示例

语法如下:

for var in "${ArrayName[@]}"
do
  echo "${var}"
  # do something on $var
done

创建一个shell脚本,如下所示:

#!/bin/bash
# define file array
files=(/etc/*.conf)
 
# find total number of files in an array
echo "Total files in array : ${#files[*]}"
total=${#files[*]}
# Print 1st file name
echo "First filename: ${files[0]}"
echo "Second filename: ${files[1]}"
echo "Third filename: ${files[1]}"
# total - 1 = last item (subscript) in an array 
echo "Last filename: ${files[$(( $total-1 ))]}"
 
echo
echo "****************"
echo "*** For Loop ***"
echo "****************"
# Use for loop iterate through an array
# $f stores current value 
for f in "${files[@]}"
do
	echo -n "$f "
done
 
echo
echo 
echo "**************************"
echo "*** C Style For Loop ****"
echo "**************************"
echo 
# Use c style for loop
# get total subscripts in an array
total=${#files[*]}
# 
for (( i=0; i<=$(( $total -1 )); i++ ))
do 
    echo -n "${files[$i]} "
done
 
echo

输出示例:

Total files in array : 77
First filename: /etc/aatv.conf
Second filename: /etc/adduser.conf
Third filename: /etc/adduser.conf
Last filename: /etc/xorg.conf

****************
*** For Loop ***
****************
/etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/argus.conf /etc/atool.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/chkrootkit.conf /etc/cowpoke.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/dconf.conf /etc/dconf-custom.conf /etc/debconf.conf /etc/deluser.conf /etc/devscripts.conf /etc/discover.conf /etc/dnsmasq.conf /etc/dnsproxy.conf /etc/e2fsck.conf /etc/fdmount.conf /etc/ffserver.conf /etc/fuse.conf /etc/gai.conf /etc/gssapi_mech.conf /etc/hdparm.conf /etc/hesiod.conf /etc/host.conf /etc/idmapd.conf /etc/inetd.conf /etc/kernel-img.conf /etc/kernel-pkg.conf /etc/krb5.conf /etc/ld.so.conf /etc/lftp.conf /etc/libao.conf /etc/libuser.conf /etc/logrotate.conf /etc/ltrace.conf /etc/mke2fs.conf /etc/mplayerplug-in.conf /etc/mtools.conf /etc/multitail.conf /etc/netscsid.conf /etc/nscd.conf /etc/nsswitch.conf /etc/ntp.conf /etc/pam.conf /etc/pnm2ppa.conf /etc/popularity-contest.conf /etc/pub.rsnapshot.conf /etc/ra.conf /etc/resolv.conf /etc/rsnapshot.bull.conf /etc/rsnapshot.conf /etc/rsnapshot.rose.conf /etc/rsnapshot.txvip.conf /etc/rssh.conf /etc/scrollkeeper.conf /etc/sensors3.conf /etc/sensors.conf /etc/smartd.conf /etc/sysctl.conf /etc/sysfs.conf /etc/syslog.conf /etc/ts.conf /etc/ucf.conf /etc/uniconf.conf /etc/updatedb.conf /etc/usplash.conf /etc/vnc.conf /etc/vsftpd.conf /etc/warnquota.conf /etc/wodim.conf /etc/wpa_supplicant.conf /etc/wvdial.conf /etc/xorg.conf 

**************************
*** C Style For Loop ****
**************************

/etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/argus.conf /etc/atool.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/chkrootkit.conf /etc/cowpoke.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/dconf.conf /etc/dconf-custom.conf /etc/debconf.conf /etc/deluser.conf /etc/devscripts.conf /etc/discover.conf /etc/dnsmasq.conf /etc/dnsproxy.conf /etc/e2fsck.conf /etc/fdmount.conf /etc/ffserver.conf /etc/fuse.conf /etc/gai.conf /etc/gssapi_mech.conf /etc/hdparm.conf /etc/hesiod.conf /etc/host.conf /etc/idmapd.conf /etc/inetd.conf /etc/kernel-img.conf /etc/kernel-pkg.conf /etc/krb5.conf /etc/ld.so.conf /etc/lftp.conf /etc/libao.conf /etc/libuser.conf /etc/logrotate.conf /etc/ltrace.conf /etc/mke2fs.conf /etc/mplayerplug-in.conf /etc/mtools.conf /etc/multitail.conf /etc/netscsid.conf /etc/nscd.conf /etc/nsswitch.conf /etc/ntp.conf /etc/pam.conf /etc/pnm2ppa.conf /etc/popularity-contest.conf /etc/pub.rsnapshot.conf /etc/ra.conf /etc/resolv.conf /etc/rsnapshot.bull.conf /etc/rsnapshot.conf /etc/rsnapshot.rose.conf /etc/rsnapshot.txvip.conf /etc/rssh.conf /etc/scrollkeeper.conf /etc/sensors3.conf /etc/sensors.conf /etc/smartd.conf /etc/sysctl.conf /etc/sysfs.conf /etc/syslog.conf /etc/ts.conf /etc/ucf.conf /etc/uniconf.conf /etc/updatedb.conf /etc/usplash.conf /etc/vnc.conf /etc/vsftpd.conf /etc/warnquota.conf /etc/wodim.conf /etc/wpa_supplicant.conf /etc/wvdial.conf /etc/xorg.conf