0

Having a "library" with constants and functions (lib.sh), I can include it in my script (script.sh)

. /myfolder/lib.sh
......

Is there a way to merge the included file in the script (i.e. replace ./myfolder/lib.sh with the content of lib.sh)?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

1

Use sed:

sed -i.bak '\|^\. /myfolder/lib\.sh| {
    r /myfolder/lib.sh
    d
}' script.sh

What this script does is:

  • \|^\. /myfolder/lib\.sh| { ... } => locate lines that begin with . /myfolder/lib.sh and execute the commands inside the braces
  • r /myfolder/lib.sh => output the content of /myfolder/lib.sh
  • d => delete the line (. /myfolder/lib.sh)

The other lines are left as is of course.

xhienne
  • 17,793
  • 2
  • 53
  • 69
0

untested: use GNU awk

gawk -i inplace '
    $1 == "." {system("cat " $2); next}
    {print}
' script.sh
glenn jackman
  • 85,964