0

Running Ubuntu 16.04 I want to install (programmatically) getopts if it is not installed.

After this post I am doing something like this:

GETOPTS=getopts

if [$GETOPTS==""] 
   then
   echo "Installing getopts"
   sudo apt-get install -y libperl4-corelibs-perl
fi

But it is not working.

Any Idea how to do this?

IgorAlves
  • 1,132
  • 1
    What does "it is not working" exactly mean? What error message do you get? sudo usually asks for a password. Do you supply one? Which getopts do you want to install? libperl4-corelibs-perl contains Perl 4 libraries which is approximately 25 years old. – PerlDuck Mar 11 '18 at 12:51
  • 1
    How about just apt-get install PACKAGE? This will do exactly what the title says: Check if PACKAGE is installed, if not install it, else do nothing. – dessert Mar 11 '18 at 12:55

1 Answers1

1

When you want to check if some tool you start from the command line is installed the following line would work:

if [ "`which someCommand`" = "" ]

The which command checks for the full file name of the executable of someCommand and if the file is not found the body of the if statement is executed.

However this assumes that someCommand is a command you can start from the command line (terminal etc.).

In your case you probably want to check if the file /usr/share/perl5/getopts.pl exists.

According to this question this can be done using the following check:

if [ ! -f /usr/share/perl5/getopts.pl ]

This statement will check if some file not exist...