テキストファイルを繰り返し、各行の名前、プロジェクト番号、およびEメールフィールドをインポートして、送信するEメールテンプレートに置き換える方法を理解する必要があります。
これは send.txt というテキストファイルです。
Project 1,Jack,Chen,06,12,[email protected]
Project 2,Emily,Weiss,06,12,[email protected]
Project 3,Mary,Gonzalas,06,12,[email protected]
これはReminder.emailという電子メールテンプレートです。
Dear __FULLNAME__:
This is a kindly reminder that our __Project__ meeting will be held on today.
Best Regards,
CIS5027
したがって、テキストファイルの各行について、この電子メールテンプレートの次のフィールドを置き換える必要があります。名前:、、そしてプロジェクト。その値を使用すると、最初の行に対して実行できますが、すべての行に対して実行することはできません。
これは私のスクリプトです。
#
!/bin/sh
#Start your code from here
date= date "+%m/%d/%y"
print $date
#The following line is to scan a file called Event-member.data and return any lines with todays date and save them to a file called sendtoday.txt
grep -n $(date +"%m,%d") Event-member.data > sendtoday.txt
#The following line is to remove the first to characters of the file created above sendtoday.txt and output that to a file called send.txt.
cat sendtoday.txt | sed 's/^..//' > send.txt
#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.
p=$(awk -F ',' 'NR==1{print $1}' send.txt)
n=$(awk -F ',' 'NR==1{print $2}' send.txt)
l=$(awk -F ',' 'NR==1{print $3}' send.txt)
e=$(awk -F ',' 'NR==1{print $6}' send.txt)
echo $p $n $l $e
#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email > sendnow.txt
cat sendnow.txt
#Yet to be written ... send out modified email templates.
exit 0
########
これが生成する出力は次のとおりです。
06/12/14
Project 1 Jack Chen [email protected]
Dear Jack Chen :
This is a kindly reminder that our Project 1 meeting will be held on today.
Best Regards,
CIS5027
ご覧のとおり、フィールドは正しく置き換えられますが、ジャックチェンの場合にのみ該当します。 send.txtファイルには3行があるため、上記のテンプレートの3つの修正バージョンが必要です。
ベストアンサー1
それを使用する理由はありませんawk
。シェルで直接使用できますread
。一般的な形式は、read foo bar
最初のフィールドをとして保存し、$foo
各行の残りの部分をとして保存することです$bar
。したがって、あなたの場合は、次のことを行います。
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email;
done < file
カンマ区切りフィールドを読み取るようにIFS
設定されている場合は、入力フィールド区切り記号です。,
これにより、各フィールドを取得して変数に保存できます。 2つの追加変数foo
とbar
。これは、各フィールドに固有の変数名が必要で、6つのフィールドがあるためです。 4つの変数名のみを指定した場合、4番目の変数($e
)には最後のフィールドまで4つのフィールドが含まれます。
これで、スクリプトにさまざまなその他の構文エラーがあります。まず、shebang行が間違っています。#! /bin/sh
との間に空行があってはいけません。追加の割り当てのために#!
/bin/sh
出力var=`command`
コマンドを変数に渡すとき、または型を使用する必要がありますvar=$(command)
。それ以外の場合、コマンド自体は出力ではなく文字列として変数に割り当てられます。ついに、print
それはあなたが考えるものとは異なります。あなたはprintf
またはを探していますecho
。したがって、スクリプトを書くより良い方法は次のとおりです。
#!/bin/sh
date=$(date "+%m/%d/%y")
echo $date
## The following line is to scan a file called Event-member.data
## and return any lines with todays date and save them to a file
## called sendtoday.txt
grep -n $(date +"%m,%d") Event-member.data > sendtoday.txt
## The following line is to remove the first to characters of the file
## created above sendtoday.txt and output that to a file called
## send.txt.
## I rewrote this to avoid the useless use of cat.
sed 's/^..//' sendtoday.txt > send.txt
## This is where you use read
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email > sendnow.txt
cat sendnow.txt
## This is where you need to add the code that sends the emails. Something
## like this:
sendmail $e < sendnow.txt
done < send.txt
exit 0
########