0

我正在尝试编写一个 shell 脚本,它读取存储在一个目录中的文件的名称,然后在另一个目录中匹配这些文件。如果两者匹配,则会向收件人发送一封电子邮件。

这是我到目前为止所写的

 OUTFILES=/remote/output

 cd $OUTFILES
for FILE in XML*.email
do
  RECIPIENT=`head -1 $FILE`
  ATTACHMENT=`head -2 $FILE | tail -1`

echo $RECIPIENT
echo $ATTACHMENT

ATTACHFILES=:/projects/scripts

  cd $ATTACHFILES
         for FILE in *.pdf do
            ATTACH=`ls -1 $FILE`

    if [ $ATTACHMENT=$ATTACH ]; then


        echo $ATTACH

    fi
done

uuencode $ATTACH $ATTACH | mail $RECIPIENT 2>&1

脚本在这一行失败

ATTACH=`ls -1 $FILE`

是因为我试图在同一个脚本中读取 2 个 $FILES

谢谢

4

1 回答 1

0

尝试在等号周围放置空格:

if [ $ATTACHMENT = $ATTACH ]; then

此外,在使用变量的所有情况下,您确实应该使用双引号:

OUTFILES=/remote/output

cd "$OUTFILES"
for FILE in XML*.email
do
  RECIPIENT=`head -1 "$FILE"`
  ATTACHMENT=`head -2 "$FILE" | tail -1`

  echo "$RECIPIENT"
  echo "$ATTACHMENT"

  ATTACHFILES=:/projects/scripts

  cd "$ATTACHFILES"
  for FILE in *.pdf
  do
    ATTACH=`ls -1 "$FILE"`

    if [ "$ATTACHMENT" = "$ATTACH" ]
    then
      echo $ATTACH
    fi
  done
done  # I inserted this "done" because as indention shows this was missing.
uuencode "$ATTACH" "$ATTACH" | mail "$RECIPIENT" 2>&1
于 2013-11-06T11:52:32.150 回答