[SOLVED] Question About VIM in scripts

deathshound7007

Reputable
Sep 4, 2018
25
1
4,535
Hey all, I have been tasked with specifically making vim commands work in a shell script for an assessment and I cant for the life of me find stuff to help figure out how to do it.
I have searched around online and only ever found things that gave answers like "just try this instead its easier", though if I used something else I wouldn't get marked on it. I have also looked through the books we were given and didn't really find anything on this specifically either.

Here's some more context. Question : "Create and test a Linux script called 'setup.sh' that contains all the previous commands and save it in your Assessment directory."
The tasks before that were, in this order:
1 - Create a directory in the home directory called 'Assessment'
2 - Create 2 directories called 'Source' and 'Destination' in the 'Assessment' directory
3 - Create 2 files named 'File1.txt' and 'File2.txt' inside 'Assessment/Source'
4 - Write your Name and DOB in 'File1.txt' using a Linux editor VIM
5 - Copy only the txt files from 'Source' to 'Destination'
6 - Apply the following permissions to the 'Assessment' directory (User: Read Write Execute, Group: Read, Other: Read)
7 - Pipe a directory listing using long listing format of 'Assessment' directory contents and save as 'listing.txt' in 'Assessment'

Now I know how to do those previous 7 tasks but I am struggling at the VIM part within a shell script as it does not like "vi file1.txt". "command not found". The closest things I found online were things saying that adding "-c" would allow it to do commands for VIM.. But then followed up by how to reverse the lines in the txt file.
From that I tried " vi File1.txt -c 'i' echo "name dob" -c ':wq' " but that didn't work for me. (I am very new to Linux, aka never used outside of this assessment)

Let me be clear though; I am not looking for an outright answer to this, just for some help/point me in the right direction so I can learn how to do it.
Any help is highly appreciated.
 
Solution
pretty sure echo works for this. simply echo "text here" >> file1.txt

as many lines as needed. this avoids vi, nano and the others though so may not be an option.

i'd be looking at how to script inside vi or nano. then once you can script inside, you should be able to shift that to the main script. unless you can have the second script and just run it from the main script. a quic google search turned up many pages on how to script inside vi and so on.

deathshound7007

Reputable
Sep 4, 2018
25
1
4,535
Does it HAVE to be VIM? SED is the "old school" method for command line based text editing in a script.
I have the choice of either VIM, Pico or Nano for the assessment for number 4. But I have to do everything in the script exactly how it is in the previous steps (1 to 7)
So basically if I used vim on number 4, I have to use vim in the script. If i used Pico then Pico in the script. Nano then Nano in the script too.
 

Math Geek

Titan
Ambassador
pretty sure echo works for this. simply echo "text here" >> file1.txt

as many lines as needed. this avoids vi, nano and the others though so may not be an option.

i'd be looking at how to script inside vi or nano. then once you can script inside, you should be able to shift that to the main script. unless you can have the second script and just run it from the main script. a quic google search turned up many pages on how to script inside vi and so on.
 
Solution

deathshound7007

Reputable
Sep 4, 2018
25
1
4,535
pretty sure echo works for this. simply echo "text here" >> file1.txt

as many lines as needed. this avoids vi, nano and the others though so may not be an option.

i'd be looking at how to script inside vi or nano. then once you can script inside, you should be able to shift that to the main script. unless you can have the second script and just run it from the main script. a quic google search turned up many pages on how to script inside vi and so on.
Thanks for this. I will definitely have a look at how to script inside of vi
 

Math Geek

Titan
Ambassador
no problem i'm learning linux myself right now and have had to come up with some creative searches myself to get things done. though so far i've not had to script anything with vi.

done many other scripts but not vi yet lol. i've been working in digital ocean clouds and have had fun scripting the full set-up i want from a freshly spun up vm. can get desktop, containers and multiple containers all spun up with one script so far. been a real learning experience mixing all kinds of commands inside the script. though i did use the get-docker script already available as part of the script then run it after it's downloaded.

fun fun fun if you're a big geek like me :)
 
Hey all, I have been tasked with specifically making vim commands work in a shell script for an assessment and I cant for the life of me find stuff to help figure out how to do it.
I have searched around online and only ever found things that gave answers like "just try this instead its easier", though if I used something else I wouldn't get marked on it. I have also looked through the books we were given and didn't really find anything on this specifically either.

Here's some more context. Question : "Create and test a Linux script called 'setup.sh' that contains all the previous commands and save it in your Assessment directory."
The tasks before that were, in this order:
1 - Create a directory in the home directory called 'Assessment'
2 - Create 2 directories called 'Source' and 'Destination' in the 'Assessment' directory
3 - Create 2 files named 'File1.txt' and 'File2.txt' inside 'Assessment/Source'
4 - Write your Name and DOB in 'File1.txt' using a Linux editor VIM
5 - Copy only the txt files from 'Source' to 'Destination'
6 - Apply the following permissions to the 'Assessment' directory (User: Read Write Execute, Group: Read, Other: Read)
7 - Pipe a directory listing using long listing format of 'Assessment' directory contents and save as 'listing.txt' in 'Assessment'

Now I know how to do those previous 7 tasks but I am struggling at the VIM part within a shell script as it does not like "vi file1.txt". "command not found". The closest things I found online were things saying that adding "-c" would allow it to do commands for VIM.. But then followed up by how to reverse the lines in the txt file.
From that I tried " vi File1.txt -c 'i' echo "name dob" -c ':wq' " but that didn't work for me. (I am very new to Linux, aka never used outside of this assessment)

Let me be clear though; I am not looking for an outright answer to this, just for some help/point me in the right direction so I can learn how to do it.
Any help is highly appreciated.

Vi and Vim (Vi Improved) are separate programs.

Some distributions ship with neither installed.

Some distributions ship with Vi installed and support Vim via repository.

Some distributions ship with Vi installed and installing Vim from that distribution's repository symlinks vi to vim, effectively replacing Vi with Vim when Vim is installed.

Some distributions ship with only Vim and symlink vi to vim out of the box.
 

Math Geek

Titan
Ambassador
i was working through a lab for snort yesterday and a part of it had us doing what you are asking about.

i don't know if you figured it out yet or not but we used a command called sed to add, replace and otherwise edit txt and conf files from the command line. here's a page about it


don't know if this is allowed for you, but it certainly accomplished the task easy enough. command structure is a bit weird but i'm sure you can figure that part out :)