How to use zypper in bash scripts for someone coming from apt-get?

33,695

Solution 1

In general, you should use --non-interactive mode, in shortcut -n, when running zypper non-interactively:

zypper -n install curl

That might seem confusing for someone coming from apt-get install -y curl. Some zypper sub-commands also support a command-specific -y/--no-confirm option as an alias for -n/--non-interactive, but not all sub-commands do. As the install command does implement that, this command is equivalent to the above:

zypper install -y curl

Note that the -y must come after install, while the global -n option comes before the subcommand (zypper install -n means something different; read the man page for that).

[Edit] The section below is no longer accurate, but is retained for historical reference. Current zypper supports the --gpg-auto-import-keys option to automatically import and trust the gpg keys associated with a new repository.


According to documentation there's no way how to accept a GPG key without interactive mode:

a new key can be trusted or imported in the interactive mode only

Even with --no-gpgp-checks the GPG key will be rejected.

A workaround for scripts is to use pipe and echo:

zypper addrepo http://repo.example.org my_name | echo 'a'

Solution 2

You have the --non-interactive option. From the man page:

Switches  to  non-interactive  mode. 
In this mode zypper doesn't ask user to type answers to various prompts, but uses default answers automatically. 
The  behaviour of this option is somewhat different than that of options like '--yes', since zypper can answer different answers to different questions. 
The answers  also  depend on other options like '--no-gpg-checks'.

There is no real correspondense to apt-get's autoremove. The closest is the --clean-deps option of the remove command, which cleans dependencies right away (but not afterwards).

Solution 3

That is a sample

zypper --non-interactive --quiet addrepo --refresh -p 90 http://packman.inode.at/suse/openSUSE_Leap_15.0/ 'packman'
zypper --gpg-auto-import-keys refresh
zypper --non-interactive dist-upgrade --allow-vendor-change --from packman
zypper --non-interactive install vlc vlc-codecs

Of course you can include more options like --auto-agree-with-licenses but remember that makes difference if its before or after install

Solution 4

This worked for me (checked on SLES12SP3):

zypper --non-interactive --quiet ar -C http://myrepo myrepo
zypper --gpg-auto-import-keys ref

Note -C/--no-check for zypper ar.

Now you can install packages:

zypper in -y --auto-agree-with-licenses vim
Share:
33,695

Related videos on Youtube

Jarek
Author by

Jarek

You may be interested in the story of SE moderator Monica Cellio and how she was unfairly treated by the corporate management of this site. More info here. An update is available. Let's hope we can cultivate a more fair environment for content creators and moderators going forward.

Updated on September 18, 2022

Comments

  • Jarek
    Jarek almost 2 years

    I have a few questions about moving from apt-get to zypper in bash scripts.

    What is the equivalent of this?

    sudo apt-get install curl --assume-yes
    

    (where curl could be any package)

    I found the Zypper Cheat Sheet - openSUSE. Very nice! But I would appreciate the voice of experience here -- what's the right way to use zypper in a script where I want to auto agree to all prompts and not skip things that need a response?

    With my inexperience I would be tempted to use:

    sudo zypper --non-interactive --no-gpg-checks --quiet install --auto-agree-with-licenses curl
    

    But is that really the equivalent of --assume-yes?

    What about the equivalent for these?

    sudo apt-get autoremove -y
    sudo apt-get autoclean -y
    

    This suggests there isn't one...

    Is there a replacement for gdebi-core? Or is gdebi not ever needed with zypper's "powerful satisfiability solver"? I use gdebi for situations where I need to install a package on an older version and I have a .deb file already (but not all the dependencies).

  • Admin
    Admin over 6 years
    is it possible to add such an option in zypper.conf for permanent confirmation as in other package managers?
  • Admin
    Admin over 6 years
    it seems its impossible for zypper unlike pacman, apt, and yum/dnf :-(
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 5 years
    Seriously?  There’s a -n option and a -y option and they mean the same thing but they must be placed at different positions in the command line?  Yes, that’s confusing.  (To be clear: I am not shooting the messenger; I’m saying that the developer should have thought more about usability issues.)
  • dannysauer
    dannysauer almost 5 years
    @G-Man - well, sort of. :) zypper has a -n option. The install sub command has both a -y option which is a convenience that aliases to the global -n option, and also has its own -n which is short for --name. It's possible to install packages by capability. So, zypper install -n blah specifies to only resolve the package named blah and not consider packages which provide blah. Without that, zypper can resolve based on the provides section from an RPM (including version comparisons), which can make dependency resolution easier. zypper install 'python>3.5' for example.
  • dannysauer
    dannysauer almost 5 years
    The main use for zypper install -n is if maybe you want to install a specific package whose name is a capability provided by others. For example, maybe there are a few vi implementations, and you only want a specific package named vi without resolution falling back to gvim (which provides vi) in the event that the specific vi package is for some reason missing in your repo. Personally, I dislike the confusing nature of the short options and rarely use them in scripts or anything else someone (like me) might need to read later. :D
  • keithpjolley
    keithpjolley over 2 years
    i'm really confused by zypper addrepo url alias | echo 'a'. specifically, piping zypper output into echo. i tried echo a | zypper ... without any luck.