I want to store some lines of the output of blkid
in an array. The problem is, that those lines contain whitespace and the array syntax takes those as delimiters for single array elements, so that i end up with splitted lines in my array instead of one line beeing one array element.
我想在一个数组中存储一些blkid输出行。问题是,这些行包含空格,并且数组语法将它们作为单个数组元素的分隔符,因此我最终在我的数组中使用分割线而不是一行代表一个数组元素。
This is the code i currently have: devices=($(sudo blkid | egrep '^/dev/sd[b-z]'))
这是我目前的代码:devices =($(sudo blkid | egrep'^ / dev / sd [b-z]'))
echo ${devices[*]}
gives me the following output:
echo $ {devices [*]}给出了以下输出:
/dev/sdb1: LABEL="ARCH_201108" TYPE="udf"
/dev/sdc1: LABEL="WD" UUID="414ECD7B314A557F" TYPE="ntfs"
But echo ${#devices[*]}
gives me 7
but insted i want to have 2
. I want /dev/sdb1: LABEL="ARCH_201108" TYPE="udf"
to be the first element in my devices array and /dev/sdc1: LABEL="WD" UUID="414ECD7B314A557F" TYPE="ntfs"
to be the second one. How can i accomplish that?
但echo $ {#devices [*]}给了我7但是我想要有2.我希望/ dev / sdb1:LABEL =“ARCH_201108”TYPE =“udf”成为我的设备数组和/ dev中的第一个元素/ sdc1:LABEL =“WD”UUID =“414ECD7B314A557F”TYPE =“ntfs”是第二个。我怎么能做到这一点?
1 个解决方案
#1
13
Array elements are split on the IFS value. If you want to split on newline, adjust IFS:
数组元素在IFS值上拆分。如果要拆分换行符,请调整IFS:
IFS_backup=$IFS
IFS=$'\n'
devices=($(sudo blkid | egrep '^/dev/sd[b-z]'))
IFS=$IFS_backup
echo ${#devices[@]}
#1
13
Array elements are split on the IFS value. If you want to split on newline, adjust IFS:
数组元素在IFS值上拆分。如果要拆分换行符,请调整IFS:
IFS_backup=$IFS
IFS=$'\n'
devices=($(sudo blkid | egrep '^/dev/sd[b-z]'))
IFS=$IFS_backup
echo ${#devices[@]}