12

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

  1. Open file /etc/yum.repos.d/epel.repo
  2. Find [epel] section
  3. 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

  1. enable = 0 to enable = 1
  2. Testing = "1" to Testing = "0"
  3. Add text in a line . functions = to function = "text1","text2"
  4. 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

Werulz
  • 305
  • 1
  • 4
  • 16

3 Answers3

18
sed '/enabled=1/a\priority=10' /etc/yum.repos.d/epel.repo

Specify an in-place editing option (-i) if you want (makes the backup with .bak extension first):

sed -i.bak '/enabled=1/a\priority=10' /etc/yum.repos.d/epel.repo

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;

sed '0,/worker_processes [0-9]*;/s//worker_processes 4;/' /etc/nginx/nginx.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

Here for you:

sed '/\[remi\]/,/enabled=0/ { s/enabled=0/enabled=1/ }' remi.repo

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)

sed 's/Testing = "0"/Testing = "1"/g'

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

sed '$a"Werulz, you are welcome"\'

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;

sed 's/listen = .*/listen = \/tmp\/php5-fpm.sock;/'
quanta
  • 50,327
  • 19
  • 152
  • 213
  • 'sed '/enabled=1/a\priority=10' /etc/yum.repos.d/epel.repo' will edit the file and save the changes.PLease confirm – Werulz Aug 21 '12 at 08:30
  • @Werulz: Just check with a file that doesn't matter or copy the contents of epel.repo to a file in the home directory and try running it there without root/superuser privileges. – Erik I Aug 21 '12 at 08:35
  • `-i` is not a [standard option](http://pubs.opengroup.org/onlinepubs/7908799/xcu/sed.html) for `sed`. [`ed`](http://pubs.opengroup.org/onlinepubs/7908799/xcu/ed.html), however, offers a standards-compliant way to edit files with syntax very similar to sed's. – kojiro Aug 21 '12 at 12:53
  • I updated my first post to ask you another suggestion.Thanks so much for support so far quanta. – Werulz Aug 22 '12 at 05:13
  • I updated post again......sorry for asking so many questions . Will share script with you in case u need – Werulz Aug 22 '12 at 10:10
  • http://www.scribd.com/doc/60807668/SED-and-AWK-101-Hacks – quanta Aug 22 '12 at 10:34
  • To substitute just the first occurrence on MacOS: sed -i.bak 's/enabled: false/enabled: true/' sample.txt – Ashwin Jayaprakash Nov 13 '14 at 21:41
12

Like ptman said, Augeas can save you from using sed/awk:

$ augtool -s set /files/etc/yum.repos.d/epel.repo/epel/priority 10

will set the priority of the epel repository to 10. If the priority key already exists, it will set its value, otherwise it will add a priority entry after the last entry in the section.

The same goes for other values you may want to modify.

If you want to modify several values, you can even use augtool as an interpreter:

$ cat epel.augtool
#!/usr/bin/augtool -sf

# Make a variable
defvar epel /files/etc/yum.repos.d/epel.repo/epel

# Set values
set $epel/enable 1
set $epel/priority 10
set $epel/Testing 0
$ chmod +x epel.augtool
$ ./epel.augtool
Saved 1 file(s)

If you want to keep it in your bash script, you can pipe the commands into augtool:

cat <<EOF | augtool
# Make a variable
defvar epel /files/etc/yum.repos.d/epel.repo/epel

# Set values
set $epel/enable 1
set $epel/priority 10
set $epel/Testing 0
save
EOF

See man augtool for more options.

There are several avantages to using Augeas vs sed/awk:

  • Augeas has parsers that know about the file syntax (in this case, yum.repo syntax). You won't take the risk of breaking the file syntax if your regex are wrong;
  • the Augeas yum.repo parser knows about yum.repo sections, so you know that you're editing the right section;
  • Augeas is idempotent. It will only make changes to the file if there are changes to be made. If all the values are already set, it won't modify the file;
  • Augeas has a Puppet provider, so you can easily automate this in a Puppet infrastructure;
  • if parameters don't exist yet, Augeas will create them for you. If the section doesn't exist yet, Augeas will also create it, with the proper syntax.
raphink
  • 11,337
  • 6
  • 36
  • 47
2

You might also want to check out Augeas, tool for programmatically modifying config files. It is very powerful in combination with Puppet (a configuration management system), but can also be used by itself.

ptman
  • 27,124
  • 2
  • 26
  • 45