【发布时间】:2021-01-20 21:57:08
【问题描述】:
TL;DR
我在 bash 中有两个数组变量,archPkgs 和 npmPkgs。稍后在我的代码中,我有一个变量变量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 行。
目前我不太确定我的选择是什么。我粘贴了脚本,其中所有更改系统的行都被注释掉了,但是在运行它之前你还是应该看看它。
显然剧本还没有完成,我打算早点重新安排很多东西。
【问题讨论】:
-
这将是一个更好的问题,您将其简化为仅显示问题的最小示例,而不是给我们您的整个脚本并让我们弄清楚。
-
不过,在 bash 中,您可以使用
declare -a array_var声明和排列数组。谷歌“bash 数组变量”,你会发现很多页面回答了你的问题。如果您无法弄清楚,请通过最小的示例提出有针对性的问题。 -
@joanis 好吧,我把脚本删减了一点。我也尝试了该变量,但它仍然无法正常工作。我将脚本顶部的数组更改为
declare -a arrayname() -
您使用的
+=语法不适用于数组。我推荐的 Google 搜索给了我这个页面:opensource.com/article/18/5/… 在其中查找“填充数组”。 -
ShellCheck 有一些提示,例如
Line 125: $pkgPlatform+=pkgName SC2281: Don't use $ on the left side of assignments.
标签: python node.js bash git npm