4

I'd like to write a bash script that essentially automates this process:

vi filename.sh

(open "filename.sh" using vi)

add the following 2 lines:

export GRADLE_HOME=/opt/gradle/gradle-5.2.1
export PATH=${GRADLE_HOME}/bin:${PATH}

and save those changes

:wq!

Basically I want to add these in jenkins file for automation

3
  • 2
    Where do you want to add those lines? At the beginning, at the end, at a specific position? Edit your question to include this. This is not a job for vi.
    – Panki
    Oct 17, 2019 at 9:57
  • I am creating a new file and adding these lines
    – SAHIL
    Oct 17, 2019 at 10:03
  • 1
    Can't Jenkins transfer files? Why not create that file once and transfer it?
    – Jeff Schaller
    Oct 17, 2019 at 10:07

3 Answers 3

4

vi is by definition a visual editor.

In this case it's probably better to use some other means.

If you only want to append those lines, do something like:

cat >> filename.sh <<'EOF'
export GRADLE_HOME=/opt/gradle/gradle-5.2.1
export PATH=${GRADLE_HOME}/bin:${PATH}
EOF

This will also work if the file doesn't exist yet.

If you want those lines added at the beginning, you can use ed which is a line-oriented editor:

ed filename.sh <<'EOF'
1i
export GRADLE_HOME=/opt/gradle/gradle-5.2.1
export PATH=${GRADLE_HOME}/bin:${PATH}
.
w
q
EOF

This instructs ed to insert lines at line 1; the solitary dot . on the line indicates the end of input, so insertion stops there. Finally the file is written (w) and the edit session quitted (q).

If you insist on using ed even if the file doesn't exist yet (in which case I would use the cat example above), you can use this:

ed filename.sh <<'EOF'
i
export GRADLE_HOME=/opt/gradle/gradle-5.2.1
export PATH=${GRADLE_HOME}/bin:${PATH}
.
w filename.sh
q
EOF

The main difference is that you don't pass a line number with the i insert command, as there are no lines yet; and you pass a filename to the w write command which is the new file.

sed can also be used, but for such tasks I find ed easier to use (and to read what's happening).

8
  • after doing ed getting No such file or directory error: [root@fcired67 ~]# ed /etc/s1.sh <<'EOF' > 1i > export GRADLE_HOME=/opt/gradle/gradle-5.2.1 > export PATH=${GRADLE_HOME}/bin:${PATH} > . > w > q > EOF /etc/s1.sh: No such file or directory
    – SAHIL
    Oct 17, 2019 at 10:11
  • That sounds like the file /etc/s1.sh does not exist. The file needs to exist before ed can edit it.
    – wurtel
    Oct 17, 2019 at 10:44
  • yes, first have to create that file then needs to add the lines so what changes should i add now to make it work?
    – SAHIL
    Oct 17, 2019 at 10:47
  • I've edited my answer to handle the case where the file doesn't exist yet.
    – wurtel
    Oct 17, 2019 at 13:03
  • @wurtel you could have been even better IMHO if you cut your examples a bit down and concentrate on these three steps in OQ; 1)open file 2)operate data 3)write file OQ says automate, and you say visual editor etc. maybe it is a small algorithm where a filetest and a sed line is needed --- more of: it depends: on A, B and C. Then you have solutions X, Y, Z, e.g. Y:
    – user373503
    Oct 17, 2019 at 13:23
1

Automated scripting can be done using ed

like ed < edit.sed filename.pdf

For more, you can visit this tutorial.

2
  • Also, I found a similar question Here on askUbuntu Oct 18, 2019 at 12:47
  • For inserting at a specific line that is fine. sed is a tool for that. But from what we know, appending at the end is enough. Two lines (in a file, I propose) and a file: not so difficult to concatenate with cat, if not for that in-place problem, which can be solved with >> redirection.
    – user373503
    Oct 18, 2019 at 14:00
0

I would also do it the way @wurtel is suggesting, but if for some reason you really want to do it with vi, add the following to your $HOME/.vimrc file:

function AddVars()
    let line1 = "export GRADLE_HOME=/opt/gradle/gradle-5.2.1"
    let line2 = "export PATH=${GRADLE_HOME}/bin:${PATH}"
    call append(line('$'), line1)
    call append(line('$'), line2)
    write
    exit
endf

And then call vi with using the -c option like this: vi filename.sh -c "call AddVars()"

4
  • If you prefer to add the lines at the beginning of the file, change call append(line('$'), line1) with call append(line('1'), line2) and call append(line('$'), line2) with call append(line('$'), line1)
    – LincolnP
    Oct 17, 2019 at 10:43
  • you append at the end; OQ says "add" (and he is in vi). See comment at wurtel. And of course you could aliias that long vi command further.
    – user373503
    Oct 17, 2019 at 13:37
  • Thank you @rastafile, good point
    – LincolnP
    Oct 17, 2019 at 18:22
  • just right now I posted a answer myself!. Thx to cas, see his bark. I like your vim function a lot, but not for this Q. Save it for smth more complex.
    – user373503
    Oct 17, 2019 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .