也许这里有人能启发我:
职能:
add_repo() {
for repo in "${repos[@]}"; do
sudo flatpak remote-add --if-not-exists "$repo"
done
}剧本:
#! /usr/bin/env bash
repos=(
"flathub https://flathub.org/repo/flathub.flatpakrepo"
)
source function
add_repo产出:
sudo flatpak remote-add --if-not-exists 'flathub https://flathub.org/repo/flathub.flatpakrepo'$repo上没有引号的输出
sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo这是所需的输出。
问:如果不添加单引号,我如何引用变量?我知道,set -x (仅为调试设置)添加了这些单引号,以便进行更好的读取,但由于添加了单引号,即使没有set -x,命令也会在--if-not-exists之后结束。
发布于 2022-07-29 10:35:27
当使用列表中的值子集组时,可以方便地使用函数或命令从列表的值中弹出作为参数传递的所需值。
在这里,创建addrepos bash命令,这样就可以使用sudo调用它,只提升一次特权来添加所有存储库。
#!/usr/bin/env bash
# addrepos NAME URL [ NAME URL ] ...
# While there are arguments
while [ "$#" -gt 0 ]; do
# Pops repository name from arguments
repo_name="${1}"
shift
# Pops repository URL from arguments
repo_url="${1}"
shift
flatpak remote-add --if-not-exists "${repo_name}" "${repo_url}"
done使其可执行:
chmod +x addrepos主脚本使用addrepo调用sudo脚本
#! /usr/bin/env bash
# repository name and url are distinct entries in the repos array
repos=(
'flathub' 'https://flathub.org/repo/flathub.flatpakrepo'
)
# sudo call addrepos with the content of the repos array as arguments
sudo ./addrepos "${repos[@]}"https://stackoverflow.com/questions/73163679
复制相似问题