用python改进bash脚本

我将如何减less以下的冗余? 也就是说,有没有办法将两个几乎相同的陈述合并为一个?

FULFILLMENT="/Users/david/Desktop/pds" # "/Volumes/FulfilmentArray/" ARCH1="/Users/david/Desktop/etc" # "/Volumes/Arch_01/" FILE="/tmp/files.txt" # find all the paths and print them to a file sudo find $FULFILLMENT -ls | python -c ' import sys for line in sys.stdin: r = line.strip("\n").split(None, 10) fn = r.pop() print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\"" ' > $FILE && sudo find $ARCH1 -ls | python -c ' import sys for line in sys.stdin: r = line.strip("\n").split(None, 10) fn = r.pop() print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\"" ' >> $FILE 

查找可以通过一个命令经过多个目录:

 FULFILLMENT="/Users/david/Desktop/pds" # "/Volumes/FulfilmentArray/" ARCH1="/Users/david/Desktop/etc" # "/Volumes/Arch_01/" FILE="/tmp/files.txt" # find all the paths and print them to a file sudo find "$FULFILLMENT" "$ARCH1" -ls | python -c ' import sys for line in sys.stdin: r = line.strip("\n").split(None, 10) fn = r.pop() print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\"" ' > $FILE 

你可以做:

 cmd=' import sys for line in sys.stdin: r = line.strip("\n").split(None, 10) fn = r.pop() print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\"" ' sudo find $FULFILLMENT -ls | python -c "$cmd" >> $FILE sudo find $ARCH1 -ls | python -c "$cmd" >> $FILE 

虽然以下的输出略有不同,但我怀疑你会更开心:

 FULFILLMENT=/Users/david/Desktop/pds ARCH1=/Users/david/Desktop/etc exec > /tmp/files.txt find $FULFILLMENT $ARCH1 -exec stat -c '%i,%b,%A,%h,%U,%G,%y,%n' {} \; 

日期的格式是不同的,如果您没有使用POSIXLY_CORRECT调用findstat可能会报告不同的块大小。 这不会尝试在文件名中转义引号,也不要将文件名放在引号中,因为您显然不担心可能包含逗号的文件名,所以我们可以假设输出可以可靠地解析为csv,而不用担心引号。