Jenkinsfile is a kind of addition to version control systems. It works with
pipelines and textfiles.
This is a nice example to demonstrate the interactive vs. the programmed approach, i.e. the differences.
The process can be split in three parts, as OQ points out:
1) open FILE in a interactive (and thus: visual) editor
2) ADD 2 lines. Line noise lines. Add (or insert) where?
3) Save. Mentioned extra
:wq. But we dont do key macros here.
Now you translate to automated. I keep it simple first, wurtel's HERE document is a good start. But is this a finished "solution"? I propose:
Create a file with these two lines alone. They are constant, and they contain dollar signs. Call it
gradle-lines for now. Good investment anyway:
Now you can
cat gradle-lines >>filename.sh
alias grad-append='cat gradle-lines >>' you can type
...to run an alias with a filename. Any filename.
grad-append filename.sh is short for
cat gradle-lines >> filename.sh
I have nothing else really to show! (So simple)
A solution with
sed -i seems an alternative. A must if you plan to insert these lines at a special place. Besides perl etc., but that is another caliber and really not needed here.
Anyway it's two slightly different elemantary steps: appending
>> and aliasing.
But Jenkinspipe and
>> of a two-line file should be n.p. Need more specs for that.
I tested all this. I mean I tested much more until I realized a file with the two lines and a
>> suffice. Nasty Q!
I started this Q by commenting quite positively on wurtel's answer; here is his main solution I mention above, the HERE document. I have to give credit to him, formally. I swear I took a detour involving
info sed and
tmpf=mktemp and even
cp $(mktemp) but then append to which file? Here wurtel's HERE:
If you only want to append those lines, do something like:
cat >> filename.sh <<'EOF'
This will also work if the file doesn't exist yet.
If you put this in a script or function it would be called
grad-append as in my solution. But this one is a function (luckily) because it needs (sadly) a positional parameter
$1. "filename.sh" is now hard-coded.
And that here document also lacks modularity; function and data are mixed. It could be perfect for certain situations. But my variation is more flexible and direct.
What if he -- only once in while -- wants to
:read gradle-lines into vim interactively? My solution has reusable modules as side effect.