Bash 如何从子shell获取变量,同时使用xargs传递参数?

Bash 如何从子shell获取变量,同时使用xargs传递参数?,bash,Bash,我想将文件名传递给xargs,并在子shell中处理它,然后将某些变量从子shell获取到当前shell。出于并行化的目的,我更喜欢这里的xargs 下面是一个示例代码: files_with_issues=() printf "%s\n" ${list_of_file_names[@]} | xargs -n1 -P4 -I {file} bash -c ' # Process the file ... exit_status=$? [ ${ex

我想将文件名传递给
xargs
,并在子shell中处理它,然后将某些变量从子shell获取到当前shell。出于并行化的目的,我更喜欢这里的
xargs

下面是一个示例代码:

files_with_issues=()
printf "%s\n" ${list_of_file_names[@]} | xargs -n1 -P4 -I {file} bash -c '
        # Process the file ...
        exit_status=$?
        [ ${exit_status} -ne 0 ] && files_with_issues+=({file})'

echo "${files_with_issues[@]}"
我正在考虑使用类似于
source
的方法从子shell中获取变量。然而,我不知道如何做到这一点?

回答新问题 如果需要对每个文件执行复杂的操作,则需要将其作为标准输入或参数传递给脚本。然后,脚本可以输出NUL分隔的文件名,然后可以在数组中捕获这些文件名:

while IFS= read -r -d '' -u 9 file
do
    files_with_issues+=("$file")
done 9< <(./process_files.sh <<< "${list_of_file_names[@]}")
你能行

$ info="$(echo foo | xargs -I {file} bash -c 'echo "Information about {file}"')"

似乎唯一的方法是将
变量
写入一个文件,然后从主shell捕获它。我是这样处理的:

capture="capture.log"
text_to_capture="file_with_an_issue: "
[ -e ${capture} ] && rm ${capture}     #(1)
printf "%s\n" ${list_of_file_names[@]} | xargs -n1 -P4 -I {file} bash -c '
    # Process the file ...
    exit_status=$?
    [ ${exit_status} -ne 0 ] && echo "${text_to_capture}{file}" >> ${capture}'     #(2)

files_with_issues=$(grep -oP "(?<=${text_to_capture}).*" ${capture})     #(3)
capture=“capture.log”
text_to_capture=“有问题的文件:”
[-e${capture}]&rm${capture}(1)
printf“%s\n”${文件名列表[@]}xargs-n1-P4-I{file}bash-c'
#正在处理文件。。。
退出状态=$?
[${exit_status}-ne 0]&&echo“${text_to_capture}{file}”>>${capture}”(2)

files_with_issues=$(grep-oP)(?我需要在subshell中进行一些处理,在执行过程中会有几个输出。然后我只想将一些特定的变量添加到当前shell中。如果您编写一个以路径作为参数或标准输入的程序,那么将比
bash-c
容易得多。
capture="capture.log"
text_to_capture="file_with_an_issue: "
[ -e ${capture} ] && rm ${capture}     #(1)
printf "%s\n" ${list_of_file_names[@]} | xargs -n1 -P4 -I {file} bash -c '
    # Process the file ...
    exit_status=$?
    [ ${exit_status} -ne 0 ] && echo "${text_to_capture}{file}" >> ${capture}'     #(2)

files_with_issues=$(grep -oP "(?<=${text_to_capture}).*" ${capture})     #(3)