I am a beginner in bash scripting.I want to create a bash script to install install and configure stuffs on my linux box in an automated way.
Suppose i want to edit
/etc/yum.repos.d/epel.repo
by the bash script
What i want to do is as follows
- Open file /etc/yum.repos.d/epel.repo
- Find [epel] section
- Add a line priority=10 just after line enabled=1 in the epel section
For the first part i added
yum install nano
nano /etc/yum.repos.d/epel.repo
My Question is how to do the 2 and 3rd part with bash script using nano (if possible , in case not possible then show me with sed)
Also at some points i will have to modify variables in files For e.g
- enable = 0 to enable = 1
- Testing = "1" to Testing = "0"
- Add text in a line . functions = to function = "text1","text2"
- Add some text to a file and save it(with nano)
I know its demanding but i need to create this for a friend who is noob at server management.Unfortunately i don't have time to study bash scripting from start as my exams as near.I will use your examples to write the script.
Guys thanks for all the replies I have successfully done most of the bash script
However i have another problem
Lets suppose i want to modify nginx.conf using sed
We will deal with worker_processes
Now i want use sed to do the following
1.Find the FIRST occurrence of worker_processes
in that conf and replace text with worker_processes 4;
Special Note Here: This is just an example. It may happen that the conf contain
worker_processes 1; . This is hard part . I want a sed command that find the FIRST wHOLE match case of the word worker_processes , delete line of text where the word is found and and paste worker_processes 4;
there and then save file.
This is the most reliable method i though of when editing files (without nay risk of breaking any conf
One last suggestion I used sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/remi.repo to change enabled =0 to enabled=1 under the [remi] section in remi.repo .However i have a feeling that it may modify all enabled=0 in that file , which will wreak the server.Can anyone suggest a better code.
Another stuff i am not sure of:P I want to edit a file that has this as Text Testing = "0"(Yes it has quotes and i need to keep it) It should be modified from Testing = "0" to Testing = "1"(with quotes)
Also i need to add some text with quotes at the end of a file with sed Like "Thanks Quanta"(with quote) For php you put a \ with echoing quotes , don't know how it is done for bash
Another thing
I need to modify a line in a conf but i don't remember what is the whole of text to replaced
Like its listen = something; , i want to modify it to listen = /tmp/php5-fpm.sock;
Big thumbs up for up quanta
Thanks for the awesome support guys