2

I have a question about the following command:

apt-cache search java | awk '{print($1)}' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e 'java-common'

Owing to my limited IT knowledge, am I right to say that the (ia32-) in the above command refers to 32-bit software? If it is, then I assume that the above command is for 32-bit installed OS.

However I installed a 64-bit OS.

What is the correct parameter for 64-bit software? Is it (amd64-) or (ia-64) or (x86_64-)?

Any expert help is much appreciated.

n00b
  • 1,897

1 Answers1

6

First of all, apt-cache search does not search for installed packages, it looks for available ones, both installed and not installed. Also, the various search patterns you see are not mutually exclusive, so lines matching any of them will be printed. Finally, the ? after ^(ia32-) means ia32- is optional, the expression will match lines that have it and lines that don't. So, your command will indeed work for 64 and 32 bit systems.

The equivalent command to look for installed packages only would be:

dpkg -l *java* | awk '{print($2)}' | 
    grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' \
      -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e 'java-common'

But don't use that. In Debian derived systems such as Ubuntu, there is a special tool for this, update-java-alternatives which, when run with the -l option will list installed Java environments:

 update-java-alternatives -l

On my system, for example, this prints:

java-1.6.0-openjdk-amd64 1061 /usr/lib/jvm/java-1.6.0-openjdk-amd64
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
jdk-7-oracle-x64 317 /usr/lib/jvm/jdk-7-oracle-x64
terdon
  • 100,812
  • Thanks for such your answer and detailed explanation. Is there a way for me to award you more points? – n00b Mar 24 '14 at 15:57
  • What programming language was the command written in? C? C++? Basic? C#? The whole command resembles a string of art with ^ | and * – n00b Mar 24 '14 at 16:00
  • 1
    The language is basic shell scripting and regular expressions. The ^ is a regular expression trick, it means match the beginning of a line, the | are pipes which pass the output of one command as input to another. * is a wildcard, it means 0 or more. – terdon Mar 24 '14 at 16:03
  • Thanks for your answer. Basic shell scripting is based on a programming language, no? – n00b Mar 24 '14 at 16:04
  • 1
    No, it is a simple programming language but all the strange stuff you see here are regular expressions, the shell part is what pass the input of one command to another (using |). For a very nice tutorial on regular expressions, see here. – terdon Mar 24 '14 at 16:06
  • If you have time, could you help provide an answer to me please? The link to the webpage is http://askubuntu.com/questions/84483/how-to-completely-uninstall-java and my question starts with the words The command sudo apt-get -y autoremove removes many other packages that I need. Is it OK to just ignore this command? Or could you provide a "gentler" command that does not remove packages other than those have Java in them? – n00b Mar 24 '14 at 16:11
  • @n00b please post your own question and I or someone else will be happy to answer it. Do not ask new questions in comments. – terdon Mar 24 '14 at 16:13
  • Could you recast your answer dpkg -l *java* | awk '{print($2)}' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' \ -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e 'java-common' in code form so that there is only one string? (See how I did it in my question.) I find it easier to copy and past if the command were in code form. Thanks in advance. – n00b Mar 24 '14 at 16:16
  • @n00b it is in "code form", notice the \ at the end of the line, that enables you to paste multi line commands. You can simply copy/paste it as is directly into your terminal. – terdon Mar 24 '14 at 20:03