递归Arch Linux Shell脚本以获得依赖关系

时间:2018-03-26 16:15:15

标签: bash list recursion dependencies archlinux

我编写了一个shell脚本,用于将指定包中的所有依赖项打印到文件中。显然它不起作用(否则我不会在这里大声笑)。我是shell脚本/ bash编程的新手。我在Arch Linux上运行并搜索网络,让我到达我所在的位置。但是现在我从“空字符串包名”中得到了一堆错误。它开始不错,然后它是一个无尽的厄运循环。我目前的代码是:

#!/bin/bash

echo -n "Enter a package: "
read p

echo "Searching through package...$p"

get_dependencies() {
    # Make sure we get the package too...
    pacman -Sp "$1" >> myPackages.list

    # Get dependency list from current package and output to tmp file
    pacman -Si "$1" | awk -F'[:<=>' '/^Depends/ {print $2}' | xargs -n1 | sort -u > depList.list

    # Read from that output file and store in array
    listArray=()
    while read -r input ; do
        listArray+=("$input")
    done < "depList.list"

    # Get the number of dependencies
    numList=${#listArray[@]}

    echo "$numList dependencies from $1"
    echo "Delving deeper.."

    # Loop through each depend and get all those dependencies
    for i in "${listArray[@]}" ; do
        get_dependencies "$i"
    done
}

# Get dependcies of package that user typed
get_dependencies "$p"

# Finished
echo "Done!"

1 个答案:

答案 0 :(得分:0)

为避免出现循环依赖关系,您可以在单独的文件中跟踪您访问过的软件包以及尚未访问的软件包。在降级为依赖关系之前比较这些文件有望使您的脚本免于麻烦。

deps() {
   pacman -Si "$1" |
   awk -F'[:<=>]' '/^Depends/ {print $2}' |
   xargs -n1 |
   sort -u |
   grep -v None
}

alldeps() {
   # needed files, potentially cached for later
   unseen_f=unseen.$1.txt
   seen_f=seen.$1.txt
   deps_f=deps.$1.txt
   # start of with the root packaage
   echo $1 > $unseen_f
   # while we still have unseen packages, find depends
   while [ $(sed /^$/d $unseen_f |wc -l ) -gt 0 ]; do
      # read in all unseen, and get their deps
      for d in $(cat $unseen_f); do
         echo $d >> $seen_f
         deps $d >> $deps_f
      done
      # those in deps but not in seen to go unseen
      # we'll finish when unseen is empty: nothing in deps we haven't seen
      comm -23 <(sort -u $deps_f) <(sort -u $seen_f) > $unseen_f
   done
   cat $seen_f
   #sort -u $seen_f $deps_f
   # rm $seen_f $deps_f $unseen_f
}
alldeps xterm