Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 仅打印bash数组中的文件名_Arrays_Bash - Fatal编程技术网

Arrays 仅打印bash数组中的文件名

Arrays 仅打印bash数组中的文件名,arrays,bash,Arrays,Bash,我有一个数组和循环打印出文件名+扩展名,但我只想打印文件名。我该怎么做 tffilearray=(`find ./ -maxdepth 1 -name "*.json"`) for filepath in "${tffilearray[@]}"; do echo $filepath done 明白了 for filepath in "${tffilearray[@]}"; do basename $filepath .jso

我有一个数组和循环打印出文件名+扩展名,但我只想打印文件名。我该怎么做

tffilearray=(`find ./ -maxdepth 1 -name "*.json"`)
for filepath in "${tffilearray[@]}"; do
    echo $filepath
done
明白了

for filepath in "${tffilearray[@]}"; do  basename $filepath .json; done

正如Cyrus指出的,使用shell通配符比
find
(因为您不需要搜索多个目录或应用复杂的搜索规则)更简洁;此外,您还可以将shell模式规则(来自重复问题)一次性应用于整个数组:

tffilearray=(*.json)    # Get all the *full* filenames
tffilearray=("${tffilearray[@]%.*}")    # Trim off the extensions

[我标记这个社区Wiki是为了避免在一个几乎重复的问题上出现问题。]

Btw:我建议删除“${tffilearray[@]}”中文件路径的第一行并替换为
;对于*.json中的文件路径,使用
执行
;执行
。请将问题标记为重复,因为您的答案已在建议的重复中提及。