2

我将如何减少以下冗余?也就是说,有没有办法将两个几乎相同的语句组合成一个?

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
4

3 回答 3

4

Find 可以在一个命令中遍历多个目录:

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 
于 2013-01-29T19:48:21.897 回答
1

你可以做:

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
于 2013-01-29T20:15:46.930 回答
1

尽管以下输出略有不同,但我怀疑您会更满意:

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' {} \;

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

于 2013-01-29T21:15:42.487 回答