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
Adrian Lopez
  • 181
  • 6
  • Have you tried it? What happened? If not: Why? – Sven May 08 '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 08 '18 at 21:20
  • It throws the error: 'sudo: sqlite: command not found'. – Adrian Lopez May 08 '18 at 21:21
  • 3
    Sounds like you just need this: https://xkcd.com/1654/ – Jed Daniels May 08 '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 05:34

1 Answers1

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
Adrian Lopez
  • 181
  • 6
kubanczyk
  • 13,502
  • 5
  • 40
  • 55
  • 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 08 '18 at 21:39