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.

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.

  • Ahh, gotcha. Thanks a lot for the explanation on that! – 64Hz Apr 9 '18 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.