Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Unix Sh:将文件夹中的所有文件添加到阵列_Unix_Sh - Fatal编程技术网

Unix Sh:将文件夹中的所有文件添加到阵列

Unix Sh:将文件夹中的所有文件添加到阵列,unix,sh,Unix,Sh,我试图使用数组来存储文件夹中的所有.txt文件,该文件夹的名称不包含单词“silent”。到目前为止,我尝试了以下命令,但没有成功: ACTIVE_LOGS=($(`find $DEST_DIR -name '*.txt' '!' -name '*silent*'`)) ACTIVE_LOGS=($("find $DEST_DIR -name '*.txt' '!' -name '*silent*'")) 我必须指出,以下命令按预期工作: ACTIVE_LOGS=`find $DEST_D

我试图使用数组来存储文件夹中的所有
.txt
文件,该文件夹的名称不包含单词“silent”。到目前为止,我尝试了以下命令,但没有成功:

ACTIVE_LOGS=($(`find $DEST_DIR -name '*.txt'  '!' -name '*silent*'`))
ACTIVE_LOGS=($("find $DEST_DIR -name '*.txt'  '!' -name '*silent*'"))
我必须指出,以下命令按预期工作:

ACTIVE_LOGS=`find $DEST_DIR -name '*.txt'  '!' -name '*silent*'`

但是我需要一个数组而不是一个变量。

根据您使用的shell,将数组声明为shell变量可能需要用空格(我认为是制表符)分隔初始值。但是
find(1)
会打印出由换行符分隔的搜索结果。使用
-printf
而不是
-print
可以得到您想要的结果:

ACTIVE_LOGS=($(find $DEST_DIR -name '*.txt'  '!' -name '*silent*' -printf "%f "))
反引号也是多余的,因为
$()
已经生成了一个子shell

使用Bash数组,即使不需要修改换行符,也能按预期工作:

ACTIVE_LOGS=($(find $DEST_DIR -name '*.txt'  '!' -name '*silent*'))

在这两种情况下,您都在命令替换中添加了一层额外的引号;把它们取下来

ACTIVE_LOGS=($(find "$DEST_DIR" -name '*.txt'  '!' -name '*silent*'))
bash
(以及其他shell;语法可能会有所不同)中,您可以完全省去
find

shopt -s globstar nullglob extglob
ACTIVE_LOGS=( "$DESTDIR"/**/!(*silent*).txt )

globstar
允许您使用
**
匹配零个或多个目录<如果没有匹配项,code>nullglob会导致模式消失,而不是按字面意思处理;而且
extglob
允许您使用
!(*silent*)
以匹配模式以外的任何内容
*silent*

我不知道有任何shell需要特定类型的空格来分隔数组元素。您使用的shell是什么?POSIX
sh
不支持数组。