tags:

views:

86

answers:

2
perl -E '$i=@{[`zypper lr`]}-2;map{`zypper rr $_`}1..$i'

What would be a good way to write this perl-onliner in bash. ( I would like to remove all repositores with zypper)?

+1  A: 

Here's a way to do this:

The first command counts the number of lines produced by zypper lr command.

So, you obtain that by:

COUNT_LINES=`zypper lr|tail +3|wc -l`

The second command merely runs zypper rr [NUMBER] for each number 1 through the counter; so you run the for loop in bash as shown in this SO question:

How do I iterate over a range of numbers in bash?

DVK
That fails when there are holes in the numbering.
daxim
@daxim - this does 100% what the Perl one did. He asked how to convert Perl code to bash, NOT how to do XXX in bash correctly.
DVK
@DVK so we shouldn't even *tell* people when their code is going to blow up on them, so long as it's exactly what they asked for?
hobbs
@hobbs - I didn't say we shouldn't. Just that I chose that in this instance (and if you peruse my many other answers, you will see that I do mean *in this instance*) I won't. Nobody's stopping someone (let's theoretically call them "daxim") to choose to answer differently, and and other people to choose to upvote what they consider the best asnwer. Isn't that what SO is supposed to work like?
DVK
A: 
zypper lr | grep -P "^\d" | cut -d'|' -f 1 | xargs sudo zypper rr

But much easier to simply:

sudo rm -rf /etc/zypp/repos.d/*
daxim
My grep doesn't understand "^\d" so I have to use "^[0-9]".
sid_com
Right, I didn't remember I had aliased `grep` to `grep -P`. When you're running zypper, you have openSUSE and I wager you do have a grep that understands Perl regexp - it just needs to be enabled specifically.
daxim