【问题标题】:Problems with script to parse "control" file解析“控制”文件的脚本问题
【发布时间】:2012-04-09 11:58:15
【问题描述】:

我正在编写一个函数来解析 debian 控制文件以获取包构建依赖项。下面是所说的功能,它不起作用。

Debian 控制文件仅由某些“标题”(即:Build-Depends:)分隔,后面总是跟一个冒号(:)。这导致多个依赖项可能会列在多行上,而同一行上没有 Build-Depends 标题(否则我已经可以使用它了)。

我看到的具体问题是我从 grep 行收到“找不到命令”错误。 “命令”是控制文件中的文字/文本。我对脚本的了解有限。我用“google”把它拼凑起来,所以我绝对会感谢任何提示。

function FindDep ()
{
  CheckVar=0
  echo "Running Find Depend."
  ControlPath=$TmpBuild/Deb-ConfFiles/$1/debian/control
  cat $ControlPath | while read line ;  
  do
    TempVar=`grep Build-Depends $line`
    if [ "$TempVar" != "" ]; then
       BuildDep=`sed s/Build-Depends://g $TempVar | sed s/Build-Depends-Indep\://g | grep -o '[a-z]*[-]*[a-z]*'`
       CheckVar=1
    elif [ $CheckVar == 1 ]; then
        TempVar=`grep : $line`
        if [ "TempVar" != "" ]; then   
           BuildDep="$BuildDep `sed s/Build-Depends://g $TempVar | sed s/Build-Depends-Indep\://g | grep -o '[a-z]*[-]*[a-z]*'`"
        else
           CheckVar=0
        fi
    fi
  done
  echo "Here is what is listed as dep for " $1 "--" $BuildDep
  for y in $BuildDep; do
    echo $y
    IsInstalled="dpkg -s $y | grep Status"
    if [ "$IsInstalled" == "" ]; then
      echo "installing " $y
      dpkg -i $y > /dev/null 2>&1
      if [ $? -gt 0 ]; then
        apt-get -f --force-yes --yes install >/dev/null 2>&1
      fi
      dpkg -i $y > /dev/null 2>&1
    fi
  done
  echo "Ending Find Depend"
}

【问题讨论】:

    标签: linux bash debian


    【解决方案1】:

    首先转义引用变量的每一行,例如,转这个...

    ControlPath=$TmpBuild/Deb-ConfFiles/$1/debian/control
    

    对此:

    ControlPath="$TmpBuild/Deb-ConfFiles/$1/debian/control"
    

    这就是为什么:

    $ y=hello world
    bash: world: command not found
    
    $ y="hello world"
    
    $ cat $y       
    cat: hello: No such file or directory
    cat: world: No such file or directory
    
    $ cat "$y"
    cat: hello world: No such file or directory
    

    【讨论】:

    • 感谢您的建议——我试了一下,不幸的是它没有任何区别。还有什么我应该看的吗?
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多