通过shell自动化脚本编辑.CSV文件

时间:2015-01-06 15:12:23

标签: linux shell svn error-handling

尝试在done语句周围执行下面的脚本时出错。代码的要点是让while语句在文件名中列出的文件的持续时间内执行,以便获取分支文件夹中每个文件位置的修订号。

filea=/home/filenames.log
fileb=/home/actions.log
filec=/home/revisions.log
filed=/home/final.log


count=1
while read Path do
Status=`sed -n "$count"p $fileb`
Revision=`svn info ${WORKSPACE}/$Path | grep "Revision" | awk '{print $2}'`
if `echo $Path | grep "UpgradeScript"` then
Results="Reverted - ROkere"
Details="Reverted per process"
else if `echo $Path | grep "tsu_includes/shell_scripts"` then
Results="Reverted - ROkere"
Details="Reverted per process"
else
Results="Verified - ROkere"
Details=""
fi
echo "$Path,$Status,$Revision,$Results,$Details" > $filed
count=`expr $count + 1`
done < $filea

1 个答案:

答案 0 :(得分:0)

  • dothen之前需要分号或换行符。
  • else if更改为elif
  • 更改

    if `echo $Path | grep "UpgradeScript"` then
    

    to(删除反引号,使用“here-string”和grep的-q选项)

    if grep -q "UpgradeScript" <<< "$Path"; then
    
  • “归档”只会包含一行。我假设您要添加>>而不是覆盖>


实际上,快速重写。您正在从2个文件中读取相应的行。更快地在shell中完成,而不是为文件中的每一行调用sed一次。

#!/bin/bash
filea=/home/filenames.log
fileb=/home/actions.log
filec=/home/revisions.log    # not used?
filed=/home/final.log

exec 3<"$filea"    # open $filea on fd 3
exec 4<"$fileb"    # open $fileb on fd 4

while read -u3 Path && read -u4 Status; do
    Revision=$(svn info "$WORKSPACE/$Path" | awk '/Revision/ {print $2}')
    if [[ "$Path" == *"UpgradeScript"* ]]; then
        Results="Reverted - ROkere"
        Details="Reverted per process"
    elif [[ "$Path" == *"tsu_includes/shell_scripts"* ]]; then
        Results="Reverted - ROkere"
        Details="Reverted per process"
    else
        Results="Verified - ROkere"
        Details=""
    fi
    echo "$Path,$Status,$Revision,$Results,$Details"
done > "$filed"

exec 3<&-   # close fd 3
exec 4<&-   # close fd 4