Recommended Posts

I have a bash script where I am trying to get this funtion to work

it needs to find the first instance of a line and append a line after it

function writeline()

{

ADDLINE="\<rdp_server client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\" title=\"$TITLE\"\ \/\>"

eval "sed -e s/^<rdp_server/$ADDLINE/ $INFILE > $TMPFILE"

}

both $INFILE and $TMPFILE are declared the error I get is

./addsub: line 24: rdp_server/<rdp_server: No such file or directory

thanks

Link to post
Share on other sites

thanks for the reply but it still fails for me

here is what I get./addsub: line 27: rdp_server/$ADDLINE/: ambiguous redirect

knoppix@Microknoppix:~$ vi addsub

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/$ADDLINE/: ambiguous redirect

knoppix@Microknoppix:~$ vi addsub

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/\<rdp_server client_audio="On"\ color_depth="16"\ comment="Created by addsub script"\ experience="0"\ gateway="12.0.0.1"\ geometry="90%"\ ip=""\ submenu="" title=""\ \/\>/: No such file or directory

knoppix@Microknoppix:~$

here is the entire script

#Script Variables, hard coded values are site default
AUDIO="On"
COLOR="16"
COMMENT="Created by addsub script"
EXPERIENCE="0"
GATEWAY="12.0.0.1"
GEOMETRY="90%"
IP=""
SUBMENU=""
TITLE=""
INFILE="./testfile"
TMPFILE="./tempfile"


function writeline()
{
ADDLINE="\<rdp_server client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\" title=\"$TITLE\"\ \/\>"

sed -e s/^<rdp_server/"$ADDLINE"/ $INFILE > $TMPFILE
}


writeline

Link to post
Share on other sites

going to this

ADDLINE='<rdp_server client_audio=\"$AUDIO\" color_depth=\"$COLOR\" comment=\"$COMMENT\" experience=\"$EXPERIENCE\" gateway=\"$GATEWAY\" geometry=\"$GEOMETRY\" ip=\"$IP\" submenu=\"$SUBMENU\" title=\"$TITLE\" />'

sed -e s/^<rdp_server/"$ADDLINE"/ $INFILE > $TMPFILE

gives me

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/<rdp_server client_audio=\"$AUDIO\" color_depth=\"$COLOR\" comment=\"$COMMENT\" experience=\"$EXPERIENCE\" gateway=\"$GATEWAY\" geometry=\"$GEOMETRY\" ip=\"$IP\" submenu=\"$SUBMENU\" title=\"$TITLE\" />/: No such file or directory

Link to post
Share on other sites

really I want to find the first match then add my line, not to everyline that matches

thanks

I have been beeting my head on doing this in bash and python (my only choices besides pearl which I no nothing about)

Link to post
Share on other sites

You have to quote the argument to sed. FWIW, I think it would be easier to use ed and a here-doc:

ed $INFILE <<EOL
/^<rdp_server/c
<rdp_server client_audio="$AUDIO" color_depth="$COLOR" comment="$COMMENT"
experience="$EXPERIENCE" gateway="$GATEWAY"
geometry="$GEOMETRY" ip="$IP"
submenu="$SUBMENU" title="$TITLE"/>
.
wq $OUTFILE
EOL

Link to post
Share on other sites

hmmm

but if i quote the variable is not passed its taken as a literal value

I am writing this script to let a windows admin add line (questions will fill in the data later) so I am putting it in a script, if it was me I would just open the xml file and yyp the lines and edit them, but its not for me..

I don't have ed but thanks

Link to post
Share on other sites
but if i quote the variable is not passed its taken as a literal value

Double-quote.

I don't have ed but thanks

Where did you find a system with bash and sed but not ed? How about ex?

Link to post
Share on other sites

ok this is close

<code>

#Script Variables, hard coded values are site default

AUDIO="On"

COLOR="16"

COMMENT="Created by addsub script"

EXPERIENCE="0"

GATEWAY="12.0.0.1"

GEOMETRY="1660x1024"

IP=""

SUBMENU=""

TITLE=""

INFILE="./testfile"

TMPFILE="./tempfile"

function writeline()

{

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/\>"

sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE

}

writeline

</code>

but I only want to append the line after the first find ?

thanks

Link to post
Share on other sites
Where did you find a system with bash and sed but not ed? How about ex?

Knoppix..

Who knows..

maybe I should use awk...

I am so out of bash scripting I feel dumb..

Link to post
Share on other sites

got it..

{

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/\>"

#sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE

sed "0,/^<rdp_server/a$ADDLINE/" $INFILE > $TMPFILE

}

Thanks..

Link to post
Share on other sites

ok change but I can noit get it to write to the file

just standard output

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/>"

#sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE
sed -e "/^<rdp_server/a$ADDLINE/" $INFILE -e "/^<rdp_server/q" < $INFILE

Link to post
Share on other sites
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...