Bash:如何将元素添加到存储在变量中的数组中?

2024-09-28 21:49:01 发布

您现在位置:Python中文网/ 问答频道 /正文

TL;DR

我在bash中有两个数组变量:archPkgsnpmPkgs。稍后在我的代码中,我有一个变量pkgPlatform,它可以指向这两个变量中的任何一个。我想通过这个变量向数组中添加一个元素。我怎么能在bash中做到这一点

详细信息

我正在尝试用Python和Bash编写一个安装后脚本,我正在编写一个bast脚本来安装所有需要的软件包。下面是它的代码

# List of all Arch packages
archPkgs=(
        # Display server
        'xorg'

        # Display manager
        'lightdm'
        'lightdm-webkit2-greeter'
        'lightdm-webkit-theme-litarvan'

        # Editor
        'neovim-nightly-bin'

        # WM
        'i3-gaps'
        'i3lock'
        'i3status'
        'picom'
        'rofi'

        # Syncing
        'syncthing'

        # Printing
        'cups'

        # Networking
        'network-manager-applet'
        'avahi'
        'nss-mdns'
        'reflector'

        # Browsing
        'firefox'
        'firefox-nightly'

        # Terminals, shell, programming, and other programsn and utilities
        'kitty'
        'bash-completion'
        'nodejs'
        'npm'
        'python'
        'python-pip'

        # Notifications
        'dunst'

        # Gaming
        'steam'

        # Media
        'vlc'
        'playerctl'
        'spotify'

        # Audio
        'pasystray'
        'pulseaudio'
        'pulseaudio-alsa'
        'pulseaudio-bluetooth'
        'pavucontrol'
        'lib32-libpulse'
        'lib32-alsa-plugins'
        'alsa-utils'
        'alsa-firmware'

        # Fonts
        'noto-fonts-cjk'
        'noto-fonts-emoji'
        'ttf-iosevka'
        'ttf-liberation'
        'ttf-opensans'

        # Drive tools + backups
        'udiskie'
        'borg'

        # Power management
        'xfce4-power-manager'

        # Themes
        'arc-gtk-theme'
        'breeze'

        # Screenshots
        'flameshot'

        # Bluetooth
        'blueman'
        'bluez'
        'bluez-utils'
)

npmPkgs=(
    # Language servers
    'vim-language-server'
    'bash-language-server'

    # Type checkers
    'pyright'
)


while true; do
    echo Are there any packages you would like to manage? Enter [arch/npm] [add/remove][ name of package].
    read pkgPlatform pkgAction pkgName

    if [ "$pkgPlatform" == 'arch' ]; then
        pkgPlatform=archPkgs
    elif [ "$pkgPlatform" == 'npm' ]; then
        pkgPlatform=npmPkgs

    elif [ "$pkgPlatform" == 'quit' ] || [ "$pkgAction" == 'quit' ] || [ "$pkgAction" == 'quit' ]; then
        echo Continuing...
        break

    else
        echo "Unknown option."
        continue
    fi

    if [ "$pkgPlatform" != 'quit' ] && [ "$pkgAction" != 'quit' ] && [ "$pkgName" != 'quit' ]; then
        while true; do
            if [ "$pkgAction" == 'add' ]; then
                $pkgPlatform+=pkgName
                break

            elif [ "$pkgAction" == 'remove' ]; then
                unset "$pkgName"[$pkgPlatform]
                break

每当我运行它时,都会发生这种情况

[johnny@lj-laptop post_install_scripts]$ ./02_pkgs.sh 
Packages to be installed:

Arch Packages
xorg lightdm lightdm-webkit2-greeter lightdm-webkit-theme-litarvan neovim-nightly-bin i3-gaps i3lock i3status picom rofi syncthing cups network-manager-applet avahi nss-mdns reflector firefox firefox-nightly kitty bash-completion nodejs npm python python-pip dunst steam vlc playerctl spotify pasystray pulseaudio pulseaudio-alsa pulseaudio-bluetooth pavucontrol lib32-libpulse lib32-alsa-plugins alsa-utils alsa-firmware noto-fonts-cjk noto-fonts-emoji ttf-iosevka ttf-liberation ttf-opensans udiskie borg xfce4-power-manager arc-gtk-theme breeze flameshot blueman bluez bluez-utils

npm Packages
vim-language-server bash-language-server pyright
Are there any packages you would like to manage? Enter [arch/npm] [add/remove] [name of package].
When you are satisfied, enter "quit" instead of "add" or "remove".
npm add test
./02_pkgs.sh: line 153: npmPkgs+=pkgName: command not found
Packages to be installed:

Arch Packages
xorg lightdm lightdm-webkit2-greeter lightdm-webkit-theme-litarvan neovim-nightly-bin i3-gaps i3lock i3status picom rofi syncthing cups network-manager-applet avahi nss-mdns reflector firefox firefox-nightly kitty bash-completion nodejs npm python python-pip dunst steam vlc playerctl spotify pasystray pulseaudio pulseaudio-alsa pulseaudio-bluetooth pavucontrol lib32-libpulse lib32-alsa-plugins alsa-utils alsa-firmware noto-fonts-cjk noto-fonts-emoji ttf-iosevka ttf-liberation ttf-opensans udiskie borg xfce4-power-manager arc-gtk-theme breeze flameshot blueman bluez bluez-utils

npm Packages
vim-language-server bash-language-server pyright
Are there any packages you would like to manage? Enter [arch/npm] [add/remove] [name of package].
When you are satisfied, enter "quit" instead of "add" or "remove".

问题似乎在第153行

我不太确定在这一点上我的选择是什么。我粘贴了脚本,注释掉了所有的系统更改行,但是在运行它之前,您仍然应该查看它

显然剧本还没有完成,我计划早些时候重新安排很多东西


Tags: ofbashaddnpmservermanagerttffirefox
3条回答

改变

    if [ "$pkgPlatform" == 'arch' ]; then
        pkgPlatform=archPkgs
    elif [ "$pkgPlatform" == 'npm' ]; then
        pkgPlatform=npmPkgs

    if [ "$pkgPlatform" == 'arch' ] || [ "$pkgPlatform" == 'npm' ]; then
        declare -n platformPkgs="${pkgPlatform}Pkgs"

这使得platformPkgs成为所需数组的nameref。然后像普通数组变量一样使用platformPkgs

然后改变

            if [ "$pkgAction" == 'add' ]; then
                $pkgPlatform+=pkgName
                break

            elif [ "$pkgAction" == 'remove' ]; then
                unset "$pkgName"[$pkgPlatform]
                break

            if [ "$pkgAction" == 'add' ]; then
                platformPkgs+=( "$pkgName" )
                break

            elif [ "$pkgAction" == 'remove' ]; then
                ## not this => unset "platformPkgs[$pkgName]"
                # first, iterate through the array to find
                # the index for this value $pkgName,
                # then, unset "platformPkgs[$index]"
                # or rewrite the array except for the element at that index: 
                #   platformPkgs=( "${platformPkgs[@]:0:index}" "${platformPkgs[@]:index+1}" )
                break

您面临的错误是因为您没有将项正确附加到数组中

$pkgPlatform+=pkgName ##that way the bash script can't get what you mean

因此,当您向变量pkgPlatform添加一个项时,可以按如下方式执行:

pkgPlatform+=("$pkgName")

您希望第153行执行的是

npmPkgs+=($pkgName)

但是你的目标变量在一个变量中。请记住,shell的初衷是构造命令行,变量替换仅在使用$运算符将shell指向命令行的情况下进行

要做到这一点,蛮力的方法是

eval $pkgPlatform'+=($pkgname)'

这将替换目标名称,然后重新扫描生成的行

作为一个不相关的注释,您可以使用一些case语句来清理该脚本

相关问题 更多 >