LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash / Sed help? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-sed-help-4175449090/)

gdizzle 02-07-2013 06:42 PM

Bash / Sed help?
 
Hi All,
I need to insert:

Code:

prompt=(\\u@\\h) [\\d]>\\_
Into my /etc/my.cnf file via a bash script using either echo or sed.

I am going a bit crazy can anyone please assist?

Thanks

evo2 02-07-2013 06:58 PM

Hi,

the following works in zsh

Code:

echo 'prompt=(\\\\u@\\\\h) [\\\\d]>\\\\_' >> /etc/my.cnf
If you are using bash I don't think you need to escape the backslashes, so:
Code:

echo 'prompt=(\\u@\\h) [\\d]>\\_' >> /etc/my.cnf
What shell are you using?

Evo2.

EDIT: oops I see now that you already specified bash.

chrism01 02-07-2013 06:59 PM

Code:

echo 'prompt=(\\u@\\h) [\\d]>\\_' >t.t

cat t.t

prompt=(\\u@\\h) [\\d]>\\_


gdizzle 02-07-2013 07:04 PM

Thanks chrism01!

I was doing echo -e and having issues....

This solved it: echo 'prompt=(\\u@\\h) [\\d]>\\_' >> /etc/my.cnf

Thanks evo2 also for your reply!

---------- Post added 02-08-13 at 02:05 AM ----------

Thanks chrism01!

I was doing echo -e and having issues....

This solved it: echo 'prompt=(\\u@\\h) [\\d]>\\_' >> /etc/my.cnf

Thanks evo2 also for your reply!

David the H. 02-09-2013 10:55 AM

There are lots of ways to add text to a file, the simplest of which don't require any external commands, just a '>>' redirect.

A slightly more complex one, which can be used to add multiple lines at once, is a here document.

Code:

cat >>/etc/my.cnf  <<"ENDSTRING"
newline1
newline2
newline3
ENDSTRING

All of the text between the ENDSTRINGS will be directed as-is to the file. The quote marks around the first ENDSTRING keep any variable names or other patterns from expanding. Remove them if you want substitutions to be done before printing.

http://mywiki.wooledge.org/BashGuide...nd_Herestrings

For more advanced editing work inside scripts, such as inserting lines at the beginning of a file, you do generally have to use an external command. But instead of sed, consider using ed.


http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)


All times are GMT -5. The time now is 04:31 AM.