0

I have tried the following questions:

I was running docker-compose that checks Dockerfile.

See my script:

archlinux="pacman"

get_package_manager_install() { if [[ -x "$(command -v $1)" ]]; then "$1 $2 $3" else
echo "Gestor do pacote desconhecido" &>/dev/null; fi }

get_package_manager_install "$archlinux" "-S --noconfirm --needed" "ca-certificates curl git p7zip wget unzip zstd" get_package_manager_install "$archlinux" "-S --noconfirm --needed" "htop micro neofetch neovim zsh" get_package_manager_install "$archlinux" "-S --noconfirm --needed" "luit xdotool xdriinfo xorg-appres xorg-iceauth xorg-xcmsdb xorg-xgamma xorg-sessreg xorg-xdpyinfo xorg-xev xorg-xfd xorg-xfontsel xorg-xhost xorg-xkbcomp xorg-xkill xorg-xlsatoms xorg-xlsclients xorg-xlsfonts xorg-xmessage xorg-xmodmap xorg-xprop xorg-xrandr xorg-xrdb xorg-xrefresh xorg-xset xorg-xvidtune xorg-xvinfo xorg-xwininfo"

Observe that I also already have replaced "$1 $2 $3" with $@ or $*. I also attempted $(echo "$@") and $(echo "${@}")

I also have already replaced " " with ' ' in the last parameter, but unsuccessfully.

It cut the last part of the parameter, for example, git p7zip wget unzip zstd and whole htop micro neofetch neovim zsh will not be installed.

Oo'-
  • 243
  • I see no $* or ~$@` in your script. – ctrl-alt-delor Mar 06 '22 at 11:03
  • I can't tell what you mean Bash "cutting the last part of the third parameter". If you want to know why some particular result happens, it would help if you show it. Maybe with the package manager replaced with e.g. printf "<%s>\n" ... which would print the args in a somewhat unambiguous format. (Also note that $*, $@, "$*" and "$@" are different.) – ilkkachu Mar 06 '22 at 11:10
  • But in general, you probably shouldn't try to shove multiple strings/arguments into one string/argument, it's just bound to give you problems. See How can we run a command stored in a variable? for similar issues and some explanation. The solution of using an array isn't really needed here but you'll get similar issues with unquoted variable expansions. – ilkkachu Mar 06 '22 at 11:12

1 Answers1

4

You need -S, --noconfirm, --needed and the packages to be passed as separate arguments to pacman, so:

archlinux="pacman"

get_package_manager_install() { if command -v -- "$1" > /dev/null 2>&1; then "$@" else
echo >&2 "Gestor do pacote desconhecido" return 1 fi }

get_package_manager_install "$archlinux" -S --noconfirm --needed ca-certificates curl git p7zip wget unzip zstd

Where get_package_manager_install is passed all those as separate arguments, and get_package_manager_install passes them as-is to pacman.

If you wanted get_package_manager_install to take only 3 arguments, and the second and third to be split on space characters to generate the list of arguments to pass to pacman, you could use the split+glob operator (which you used already by mistake by forgetting to quote $1 in your code), after having disabled the glob part and set $IFS to space:

get_package_manager_install() {
  if command -v -- "$1" > /dev/null 2>&1; then
    (
      set -o noglob
      IFS=' '
      exec "$1" $2 $2
    )
  else        
    echo >&2 "Gestor do pacote desconhecido"
    return 1
  fi
}

get_package_manager_install "$archlinux"
'-S --noconfirm --needed' 'ca-certificates curl git p7zip wget unzip zstd'

Though I can't imagine why you'd want to do that.

It's also not clear to me why you'd name the variable holding the name of the package manager archlinux which is the name of a Linux distribution.

  • If get_package_manager_install "$mageia" "" "ca-certificates curl git p7zip wget unzip zstd", would it be get_package_manager_install "$mageia" ca-certificates curl git p7zip wget unzip zstd? Observe that the second parameter is empty because uprmi does not have any option for installing. – Oo'- Mar 06 '22 at 09:30
  • As for the archlinux variable, check https://unix.stackexchange.com/questions/690666/bash-function-condition-comand-argument-misinterprets-strangely-and-wrongly-ubun. I used to use the original commands, but the Bash functions do not recognise the original command. – Oo'- Mar 06 '22 at 09:33
  • To better understand, you can check my file (observe that it is a bit old): https://github.com/gusbemacbe/complete-linux/blob/main/scripts/1-root – Oo'- Mar 06 '22 at 09:35
  • @stéphane-chanzelas, I tested both your examples, but I checked which zsh, it informed zsh does not exist. My script seems to have more problems. Sorry! :-/ – Oo'- Mar 06 '22 at 12:56
  • Ah, please forgive me for my stupidity, I investigated and figured out why git, htop, micro, neofetch, zsh, etc., weren't installed, then it turned that there were files that already existed in the filesystem before. I had to remove them from my project, and they were installed. Only your first example worked. The next time, I have to inspect the log I export with tee to check the errors. – Oo'- Mar 06 '22 at 15:23