在“Downloads”目录中,我有两个目录 v1 和 v2。以下脚本的输出放在 v1 中,而不是“Downloads”中。为什么?我从“Downloads”目录运行脚本。
#!/usr/bin/env bash
# Full path to v1
v1='/mnt/c/Users/myusername/Downloads/v1'
# Full path to v2
v2='/mnt/c/Users/myusername/Downloads/v2'
# Loop through files in v1 and compare with v2
for file in "$v1"/*
do
if [ -f "$file" ]; then
fileInv2=$(find "$v2" -type f -iname "$(basename "$file")")
if [ -f "$fileInv2" ]; then
sort "$file" "$fileInv2" | uniq -u > "${file}_Diff_Output.txt"
fi
fi
done
Q2) 是否可以创建一个名为“...../Downloads/output”的文件夹并将输出文件重定向到输出文件夹。四个点表示前导路径可以是任何路径。由于我从“Downloads”文件夹运行脚本,因此我希望输出文件转到“...../Downloads/output”
输出文件正在写入
v1
目录中,因为您将输出指定为并且是 来自
${file}
的输入文件的完整路径名 。v1
for file in "$v1"/*
您可以使用类似以下结构指定另一个目录中的输出文件
为了清晰和简单起见,您可能希望将其分配
$(basename "$file")
给另一个变量。