我正在尝试将命名文件描述符与进程替换一起使用。
我写了下面的代码,但它不起作用:
# Open named file descriptors and associate to Process Substitution result
exec {folder1_files_list} < <( ls -v "${FOLDER1_PATH}"/* )
exec {folder2_files_list} < <( ls -v "${FOLDER2_PATH}"/* )
IFS=$'\n' read -r folder1_filename -u "${folder1_files_list}"
IFS=$'\n' read -r folder2_filename -u "${folder2_files_list}"
# Close named file descriptors
exec {folder1_files_list}<-
exec {folder2_files_list}<-
错误是:
exec: {folder1_files_list}: not found
我已经通读了 bash 手册,但可能遗漏了一些东西
答案1
这是为我们的下一代提供的完整有效代码:
exec {folder1_files_list}< <( ls -v -1 "${FOLDER1}"/* )
# Usage example:
# If I want to output the entire contents of the file descriptor,
# then I should write:
# cat - <&$folder1_files_list
exec {folder2_files_list}< <( ls -v -1 "${FOLDER2}"/* )
IFS=$'\n' read -r -u $folder1_files_list folder1_filename
IFS=$'\n' read -r -u $folder2_files_list folder2_filename
exec {folder1_files_list}<&-
exec {folder2_files_list}<&-