从文件中读取变量并将其发送到命令

Reading a variable from a file and sending that to a command

我正在尝试使构建过程自动化,该过程需要从文件中检索依赖项列表,然后将其通过管道传输到命令中。我有一个循环来自动下载我正在构建的包,但我需要读取一个文件并获取依赖项列表。我想做的是:

cd ${pkg}
then read this line from a file called PKGBUILD: depends=('dep1' 'dep2')
paru -S dep1 dep2

有什么办法吗?我是 bash 的新手,但我已经(复制)了这段代码: 此代码的问题是它不会检索 AUR 中的某些依赖项,因此 makepkg 不会自动检索它们。

build_pkgs () {
    { echo; echo "Building AUR Packages - "; echo; }
    cd $DIR/aur_pkgs
    for pkg in "${PKGS[@]}"; do
        echo "Building ${pkg}..."
        cd ${pkg} && makepkg -s
        mv *.pkg.tar.zst $DIR/x86_64
        # Verify
        while true; do
            set -- $DIR/x86_64/$pkg-*
            if [[ -f "" ]]; then
                { echo; echo "Package '${pkg}' generated successfully."; echo; }
                break
            else
                { echo; echo "Failed to build '${pkg}', Exiting..." >&2; }
                { echo; exit 1; }
            fi
        done
        cd $DIR/aur_pkgs
    done    
}
read this line from a file called PKGBUILD: depends=('dep1' 'dep2')

Archlinux PKGBUILD 是自己用 bash 编写的 - 只需获取它并输出即可。

. PKGBUILD
echo "${depends[@]}"

您最好在子 shell 中“安全地”执行此操作,并使用 declare -p 传输变量的值,以免修改父环境。

但是makepkg本身是用bash写的,所以就see it's sources how it's doing and here.

无论如何,许多 archlinux 用户编写了他们自己的一小部分脚本来设置他们自己的存储库 - there are so many of them 并且他们都需要解决这个问题。与其重新发明轮子并修复与其他人一样的错误,不如考虑寻找现成的解决方案。