Pac - a simple pacman helper

Hello,
I always have hard time writing pacman commands like

    sudo pacman -Syu
    pacman -Q firefox

so I wrote a small bash script to help. It provides a command

   pac [pacman options and parameters]

and it means almost the same as pacman. Same parameter and options apply.
The above commands can now be written as:

    pac -Syu
    pac -Q firefox

Note: sudo is not needed, pac will ask for the sudo password if needed.

The script can be added as-is to ~/.bashrc.

Note that if you always want to use yay, then this script does not help you much. But I always use pacman…

And here’s the script:

# Wrapper for pacman. Knows when to have root permissions.
# Usage: pacmanwrapper [pacman-parameters]

Main()
{
    local rooterrorstring="error: you cannot perform this operation unless you are root."
    local log=$(mktemp)
    local pacman=/usr/bin/pacman
    local retpacman

    LANG=C $pacman "$@" >& $log
    retpacman=$?

    case "$retpacman" in
        0) cat $log ;;
        1)
            if [ "$(cat $log)" = "$rooterrorstring" ] ; then
                sudo $pacman "$@"
            else
                cat $log
            fi
            ;;
        *) cat $log ; echo "Warning: pacman returned error code $retpacman." >&2 ;;
    esac
    rm -f $log
}

Main "$@"

EDIT at 2019-Dec-26: fixed a bug (of sometimes showing unnecessary logs).

7 Likes

First of all wonderful job!!!

I did some test runs with it and it worked flawlessly but every fibre in my being wants to write sudo pacman, so I think a lot of people will struggle with that or even say it is unnecessary, but I think you thought this very through.

It certainly makes it easier for people who’ve always used GUI tools.

Thanks for your comments!

I know many people, including me, are very used to using sudo. Still it bothers me (probably because I’m so lazy) so I figured that I can be made easier.

Some people use aliases pacman=‘sudo pacman’ but that in my mind only moves the real problem into another place.

But to be honest, I wouldn’t recommend using this pac on mission critical systems simply because it is not yet extensively tested. But I believe it is a handy little script for most if not all common use cases.

And it essentially makes usage of pacman as simple as using yay. :wink:

That’s my thought too, I see it as a bridge for those Pamac users who want to dive in the terminal.

It could be a candidate for a package (or an app within another package), but before doing anything like that, it should be tested much more and in many different situations and use cases.
So that’s why I simply posted it here so that interested users (like you :wink: ) have the opportunity to test it.

1 Like

I agree it has potential.

1 Like

I’m even more lazier than typing pac -xxx :slight_smile:
i’m using

alias ins="sudo pacman -S"
alias insa="trizen -S" #install aur packages
alias rem="sudo pacman -R"
alias remd="sudo pacman -Rcns"   #removes dependencies also

That is also sensible. And with pac you could write them like

alias ins="pac -S"
alias insa="yay -S"
alias rem="pac -R"
alias remd="pac -Rcns"   #removes dependencies also

Especially removing packages sometimes needs different options to do it safely, and static aliases are not very flexible.
Anyway, pacman in its current form is basically an excellent tool, but not the easiest to learn and use. That’s why there are so many alternatives/wrappers to it in Arch repos and AUR.

1 Like

Well using the aliases I have given above i could for example use rem -cns and it would have the same effect as using remd. ins -yu would be equivalent to sudo pacman -Syu in the above example. (of course I have an alias for updating the system also, so I wouldn’t have to type so much :stuck_out_tongue:

I’d say there is more flexibility with aliases than one would expect if you experiment around a bit.

regarding the rewrite to alias ins="pac -S" that would not necessary be helpful. Since it’s an alias anyway, I don’t really care how long the command is. I’d rather be sure I’m not introducing too many potential points of failure by adding an additional wrapper into the mix. The initial goal of only having to type the short alias is met already even without the rewrite.

1 Like

Agreed, well selected aliases like yours are really versatile and handy.

For me remembering many alias names is harder than remembering options of a command.
But people learn things differently and that’s OK. :smile:

The great thing is that we all can have a way that is best for each of us!

One thing I learned in programming (but can be extended to tinkering with linux-based OS) is that many things can be done in a multitude of ways. and there is not always one better way to do it. it’s just a matter of choice.

Btw the wrapper you made is pretty slick.

1 Like

That would be me. As is known, my Unix/Linux background goes back to the early 70’s. I was also an administrator for 18 months on a critical system. Worst 18 months of my career. So being quite old school, I absolutely hate sudo on many levels, the biggest being security. Sudo is not a real big deal on a home computer, but I believe it would be rarely used in an enterprise situation. The first thing I do after installing a new OS is disable sudo. The second thing I do is make an alias ll=‘ls -l --color=auto’ as the ll alias always came by default in every Unix system i ever used. Some Linux distros include ll others dont.

Pudge

This is exactly why I wouldn’t use my own special solutions such as aliases. That’s not how you learn the original.

I myself use only one age-old alias (from history, command.com and cmd.exe know it)…

alias cd..='cd ..'

…which, once you get used to it, is missing in other systems and then just annoying (therefore I use, if possible, only keyboards and mice with standard assignments without special keys - I’m at a lot of different computers every day, directly and remotely).

Btw., “remd”, I wouldn’t call it that. “d” is reserved for daemons. In addition rem reminds of commented out. If so, then “purge”, for example.

BTW, the same as alias .. can be done with a more general bash setting (currently cannot find the setting… :wink: ). With that settings, when you write just a directory name on a command line, bash automatically interprets it as cd "dir".

Edit: because of my terminal colors, I slighly misinterpreted your alias. Didn’t notice it was alias cd.., read it as alias ..
Sorry, my bad.

Edit2: the bash setting I referred to is autocd in shopt command.