使用python改进bash脚本

使用python改进bash脚本

如何减少以下内容的重复?也就是说,有没有办法将两个几乎完全相同的陈述合并为一个?

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

关于python - 使用python改进bash脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14590955/

10-11 19:05