0

I need to change and create several configuration files for some kiosks at work, is there a way to do this with a single bash script? I've heard about SED but the documentation is confusing, could someone give an example?

5
  • 1
    The generic answer is yes: echo 'text into this file' > 'a new file'. I don't see how this helps change configuration files though. Maybe you want to be more specific about the changes you need to make to certain files?
    – drs
    Aug 26, 2014 at 17:57
  • I'm very new to bash, do I need to specify the file directory in 'a new file'?
    – Scott Kunz
    Aug 26, 2014 at 18:06
  • By default a new file will be in the current working directory. You can specify another directory in the manner that you'd expect: > 'subdir/a new file' or > '../a new file' or > /root/a new file.
    – drs
    Aug 26, 2014 at 18:09
  • Could you give an example of what exactly it is that you need to change in these configuration files? If all you intend to do is replace word 'abc' with 'xyz', sed might be the perfect tool for you. If not, maybe some other tool can be suggested based on your requirement. Aug 26, 2014 at 18:28
  • Are you editing, creating, or appending files? If creating then > will redirect to a new file (or overwrite an existing one). If you want to append a file then >> adds lines to the end of a file. If you want to modify then your friends will be the redirections as above PLUS cat, sed, cut, and grep. The above redirections are for standard output. If you are doing more advanced tasks you will need different redirections. Aug 27, 2014 at 23:58

1 Answer 1

2

You are not clear: Are you editing old files, or creating entirely new ones?

New ones can be created in this style with a single bash script:

$ cat script.sh 
#!/bin/bash

cat <<EOF >firstfile
1. The quick brown fox jumped over the lazy dog.
EOF

cat <<EOF >secondfile
2. The quick brown fox jumped over the lazy dog.
EOF

echo Done.

$ chmod 755 script.sh 

$ ./script.sh 
Done.

$ for f in *file ; do echo -e "\n--- $f ---" ; cat $f ; done

--- firstfile ---
1. The quick brown fox jumped over the lazy dog.

--- secondfile ---
2. The quick brown fox jumped over the lazy dog.

$ 

To change simple things in already existing files:

$ tail -n 2 dead.letter 
With kind regards,
Hannu

$ sed -i -re 's/ kind/ very best/' dead.letter

$ tail -n 2 dead.letter 
With very best regards,
Hannu

$ 
3
  • Sorry, never used this site before. I'm actually doing both. Some of the files already exist, and some I need to create entirely new. I think I have it figured out from your example though, thanks!
    – Scott Kunz
    Aug 26, 2014 at 18:11
  • General scripting can be learnt from the Bash guides at tldp.org/guides.html
    – Hannu
    Aug 26, 2014 at 18:14
  • Changing files... ^- added.
    – Hannu
    Aug 27, 2014 at 19:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.