【问题标题】:Editing a .CSV file through shell automation script通过 shell 自动化脚本编辑 .CSV 文件
【发布时间】:2015-01-06 15:12:23
【问题描述】:

我在尝试围绕 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

【问题讨论】:

    标签: linux shell svn error-handling


    【解决方案1】:
    • dothen 之前需要一个分号或换行符。
    • else if 更改为elif
    • 改变

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

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

      if grep -q "UpgradeScript" <<< "$Path"; then
      
    • "filed" 只会包含一行。我假设您想附加 &gt;&gt; 而不是覆盖 &gt;


    实际上,快速重写。您正在从 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-12
      • 2016-01-21
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-11-29
      相关资源
      最近更新 更多