加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

什么是bash中的列表?

发布时间:2020-12-15 17:03:27 所属栏目:安全 来源:网络整理
导读:我在这里搜索过类似的问题,但令人惊讶的是找不到任何问题. 在GNU bash中,有一个名为“arrays”的(构造?结构?数据类型?).数组在bash文档中有详细记录,所以我认为我理解了基础知识. 但突然间,在文档中也出现了“列表”一词.例如,在谈论filename expansion(
我在这里搜索过类似的问题,但令人惊讶的是找不到任何问题.

在GNU bash中,有一个名为“arrays”的(构造?结构?数据类型?).数组在bash文档中有详细记录,所以我认为我理解了基础知识.

但突然间,在文档中也出现了“列表”一词.例如,在谈论filename expansion(重点是我的)时使用它:

If one of these characters appears,then the word is regarded as a pattern,and replaced with an alphabetically sorted list of filenames matching the pattern (see Pattern Matching).

因此,我有三个问题:

>“列表”在这里意味着什么?
>它的用法与for loop description中的含义相同吗?
>我在某种程度上迷失在bash的空白世界中.如果这个“列表”是一个单独的数组概念(我认为),它是在处理空格和IFS时,还是以与数组相同的方式处理?

在讨论一个或多个管道的序列时有another use of the “list” term,但我知道它很可能意味着不同类型的列表.

UPDATE

>因为我看到这个“列表结构”的工作方式与数组的工作方式非常相似 – 它们之间有什么区别?

更新2

>“列表”比数组更受欢迎的用例有哪些?例如,让我们进行比较.让我们创建两个文件:

$touch file1.txt file2.txt

说到列表,我可以执行以下操作:

$A=*.txt ; echo $A
file1.txt file2.txt
$

当涉及到数组时,我可以执行以下操作:

$B=(*.txt) ; echo ${B[@]}
file1.txt file2.txt
$

虽然这两个结果完全相同,但是当数组和列表返回不同的结果时是否存在任何情况?

更新3

我可能会混淆一些东西,因为在上面的例子中,它似乎是一个“包裹”在数组中的列表.我不知道它是否有所作为.

Bash中没有名为list的数据类型.我们只有数组.在您引用的文档中,术语“列表”不是指数据类型(或任何技术) – 它只是指一系列文件名.

但是,就所考虑的顺序循环而言,glob expansions与数组元素非常相似:

for file in *.txt; do          # loop through the matching files
                               # no need to worry about white spaces or glob characters in file names
  echo "file=$file"
done

和…一样

files=(*.txt)                  # put the list of matching files in an array
for file in "${files[@]}"; do  # loop through the array
  echo "file=$file"
done

但是,如果您要对文件名进行硬编码,那么您需要引号以防止word splitting和globbing:

for file in verycramped.txt "quite spacious.txt" "too much space.txt" "*ry nights.txt"; do ...

要么

files=(verycramped.txt "quite spacious.txt" "too much space.txt" "*ry nights.txt")
for file in "${files[@]}"; do ...

在这里阅读更多关于分词的内容:

> Word Splitting – Greg’s Wiki
> Word Splitting – Bash Manual
> Word splitting in Bash with IFS set to a non-whitespace character
> I just assigned a variable,but echo $variable shows something else

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读