Home > Software engineering >  .Deb package postinst file not appending data to ~/.bashrc
.Deb package postinst file not appending data to ~/.bashrc

Time:01-07

I have this very simple postinst file for a .Deb package:

#!/bin/sh

echo 'alias command_pandora="sudo /usr/local/bin/pandora"' >> ~/.bashrc

echo 'Pandora Storage Server Installation complete.'

When I run it I even get the 'Pandora Storage Server Installation complete.' message, but nothing is appended to ~/.bashrc; nevertheless when I run this command alone in the terminal:

echo 'alias command_pandora="sudo /usr/local/bin/pandora"' >> ~/.bashrc

It does work. I already tried modifying the file permisions for ~/.bashrc but still get the same result. I also tried running a separate script with the same content and running it and it also works, so it seems to be related to dpkg.

Why is the content not being appended?

CodePudding user response:

The postinst script runs as root. Package installation is a system installation utility; it should absolutely not modify users' private files, including those of root.

Tangentially, defining an alias seems like the wrong solution to your problem. Generally, prefer functions or shell scripts over aliases.

If the tool requires privileged access through sudo, perhaps refactor it to run itself with sudo (maybe with a check to only do this when it is connected to a terminal, to prevent it from hanging when run unattended).

Or, simply, include /usr/bin/command_pandora in the package with the following contents:

#!/bin/sh
exec sudo /usr/local/bin/pandora "$@"

(Marginally, I suppose it could add something to /etc/skel/.bashrc but this will only create a new alias for users which are created after this change, or users whose .bashrc presciently run source /etc/skel/.bashrc. I don't think that's a good idea, either.)

  • Related