在阵列击子扩张 [英] Bash substring expansion on array

查看:262
本文介绍了在阵列击子扩张的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个给定后缀的一组文件。举例来说,我有一个后缀一组PDF文件的 .PDF 。我想用串扩张来获取文件的名称不带后缀的。

I have a set of files with a given suffix. For instance, I have a set of pdf files with suffix .pdf. I would like to obtain the names of the files without the suffix using substring expansion.

有关一个单一的文件,我可以使用:

For a single file I can use:

file="test.pdf"
echo ${file:0 -4}

要对所有文件执行此操作,我现在尝试:

To do this operation for all files, I now tried:

files=( $(ls *.pdf) )
ff=( "${files[@]:0: -4}" )
echo ${ff[@]}

我现在得到一个错误,指出串前pression< 0 ..

(我想避免使用循环)

推荐答案

使用的参数扩展,以除去像这样的 .PDF 部分:

Use parameter expansions to remove the .pdf part like so:

shopt -s nullglob
files=( *.pdf )
echo "${files[@]%.pdf}"

禁用了javascript -s nullglob 总是使用水珠时,一个好主意:它将使水珠扩大到什么,如果没有匹配

The shopt -s nullglob is always a good idea when using globs: it will make the glob expand to nothing if there are no matches.

$ {文件[@]%。PDF}将扩展到一个数组中的所有尾随 .PDF 删除。你可以,如果你希望把这个在另一个数组像这样:

"${files[@]%.pdf}" will expand to an array with all the trailing .pdf removed. You can, if you wish put this in another array as so:

files_noext=( "${files[@]%.pdf}" )

这一切是100%安全的关于文件名(空格,换行等)有趣的符号,除了回声命名文件的一部分 -n.pdf -e.pdf -E.pdf ...但回声只是这里演示的目的。你的文件=($(LS * .PDF))是真的很不错! 不要从不解析 LS的输出

All this is 100% safe regarding funny symbols in filenames (spaces, newlines, etc.), except for the echo part for files named -n.pdf, -e.pdf and -E.pdf... but the echo was just here for demonstration purposes. Your files=( $(ls *.pdf) ) is really really bad! Do never parse the output of ls.

要回答您的评论:串扩张不要在阵列中的每个领域工作。从上面链接的参考手册摘自:

To answer your comment: substring expansions don't work on each field of the array. Taken from the reference manual linked above:

$ {参数:偏移}

$ {参数:偏移:长度}

如果偏移计算为一个数字小于零,则该值被用​​作从参数的值的末尾偏移。如果长度计算为一个数字小于零,而参数不是 @ ,而不是索引或关联数组,它是PTED作为一个跨$ p $从参数,而不是字符数的值的末尾偏移,和膨胀是在两个偏移之间的字符。如果参数 @ ,结果是长度位置参数开始偏移。 如果参数 @ 下标索引数组名或 * ,结果是长度的数组成员开始与 $ {参数[偏移]} 负偏移相对于比指定数组的最大指数大于服用。适用于关联数组子串扩张产生不确定的结果。

If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. If length evaluates to a number less than zero, and parameter is not @ and not an indexed or associative array, it is interpreted as an offset from the end of the value of parameter rather than a number of characters, and the expansion is the characters between the two offsets. If parameter is @, the result is length positional parameters beginning at offset. If parameter is an indexed array name subscripted by @ or *, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Substring expansion applied to an associative array produces undefined results.

所以,如,

$ array=( zero one two three four five six seven eight )
$ echo "${array[@]:3:2}"
three four
$

这篇关于在阵列击子扩张的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆