1

I have scripts like this

#!/bin/sh
SHELL_CMD=busybox

and try to substitute some pattern to bash shell using makefile,

#!/bin/bash
#SHELL_CMD=busybox

Followings are the procedures in Makefile

release:
    @rm -rf my_temp/
    @mkdir my_temp/
    @cp dir1/burn_*.sh dir1/dump_*.sh my_temp/
    @cd my_temp/; \
        for f in $(shell ls); do \
            sed 's:#!/bin/sh\nSHELL_CMD=busybox:#!/bin/bash\n#SHELL_CMD=busybox:1' $${f} > temp; mv temp $${f}; \
        done; \
        cd ..;
    @cd my_temp/; tar -jcv -f bash.tar.bz2 *.sh; cd ..;

My questions are:

1 in the for loop, it didn't get the correct script names in for loop. How to patch it ?

  1. Any better patterns in this sed substitution?
orionlin
  • 167
  • 1
  • 5

1 Answers1

0

You are much better off doing the substitution without trying to match the newline in the source string. Unless you are ready to do some complex sed-fu (How can I replace a newline (\n) using sed?) you can just apply the substitution on each of the line with a

You can do this to apply the action on both 1st and 2nd lines. Also the $(shell ls) part is not needed. You can just run a shell glob expression to get you the files ending with .sh

@for f in *.sh; do \
    sed -i -e '1 s:#!/bin/sh:#!/bin/bash:1' -e '2 s:^:#:1' $${f} ;\
done

If you don't want the -i in-place substitution, use the tmp file approach as you had originally shown.

Inian
  • 80,270
  • 14
  • 142
  • 161