Bash 脚本帮助

Bash 脚本帮助

apt 中的 --print-uris 将打印整个输出以及指定 txt 文件的 URL。我需要一个命令,它只会从 print uris 的输出中选择要安装的包并将它们重定向到文件。您可能已经猜到了,我正在尝试创建一个脚本。提前致谢

答案1

Mowiewowie,超级牛能力的又一证明!!也许这个名为的文件supercow有帮助:

#!/bin/bash

# exactly one argument
[[ $# -ne 1 ]] && echo "Usage: $( basename $0 ) PACKAGE" && exit 1

PACKAGE_DIRECTORY=$( mktemp -d )    # Target directory
PACKAGE_LOGFILE=$( mktemp )         # Target file
cd $PACKAGE_DIRECTORY || exit 1     # Exit if directory does not exist
apt_out_all=$(( apt-get --print-uris --yes install $1 ) 2>&1 )

apt_out_check=$( echo $apt_out_all | sed s/^.*information...\ // )

# error checks
if ! [[ $( echo $apt_out_check | grep "packages will be installed" ) ]]
then
    echo -n "ERROR: "

    if   [[ "${apt_out_check%% *}" == "E:" ]]
        then echo "Unable to locate package $1."

    elif [[ "${apt_out_check%% *}" == "$1" ]]
        then echo "$1 already installed." # look at /var/cache/apt/archives

    # other, eg: Package solid is not available, but is referred to ...
    else echo "${apt_out_check}"
    fi
    exit 1
fi

# loop through all lines: tee to file if package, wget uri if not empty
for i in $apt_out_all ; do
    uri=$(  echo $i | tee >(grep ".deb$" >> $PACKAGE_LOGFILE) \
                    | grep "^'http" | sed -e s/^\'// -e s/\'$// )
    [[ $uri ]] && if ! wget $uri ; then
                echo "ERROR: wget failed" ; exit 1 ; fi
done

echo "Here is the content of the logfile:" ; cat $PACKAGE_LOGFILE
echo -e "\nHere is the content of the directory:" ; ls $PACKAGE_DIRECTORY

我的第一次尝试失败了,因为没有检查疯狂的用户输入,也没有检查软件包是否已安装。此外,如果已经gcc安装,似乎会丢弃错误字符串,例如geany只丢弃一个空行。因此,尽管这次尝试感觉更可靠,但我仍会谨慎使用它。我还删除了双重检查等。

例子:$ bash supercow kobodeluxe

#... wget messages

Here is the content of the logfile:
kobodeluxe-data_0.5.1-6_all.deb
kobodeluxe_0.5.1-6_amd64.deb

Here is the content of the directory:
kobodeluxe_0.5.1-6_amd64.deb  kobodeluxe-data_0.5.1-6_all.deb

例子:$ bash supercow bash

bash already installed.

例子:$ bash supercow windows3.11

Unable to locate package windows3.11.

例子:$bash supercow solid

ERROR: Package solid is not available, but is referred to by another package.
#...

希望这有帮助!

编辑问题:然后用grep ".deb$" >> $PACKAGE_LOGFILE括号内的内容替换,grep ".deb$" | sed s/_.*// >> $PACKAGE_LOGFILE因为我不知道包含下划线的官方包,除非我弄错了。

相关内容