gpt4 book ai didi

arrays - Bash:将 ls 保存在一个数组中 -> 只接收小写字母

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:43 27 4
gpt4 key购买 nike

我尝试将一个目录的 ls 保存在一个数组中,这很有效,但是当我在数组中查看时,所有目录和文件的名称都是小写字母。我必须更改什么,以便文件和目录的名称保留在它们的旧符号中?像“Picture of Dog.jpg”这样的文件也是数组中的 3 个条目:Picture, of, Dog.jpg是否也有可能改变它?

我的代码:

#!/bin/bash
currentDir="/home/marius/"
declare -l content=(` ls $currentDir `)
printf -- '%s ' "${content[@]}"

最佳答案

您正在使用 declare -l 根据定义将其转换为小写。根据 help declare:

-l  to convert NAMEs to lower case on assignment

不用 ls 就可以创建数组:

declare -a content=("$currentDir"/*)

关于arrays - Bash:将 ls 保存在一个数组中 -> 只接收小写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com