user2031888 user2031888 - 1 year ago 108
Bash Question

Changing contents of a file through shell script

I have a requirement where in I need to change the contents of a file say xyz.cfg. the file contains values like:

group address=
Jboss username=xyz_ITR3

I want to change this content when ever needed through a shell script and save the file. Changed content can look like:

group address=
Jboss username=xyz_ITR4

How can i achieve this using shell script by taking user input or otherwise?

Answer Source

How about something like:



sed -i -e "s/\(address=\).*/\1$1/" \
-e "s/\(port=\).*/\1$2/" \
-e "s/\(username=\).*/\1$3/" xyz.cfg

Where $1,$2 and $3 are the arguments passed to the script. Save it a file such as and make sure it executable with chmod +x then you can run it like:

$ ./ 7822 xyz_ITR4

$ cat xyz.cfg
group address=
Jboss username=xyz_ITR4

This gives you the basic structure however you would want to think about validating input ect.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download