0

So I'm currently trying to reverse-engineer a batch of scripts passed down from our last SysAdmin.

There's a line in one of the shell scripts that I (for the life of me) am unable to understand. Here it is:-

sed -n 'H;${x;s/^\n//;s/-jar\n/Dcom.sun.management.jmxremote.ssl=false\n&/;p;}' "$File"

GNU Bash 4.2.46(2) RHEL6 64bit.

1 Answer 1

0

The command requires GNU sed and seems to insert the string Dcom.sun.management.jmxremote.ssl=false followed by a newline before the first occurance of -jar at the end of a line.

Example:

something
something
something-jar
something

is transformed into

something
something
somethingDcom.sun.management.jmxremote.ssl=false
-jar
something

The sed command reads the whole file into memory by running H for each line of the file (appends the current line into the "hold space" with a newline character at the start). When it reaches the last line ($), it swaps the hold space with the pattern space (x) and deletes the first newline (put there by running H for the first line of the file). It then substitutes -jar\n with Dcom.sun.management.jmxremote.ssl=false\n-jar\n (the & is whatever was matched by the expression). It then prints the collected lines (p).

Another way of doing the same thing with awk:

awk 'skip==0 && sub("-jar$", "Dcom.sun.management.jmxremote.ssl=false\n-jar") { skip=1 } { print }' "$File"

This tries to do the same substitution on each line, and when it succeeds it sets skip=1 which will cause it to not try again. Every line is printed regardless of whether a substitution occurred or not.

This does not store the whole file in memory.

1
  • Ahh, gotcha. Thanks a lot for the explanation on that! Commented Apr 9, 2018 at 9:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.