Edit the file in shell script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory and add some more lines to the file /etc/sysctl.conf. How this can be done?

  1. Relevant commands, code, scripts, algorithms:

Need to edit /etc/sysctl.conf while running script.

  1. The attempts at a solution (include all code and scripts):
    I have tried it using vi and i am not able to do this.

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Houston , Texas, USA, Dr. Deniz Gurkan,ELET6300

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

The vi utility is an interactive editor, not a scripting editor. Look at the man pages for ex , ed , and sed for utilities that are intended to be used to edit files in scripts.

Another thing to consider in your script (maybe some extra credit :wink: ): Does it create a way to undo the change you are making to sysctl.conf? Consider taking a backup first...

Assuming that I have read the question correctly.

One solution would be to create a file called say /etc/sysctl.conf.append with "vi" or editor of the day, containing ONLY the new lines.

Then append the new lines to the old file using good old "cat".

cat /etc/sysctl.conf.append >> /etc/sysctl.conf

In the commercial world we would save the old version of the file (and it's permissions) before changing it. For example:

cp -p /etc/sysctl.conf /etc/sysctl.conf.180414

---------- Post updated at 02:40 ---------- Previous update was at 02:35 ----------

If we go one step further it is possible to automate the entire process by creating the "append" file in the shell script.

echo "line 1" > /etc/sysctl.conf.append
echo "line 2" >> /etc/sysctl.conf.append
echo "line 3" >> /etc/sysctl.conf.append

As we know nothing about your computer or its Operating System or Shell, this is a generic answer. In some shells the "echo" needs replacing.