Better way to add alternative using update-alternatives?
To answer your first question I'd like to hint you to --query
:
--query name
Display information about the link group like --display does, but in a machine parseable way (see section QUERY FORMAT below).
Armed with this you will get the link source, e.g. in my case for java:
usr@srv % update-alternatives --query java
Name: java
Link: /usr/bin/java
[...]
So, you see my chain goes like /usr/bin/java -> /etc/alternatives/java -> ...
.
Unfortunately, I don't have an answer to your second question. I've been searching for a solution for this some time ago, but it seems that there is no simple workaround, yet. What you could do is writing some hack to parse the output of the --query
call, maybe like this:
update-alternatives --query java | /bin/grep Link | cut -f 2 -d " "
which you could then use as input for the --install
call. But since this is quite messy in my opinion I won't recommend it. Instead you might want to have a look at galternatives
, a front-end to the alternatives system. Even if I don't like to use graphical stuff for such basic jobs it is quite convenient and I ended up using this tool instead of the command line tools.
EDIT
I've been curious how update-alternatives
knows what's the command symlink and took a short look into the sources. Of course the alternatives system has to store the config for each group and it turns out that it's called administrative directory and written on the man page ;-)
You'll find this information in /var/lib/dpkg/alternatives
(by default). The second line in each of these files defines the master link you're looking for. You may extract it like this:
usr@srv $ sed -ne 2p /var/lib/dpkg/alternatives/java
/usr/bin/java
However, this is just a workaround for those having an older version of update-alternatives
.
Simpleton question here: Isn't the <link>
always going to be the same one retrieved by a simple which [program]
since that is the link that the system uses to call a program by command-line?
For instance, tracing back the links for pycharm I get:
user@computer:~$ which pycharm
/usr/bin/pycharm
user@computer:~$ ll /usr/bin/pycharm
lrwxrwxrwx 1 root root 25 Mar 12 2015 /usr/bin/pycharm -> /etc/alternatives/pycharm*
user@computer:~$ ll /etc/alternatives/pycharm
lrwxrwxrwx 1 root root 43 Mar 12 2015 /etc/alternatives/pycharm -> /opt/pycharm-community-4.0.5/bin/pycharm.sh*
And I can then use:
sudo update-alternatives --install /usr/bin/pycharm pycharm /opt/pycharm-community-4.5.0/bin/pycharm.sh 0
For my new pycharm install.
Then maybe you could construct a script to use the output of which
.