🖌️
Linux命令大全
  • Linux 命令大全
  • 命令
    • vgchange
    • ack
    • login
    • help
    • unset
    • pstree
    • emacs
    • route
    • fg
    • uupick
    • uniq
    • exportfs
    • jed
    • cd
    • bc
    • ab
    • gunzip
    • halt
    • readelf
    • fold
    • ss
    • nologin
    • users
    • unzip
    • head
    • xinit
    • dd
    • chkconfig
    • apropos
    • spell
    • rpmverify
    • ssh
    • seinfo
    • tcpreplay
    • dpkg-preconfigure
    • mkinitrd
    • patch
    • lilo
    • xclip
    • rmmod
    • nm
    • lprm
    • look
    • reject
    • ifdown
    • ifcfg
    • gcc
    • yes
    • mysqlimport
    • ed
    • nslookup
    • ispell
    • nisdomainname
    • rsh
    • ifconfig
    • inotifywait
    • whoami
    • rpmbuild
    • e2label
    • wget
    • sendmail
    • crontab
    • setfacl
    • nl
    • rename
    • jwhois
    • sort
    • lsof
    • passwd
    • exit
    • lvresize
    • cmp
    • smbpasswd
    • tree
    • elm
    • tr
    • kill
    • times
    • pgrep
    • axel
    • gdb
    • fc
    • lha
    • pfctl
    • mknod
    • bind
    • bzdiff
    • nice
    • dpkg-statoverride
    • systool
    • gpasswd
    • tar
    • slocate
    • dpkg-deb
    • file
    • zcat
    • ngrep
    • gpm
    • arptables
    • sshd
    • ipcalc
    • sar
    • basename
    • tmux
    • ftpwho
    • uuto
    • false
    • builtin
    • cksum
    • pwconv
    • rexec
    • rev
    • rm
    • source
    • fdisk
    • sync
    • killall
    • lsb_release
    • id
    • tput
    • repquota
    • uncompress
    • cut
    • ldconfig
    • mysql
    • pidof
    • sesearch
    • shift
    • openssl
    • ftp
    • vgconvert
    • nohup
    • dig
    • iostat
    • strings
    • set
    • type
    • expand
    • cancel
    • chsh
    • gzip
    • umask
    • hping3
    • logsave
    • ltrace
    • pathchk
    • nmcli
    • lscpu
    • zipsplit
    • printf
    • rpmquery
    • zip
    • diffstat
    • runlevel
    • sftp-server
    • find
    • less
    • chown
    • col
    • ifup
    • uname
    • named-checkzone
    • mke2fs
    • pssh
    • disown
    • lftpget
    • scp
    • mii-tool
    • pstack
    • lsattr
    • joe
    • ld
    • chgrp
    • dris
    • xhost
    • xauth
    • userdel
    • unarj
    • ssh-agent
    • lpstat
    • cupsenable
    • grub
    • lynx
    • ipcs
    • write
    • logrotate
    • csplit
    • seq
    • rcp
    • touch
    • lpc
    • quotaoff
    • mail
    • mpstat
    • vgreduce
    • hwclock
    • arp
    • rlogin
    • break
    • mysqlshow
    • syslog
    • gzexe
    • semanage
    • ip6tables-save
    • startx
    • pvchange
    • rpm2cpio
    • chattr
    • grep
    • volname
    • who
    • last
    • lnstat
    • read
    • atq
    • lftp
    • setsebool
    • ssh-keygen
    • bzcat
    • iptables-save
    • jobs
    • arch
    • shutdown
    • shopt
    • sh
    • more
    • iconv
    • chcon
    • od
    • nc
    • lsusb
    • squidclient
    • pwd
    • dircolors
    • which
    • fsck
    • expr
    • partprobe
    • wait
    • dpkg-split
    • get_module
    • chmod
    • vgremove
    • rcconf
    • export
    • nano
    • stat
    • blkid
    • pvdisplay
    • dump
    • elinks
    • atop
    • restorecon
    • showmount
    • suspend
    • apk
    • telint
    • supervisord
    • groups
    • ssh-add
    • fgrep
    • setpci
    • dnf
    • pwunconv
    • swapoff
    • resize
    • kexec
    • insmod
    • install
    • cpio
    • hostname
    • kernelversion
    • ex
    • at
    • unlink
    • let
    • dpkg-divert
    • egrep
    • unexpand
    • popd
    • lsmod
    • slabtop
    • dpkg-trigger
    • vi
    • colrm
    • ftptop
    • depmod
    • cdrecord
    • swapon
    • mailq
    • ping
    • mktemp
    • cu
    • as
    • uucico
    • test
    • man
    • losetup
    • strace
    • dhcrelay
    • tload
    • batch
    • split
    • parted
    • chage
    • dos2unix
    • lsblk
    • locate
    • du
    • bzip2recover
    • arj
    • ar
    • bzless
    • tty
    • exec
    • mount
    • ifstat
    • mkswap
    • grpconv
    • edquota
    • lvreduce
    • tempfile
    • init
    • fishshell
    • dstat
    • logger
    • php
    • tail
    • return
    • htdigest
    • arpwatch
    • dnsdomainname
    • iftop
    • ntpdate
    • renice
    • fping
    • quotacheck
    • quota
    • lvdisplay
    • mapfile
    • dirname
    • ntsysv
    • pwck
    • unalias
    • usermod
    • sftp
    • apt-key
    • dhclient
    • ipcrm
    • quotaon
    • vgrename
    • perl
    • sleep
    • w
    • domainname
    • vdfuse
    • ssh-keyscan
    • dpkg-reconfigure
    • info
    • mtools
    • lvremove
    • tftp
    • zfore
    • modprobe
    • vgscan
    • date
    • mysqldump
    • sysctl
    • cat
    • tac
    • bzgrep
    • nmap
    • tailf
    • bmodinfo
    • useradd
    • whatis
    • make
    • atrm
    • true
    • ftpshut
    • pvck
    • top
    • mkbootdisk
    • chfn
    • htop
    • service
    • trap
    • md5sum
    • ftpcount
    • firewall-cmd
    • ip6tables-restore
    • time
    • bzmore
    • sudo
    • tcpdump
    • iptables-restore
    • awk
    • apt-sortpkgs
    • cp
    • xargs
    • mysqladmin
    • vgcreate
    • wc
    • indent
    • screen
    • groupdel
    • comm
    • tee
    • pmap
    • free
    • curl
    • mkisofs
    • mesg
    • history
    • ip6tables
    • getenforce
    • pigz
    • talk
    • protoize
    • iptables
    • setsid
    • clockdiff
    • vgextend
    • mkfs
    • paste
    • mailstat
    • dhcpd
    • cupsdisable
    • logwatch
    • smbclient
    • bzip2
    • vmstat
    • git
    • iperf
    • wall
    • ppp-off
    • squid
    • join
    • getsebool
    • xset
    • htpasswd
    • logout
    • rmdir
    • grpunconv
    • poweroff
    • pvremove
    • iptraf
    • sed
    • mv
    • finger
    • netstat
    • skill
    • xlsclients
    • whereis
    • fuser
    • eject
    • consoletype
    • usernetctl
    • yum
    • ethtool
    • vgdisplay
    • newusers
    • blockdev
    • alias
    • hdparm
    • host
    • nethogs
    • dmesg
    • accept
    • dpkg
    • uptime
    • lvscan
    • lspci
    • chroot
    • updatedb
    • rpm
    • watch
    • logname
    • hexdump
    • zipinfo
    • nfsstat
    • continue
    • ls
    • iotop
    • ip
    • mkdir
    • aptitude
    • dirs
    • lvcreate
    • dmidecode
    • diff3
    • apt-get
    • stty
    • clock
    • pkill
    • ypdomainname
    • restore
    • ulimit
    • findfs
    • pv
    • znew
    • cal
    • ssh-copy-id
    • lpr
    • bzcmp
    • lastlog
    • bye
    • bg
    • unprotoize
    • convertquota
    • pico
    • diff
    • grpck
    • apachectl
    • env
    • fmt
    • ncftp
    • xlsfonts
    • rpmdb
    • reboot
    • lpq
    • clear
    • echo
    • pvs
    • rsync
    • arpd
    • ldd
    • lastb
    • umount
    • systemctl
    • lvextend
    • bunzip2
    • speedtest-cli
    • iptstate
    • lp
    • traceroute
    • rpmsign
    • pvscan
    • enable
    • objdump
    • command
    • arping
    • badblocks
    • gcov
    • hostid
    • local
    • ps
    • xlsatoms
    • dpkg-query
    • groupmod
    • lpadmin
    • sum
    • chpasswd
    • xz
    • groupadd
    • su
    • pr
    • e2fsck
    • unrar
    • telnet
    • ln
    • declare
    • df
    • compress
    • tracepath
    • pvcreate
    • pushd
    • readonly
Powered by GitBook
On this page
  • 概要
  • 主要用途
  • 选项
  • 参数
  • 返回值
  • 例子
  • 注意

Was this helpful?

  1. 命令

mapfile

从标准输入读取行并赋值到数组。

概要

mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]

主要用途

  • 从标准输入或文件描述符读取行并赋值到数组。

选项

-d delim       将delim设为行分隔符,代替默认的换行符。
-n count       从标准输入中获取最多count行,如果count为零那么获取全部。
-O origin      从数组下标为origin的位置开始赋值,默认的下标为0。
-s count       跳过对前count行的读取。
-t             读取时移除行分隔符delim(默认为换行符)。
-u fd          从文件描述符fd中读取。
-C callback    每当读取了quantum行时,调用callback语句。
-c quantum     设定读取的行数为quantum。

如果使用-C时没有同时使用-c指定quantum的值,那么quantum默认为5000。
当callback语句执行时,将数组下一个要赋值的下标以及读取的行作为额外的参数传递给callback语句。
如果使用-O时没有提供起始位置,那么mapfile会在实际赋值之前清空该数组。

参数

array(可选):用于输出的数组名称。如果没有指定数组名称,那么会默认写入到变量名为MAPFILE的数组中。

返回值

返回成功除非使用了非法选项、指定的数组是只读的、指定的数组不是下标数组。

例子

# 常见的读取形式。
mapfile < source_file target_array
cat source_file |mapfile target_array
mapfile -u fd target_array

# 只读取前5行。
mapfile < source_file -n 5 target_array

# 跳过前5行。
mapfile < source_file -s 5 target_array

# 在数组指定的下标开始赋值。
# 请注意:这样做不会清空该数组。
mapfile < source_file -O 2 target_array

# 读取时设定行分隔符为tab。
# 注意,第二行的tab在终端需要用ctrl+v tab输入;
mapfile < source_file -d $'\t' target_array
mapfile < source_file -d '    ' target_array

# 读取时移除行分隔符(tab)。
mapfile < source_file -d $'\t' -t target_array
# 读取时移除行分隔符(换行符)。
mapfile < source_file -t target_array

# 每读取2行,执行一次语句(在这里是echo)。
mapfile < source_file -C "echo CALLBACK:" -c 2 target_array

# 遍历下标,依次显示数组的元素。
for i in ${!target_array[@]}; do
  printf "%s" ${target_array[i]}
done

注意

  1. 该命令是bash内建命令,相关的帮助信息请查看help命令。

  2. bash内建命令readarray是mapfile的同义词。

PreviouslvdisplayNextdirname

Last updated 5 years ago

Was this helpful?