2

I cannot work this out, seems my command in awk is not good enough:

Basically I'd like to extract information from a simple Linux ls -l:

ls -l /etc/alternatives/java

lrwxrwxrwx. 1 root root 29 Apr  5 12:28 /etc/alternatives/java -> /jre/IBM/JDK6SR10FP1/bin/java

I then use awk to retrieve this part:

 ls /etc/alternatives/java -l | awk -F"->" '{print $2}'
 /jre/IBM/JDK6SR10FP1/bin/java

As I need to remove the trailing /bin/java to make it a valid $JAVA_HOME, how to do that in bash?

All I want is:

/jre/IBM/JDK6SR10FP1

Many thanks in advance

Michael Mao
  • 9,878
  • 23
  • 75
  • 91

2 Answers2

1

Suppose you assigned this value /jre/IBM/JDK6SR10FP1/bin/java to JAVA_HOME using command substitution:

JAVA_HOME=$(ls /etc/alternatives/java -l | awk -F"->" '{print $2}')

Update: as pointed out in the comments, readlink is the right tool to use for this step, not ls.


then you can remove the trailing part using

JAVA_HOME=${JAVA_HOME%/bin/java}

For more information, see, for example the section Manipulating Strings

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
  • Thanks for the quick and working response, I will leave it there for sometime just to see if there are other options – Michael Mao Apr 05 '13 at 02:36
  • just a side note: I need to run sed command to trim off leading and trailing whitespace from awk. But overall a nice and simple solution. Thanks a lot! – Michael Mao Apr 05 '13 at 03:58
  • [Don't parse `ls` output](http://mywiki.wooledge.org/ParsingLs) – tripleee Oct 08 '22 at 14:47
  • @tripleee In general it may be a good advise to be careful parsing `ls` output, but for this particular case, with only one line of output and the specific `->` separator used, I do not see how this could go wrong. – Reinier Torenbeek Oct 10 '22 at 18:05
  • A file named `foo->bar` would be one example. There's of course a limit to how much time you are willing to spend on robustness around corner cases; but the simple solution is to use `readlink` which doesn't have them. – tripleee Oct 10 '22 at 18:19
  • of course -- thanks for pointing this out. Additionally, the technique of removing the trailing part did not exactly work either so I wanted to remove this answer, but I can't because it is the accepted answer. – Reinier Torenbeek Oct 11 '22 at 12:34
  • By the way, the duplicate that you (@tripleee) associated this question with does not address the removal of the trailing part, which seemed to be the aspect that the OP was asking about. – Reinier Torenbeek Oct 11 '22 at 12:42
  • 1
    Oh well, I'll add a second duplicate. – tripleee Oct 11 '22 at 12:49
0

To extract the JAVA_HOME path from the Java path by removing the trailing /bin/java you can use sed (and readlink to resolve symbolic links):

JAVA_HOME=$(readlink -f /etc/alternatives/java | sed 's/\/bin\/java$//')

The regular expression in sed matches trailing /bin/java.

Alternatively (if not using alternatives):

JAVA_HOME=$(readlink -f $(which java) | sed 's/\/bin\/java$//')
user2314737
  • 27,088
  • 20
  • 102
  • 114