2014-10-27 32 views
0

我希望精簡發送附件使用以下bash腳本固定的身體信息,查找文件,然後選擇傻子附件

#!/bin/sh 
echo "body of message" | mutt -s "subject" -a $(find /path/to/dir -type f -name "*$1*") -- $2 < /dev/null 

然而,有時find命令查找多個文件的附件。有沒有更具互動性的做法呢?例如,如果它找到文件xyz.pdf和xyz2.pdf,我可以選擇一個,然後繼續發送文件?

回答

1

您可以將find的輸出傳遞給select命令。這是一個循環,可讓您重複選擇選項列表中的項目,並使用 剛剛選擇的值運行循環體。

select attachment in $(find /path/to/dir -type f -name "*$1*"); do 
    echo "body of message" | mutt -s "subject" -a "$attachment" -- "$2" < /dev/null 
    break # To avoid prompting for another file to send 
done 

這並不理想;如果它找到任何名稱中帶有空格的文件,它將會中斷。您可以更仔細地瞭解如何構建文件列表(這超出了本答案的範圍),然後調用select命令。例如:

# Two poorly named files and one terribly named file 
possible=("file 1.txt" "file 2.txt" $'file\n3.txt') 

select attachment in "${possible[@]}"; do 
    echo "body of message" | ... 
    break 
done 
0
#!/bin/bash 

function inter { 
    typeset file array i=0 
    while IFS= read -r -d $'\0' file; do 
     array[i]=$file 
     ((i+=1)) 
    done 
    if ((i == 0)); then 
     echo "no file" >&2 
     exit 1 
    fi 
    if ((i == 1)); then 
     echo "$array" 
     return 
    fi 
    select file in "${array[@]}"; do 
     if ((REPLY>=1 && REPLY<=i)); then 
      break 
     fi 
    done </dev/tty 
    echo "$file" 
} 

echo "body of message" | mutt -s "subject" -a "$(find /path/to/dir -type f -name "*$1*" -print0 | inter)" -- $2 < /dev/null 
相關問題