8

I would like to create a script that will add a new PPA or skip if already installed.

Someone posted this solution to a different question:

#!/bin/bash 

add_ppa() {
      for i in "$@"; do
        grep -h "^deb.*$i" /etc/apt/sources.list.d/* > /dev/null 2>&1
        if [ $? -ne 0 ]
        then
          echo "Adding ppa:$i"
          sudo add-apt-repository -y ppa:$i
        else
          echo "ppa:$i already exists"
        fi
      done
    }

shell script to conditionally add apt repository

In my case I would like to add ppa:otto-kesselgulasch/gimp-edge, but I am not sure where I am supposed to add this in the script.

I tried add_ppa(ppa:otto-kesselgulasch/gimp-edge) but I keep getting an error saying (ppa is the name of my script):

ppa: 1: ppa: Syntax error: word unexpected (expecting ")")

I also tried with a space in between ppa and without the ()

ppa: 1: ppa: add_ppa: not found
ppa: 12: ppa: Syntax error: "}" unexpected

Can anyone tell me what am I doing wrong please?

  • 2
    It would be useful to edit your question to include your script, making it easier to diagnose where the problems lie. – Arronical Mar 28 '18 at 8:59
9

That is a bash function, and to pass arguments to bash functions, you don't need (). Also, the function adds the ppa: prefix to its arguments, so just this would do:

add_ppa otto-kesselgulasch/gimp-edge

Additionally, the function supports multiple arguments, so you can run it for multiple PPAs:

add_ppa otto-kesselgulasch/gimp-edge foo/bar a/b

Given that you have put the function in a script, you should either:

  • source it as Zanna suggests to get the function in your shell and call the function, or
  • just use the contents of the function as the script:

    #! /bin/bash
    for i in "$@"; do
      if grep -Rq "^deb.*$i" /etc/apt/sources.list.d/*.list
      then
        echo "Adding ppa:$i"
        sudo add-apt-repository -y ppa:$i
      else
        echo "ppa:$i already exists"
      fi
    done
    

    And the call the script with the ppa name:

    ppa otto-kesselgulasch/gimp-edge
    
  • 3
    Kind of a side note, you can drop the in "$@"; for i; do loops over the positional parameters. – Kevin Mar 28 '18 at 21:37
6

I think you are executing the file as a script, but it's only a function definition, so the function is never called when it's run.

You don't need to add anything to it; you don't need to add the PPA anywhere inside it. You can source the file and then call the function in the current shell:

. ppa
add_ppa otto-kesselgulasch/gimp-edge

(assuming the file you saved it in is called ppa, is in the current working directory, and has exactly the same content as the example you posted)

You could also run it as a script, if you add a line that actually calls the function. You could hard-code the PPA into it by adding a line like this at the end:

add_ppa otto-kesselgulasch/gimp-edge

and then run the script. But that way, you'd have to edit the file every time you wanted to add a new PPA...

Instead you can call the function on all the arguments passed to the script by adding a line like this:

add_ppa "$@"

then when the script is called:

./ppa otto-kesselgulasch/gimp-edge thing/stuff etc/andSoOn
3

It looks like you may have made a typo in your script, specifically at the definition of the add_ppa function.

The error message:

ppa: 1: ppa: add_ppa: not found
ppa: 12: ppa: Syntax error: "}" unexpected

Indicates that add_ppa is not being recognised as a function.

It appears that you may have used ( in place of { when defining the function. Correct this mistake and the function should work correctly, though it should be used as indicated in muru's answer.

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.