0

I was analyzing the script file and I came across the line below

sed -i '/JBOSS_HOME\/bin\/run.sh/i \export TMP_FOLDER=$JBOSS_HOME/server/default/tmp ' /home/jboss/runJBOSSEAP.sh

I am still not able to figure out what does this command do. I know -i means it is an inline operation. But what it does is still unknown to me. Please help me understand this line.

6
sed -i  

-i says to edit file in-place, that is write the new version over the same name

/JBOSS_HOME\/bin\/run.sh/

A pattern, separated by slashes, the slashes contained in the pattern are quoted with backslashes, so this matches any line containing JBOSS_HOME/bin/run.sh. (Actually since it's a regex, the dot matches any character.)

i \export TMP_FOLDER=$JBOSS_HOME/server/default/tmp '

Command to run when the pattern matches, i is for inserting a line (before the current). The line to be added is separated by the backslash, so this adds the string export TMP_FOLDER=$JBOSS_HOME/server/default/tmp.

/home/jboss/runJBOSSEAP.sh

Target file name.

e.g.

$ echo JBOSS_HOME/bin/run.sh > pla 
$ sed -i '/JBOSS_HOME\/bin\/run.sh/i \export TMP_FOLDER=$JBOSS_HOME/server/default/tmp ' pla
$ cat pla
export TMP_FOLDER=$JBOSS_HOME/server/default/tmp 
JBOSS_HOME/bin/run.sh

It's pretty much the same as e.g. the example here

2

ilkkachu's answer is great and extensive, upvote given. Here's just a side note if you are using BSD sed (as in OS X). This:

sed -i '/JBOSS_HOME\/bin\/run.sh/i \export TMP_FOLDER=$JBOSS_HOME/server/default/tmp ' foo 
sed: 1: "foo": invalid command code f

Does not work. In order for it to work with BSD sed you first need to fix the -i option like this: sed -i."". Furthermore, you need to have your insert on a separate line, like this:

sed -i."" '/JBOSS_HOME\/bin\/run.sh/i \
export TMP_FOLDER=$JBOSS_HOME/server/default/tmp ' foo 

But, again, this is just if you are using BSD sed as found under OS X. Does not apply to GNU sed.

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.