Shell脚本结果输出到文件

时间:2016-04-12 02:42:51

标签: shell

我刚遇到有关shell脚本输出问题的问题。这是代码

read_each_user_rating(){
TOTAL_RATING_NUMBER="$(grep -c '<Author>' $1)" #Find how many rating in each file
ALL_AUTHOR="$(grep '<Author>' $1 | sed -e 's/<Author>//'| tr -d '\r')"
ALL_COMMENT="$(grep '<Content>' $1 | sed -e 's/<Content>//'| tr -d '\r')"
ALL_DATE="$(grep '<Date>' $1 | sed -e 's/<Date>//'| tr -d '\r')"
ALL_RATING_FILE="$(grep '<Overall>' $1 | sed -e 's/<Overall>//'| tr -d '\r')"
ALL_VALUE="$(grep '<Value>' $1 | sed -e 's/<Value>//'| tr -d '\r')"
ALL_ROOMS="$(grep '<Rooms>' $1 | sed -e 's/<Rooms>//'| tr -d '\r')"
ALL_LOCATION="$(grep '<Location>' $1 | sed -e 's/<Location>//'| tr -d '\r')"
ALL_CLEANLINESS="$(grep '<Cleanliness>' $1 | sed -e 's/<Cleanliness>//'| tr -d '\r')"
ALL_CHECKIN="$(grep '<Check in / front desk>' $1 | sed -e 's/<Check in / front desk>//'| tr -d '\r')"
ALL_SERVICE="$(grep '<Service>' $1 | sed -e 's/<Service>//'| tr -d '\r')"
ALL_BUSSINESS="$(grep '<Bussiness service>' $1 | sed -e 's/<Bussiness service>//'| tr -d '\r')"
for ((COUNTER_A=1;COUNTER_A<=$TOTAL_RATING_NUMBER;COUNTER_A++))
do
echo "INSERT INTO UserRating (Author,Comment,Date,Overall,Value,Rooms,Locations,Cleanliness,Checkin,Service,Bussiness)" >> hotelreviews.sql
echo $($ALL_AUTHOR | sed "${COUNTER_A}q;d") >> hotelreviews.sql
done
}
read_each_user_rating $1

我可以输出      echo“INSERT INTO UserRating(作者,评论,日期,整体,价值,房间,地点,清洁度,签到,服务,商务)”&gt;&gt; hotelreviews.sql到该文件。但为什么我可以输出“echo $($ ALL_AUTHOR | sed”$ {COUNTER_A} q; d“)&gt;&gt; hotelreviews.sql”part to to file?

1 个答案:

答案 0 :(得分:0)

echo $($ALL_AUTHOR | sed "${COUNTER_A}q;d") >> hotelreviews.sql

格格不入。 $()中的表达式必须是有效命令($ALL_AUTHOR几乎肯定不是)。

您更有可能需要以下内容:

echo "$ALL_AUTHOR" | sed "${COUNTER_A}q;d"

$()内。

然而,在这种情况下,当您可以完成时,几乎可以肯定不需要在所有中调用子命令

echo "$ALL_AUTHOR" | sed "${COUNTER_A}q;d" >>hotelreviews.sql