0

Hi I'm try to do an installer which is compatible among different linux distros. It is possible to achieve something like this (see the last line of code).

PACKAGE_MANAGER=$1
INSTALL_PACKAGE_CMD=""

# CHECK PACKAGE MANAGER
if [ "$s1" == apt ]
then INSTALL_PACKAGE_CMD="apt install -y" fi
if [ "$s1" == pacman ]
then INSTALL_PACKAGE_CMD="pacman --noconfirm --force" fi

# INSTALL PACKAGE 
# (using the selected package manager)
sudo $INSTALL_PACKAGE_CMD sqlite
5
  • Have you tried it? What happened? If not: Why? – Sven May 8 '18 at 21:16
  • No, it didn't work, that's why I'm opening a question. I'm looking for an alternative. – Adrian Lopez May 8 '18 at 21:20
  • It throws the error: 'sudo: sqlite: command not found'. – Adrian Lopez May 8 '18 at 21:21
  • 3
    Sounds like you just need this: xkcd.com/1654 – Jed Daniels May 8 '18 at 21:24
  • This does not deserve an answer :-). You're assigning $1 to PACKAGE_MANAGER, but use $s1 for deciding later on. This way $s1 is always empty an you are trying to run sudo sqlite. Either assign $1 to s1 or use PACKAGE_MANAGER in your if statements. You should also check whether there is something in $1 and fail or set the PM to some reasonable default value if it is empty. – Ondřej Xicht Světlík May 10 '18 at 5:34
2

Unlike in C, the line endings are not ignored by bash. Try this:

s1=$1
INSTALL_PACKAGE_CMD=""

# CHECK PACKAGE MANAGER
if [ "$s1" == apt ] ; then
    INSTALL_PACKAGE_CMD="apt install -y" 
fi
if [ "$s1" == pacman ] ; then 
    INSTALL_PACKAGE_CMD="pacman -S --noconfirm --force"
fi

# INSTALL PACKAGE 
sudo $INSTALL_PACKAGE_CMD sqlite
1
  • 3
    The reason this works is because "$s1" in the OP was never set, so $INSTALL_PACKAGE_CMD always resulted in "". So the command "sudo $INSTALL_PACKAGE_CMD sqlite" always evaluated to "sudo sqlite". – Jed Daniels May 8 '18 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.