Hi,

I am having trouble inserting lines into a file via a Unix shell script, can anyone out there help me with this?

Part of my file content looks like this:

# default ssl qop
[ssl-qop-mgmt-default]
default = ALL

I now need this part to read as follows:

# default ssl qop
[ssl-qop-mgmt-default]
default = DES-168
default = RC2-128
default = RC4-128
#default = ALL

One way is to use an ed script. For example

$ cat ed.script
/ssl-qop-mgmt-default/a
default = DES-168
default = RC2-128
default = RC4-128
#default = ALL
.
+1d
wq
$

To make the change in your file, do the following

ed file < ed.script

Hi,
I attempted your solution and it did not work, or maybe I ran it wrong:

(/home/cap) $ cat ed.script
/ssl-qop-mgmt-default/a
default = DES-168
default = RC2-128
default = RC4-128
#default = ALL
.
+1d
wq

(/home/cap) $ cat tempfile
# this is a test file

# Enable/Disable SSL Quality of Protection management
ssl-qop-mgmt = no


# Legal cipher values for qop in the following stanzas are:
# NONE, ALL, NULL, DES-56, FIPS-DES-56, DES-168, FIPS-DES-168,
# RC2-40, RC2-128, RC4-40, RC4-56, RC4-128, AES-128, AES-256
#
# Notes:
# - NONE = No SSL connection allowed.

# default ssl qop
[ssl-qop-mgmt-default]
default = ALL

# Notes:
# - NONE = No FTP connection allowed.

(/home/cap) $ ed tempfile < ed.script
386
?
?

(/home/cap) $

Check your ed script. I'll bet there is at least one space at the beginning of most of the lines, which happens when one copy/pastes from a web browser.

The ed() commands should be at the beginning of the line. The solo '.' that indicates the end of new data must be the only character on its line.

Oh, perhaps we are incorrectly assuming you are using UNIX/Linux for this. If using Winders, your editor might be putting other characters in the ed script, like a <CR> at the end of each line. To see invisible characters that might be screwing up the ed script, try:

od -c ed.script

You should see exactly the characters you entered, '\n' for the new lines and blanks for spaces. If you see any '\r's, this might explain your trouble.

use awk

awk '/default = ALL/{
 print "default = DES-168"
 print "default = RC2-128"
 print "default = RC4-128"
 print "#"$0
 next}1' file
commented: Yep, probably as good as anything +32

I am using Unix, AIX to be exact. Played with that ed.script and got nowhere, do not know where my troubles layed so I skipped it.

Fiddled with "sed" over the weekend and got it working. Created 2 scripts. "Script A" creates a copy of the file(s) and then edits the original while calling "Script B" to make the changes. Could not figure out how to make it all one script but it does the trick. Hope this helps someone else who may have been having the same troubles:

Script A
======
today=`date +"%m-%d-%y-%T"`

ls *.conf > conf.out
cat conf.out | while read data
do
`cp $data BKP/$data."$today"`
`ScriptB < $data > $data.newone`
`cp $data.newone $data`
done

rm -f *.newone

Script B
======
sed '
/ssl-qop-mgmt = no/ c\
ssl-qop-mgmt = yes\

/default = ALL/ c\
default = DES-168\
default = RC2-128\
default = RC4-128\
default = AES-128\
default = AES-256\
\#\default = ALL\
'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.