You are not logged in.

#1 2007-08-12 22:47:32

jnengland77
Member
From: Black Hills, USA
Registered: 2005-05-06
Posts: 111

need help with a bash script and sed

Here is the script: http://pastebin.archlinux.org/12509

Basically I'm making this script to automate the process of creating svn repositories a bit, and to help a gaming server (revora.net : C&C community).

If anything I just need help getting that sed section to work, but any other corrections or suggestions for the script would be grea, too.  This is my first bash script that has purpos and hopefully I did an ok job with it so far.  tongue Yes, that get_input function is copied from adduser script; it came in handy.

After the script is done another community member is going to make a php script which uses this script to allow hostees and staff of the site to create repositories. 

So any help or suggestions would be great.

~jnengland77

Offline

#2 2007-08-12 23:30:54

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: need help with a bash script and sed

I think what you're looking for is this?

# copy svnserve.conf based on whether 
if [[ ${priv_pub} = private ]]; then
        cp ${svn_stuff}/confs/svnserve.conf_priv svnserve.conf
        sed -i "s/password_file/\"$repoconf_dir\"\/${reponame}\/yourmoma/g" ${svnrepo_dir}/conf/svnserve.conf
else
        cp ${svn_stuff}/confs/svnserve.conf_pub svnserve.conf
        sed -i "s/password_file/\"$repoconf_dir\"\/yourmoma/" ${svnrepo_dir}/conf/svnserve.conf
        sed -i "s/division_name/\"$division\"/g" ${svnrepo_dir}/conf/svnserve.conf
fi

Basically, any variables ($foo) between single-quotes ( ' ) are not evaluated.  So, in a bash script, '$foo' ends up being exactly the string $foo, whereas "$foo" will evaluate the variable foo.

eg.

#!/bin/bash

foo=4

# will output $foo to the screen
echo '$foo'
# will output 4 to the screen
echo "$foo"

Last edited by Cerebral (2007-08-12 23:32:31)

Offline

#3 2007-08-12 23:42:51

jnengland77
Member
From: Black Hills, USA
Registered: 2005-05-06
Posts: 111

Re: need help with a bash script and sed

Thanks for your help.  The one that replaces the division_name works great (I didn't want the " " so I removed them).  However, the one with password_file doesn't change. 

I think I tried sed before with double-qoutes, but it did and still does give me this error: sed: -e expression #1, char 18: unknown option to `s' .  So I tried to find a different way, but couldn't get anything to work.

~jnengland77

Last edited by jnengland77 (2007-08-12 23:59:58)

Offline

#4 2007-08-13 00:38:04

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: need help with a bash script and sed

what about this?

# copy svnserve.conf based on whether 
if [[ ${priv_pub} = private ]]; then
        cp ${svn_stuff}/confs/svnserve.conf_priv svnserve.conf
        sed -i "s#password_file#${repoconf_dir}/${reponame}/yourmoma#g" ${svnrepo_dir}/conf/svnserve.conf
else
        cp ${svn_stuff}/confs/svnserve.conf_pub svnserve.conf
        sed -i "s#password_file#${repoconf_dir}/yourmoma#" ${svnrepo_dir}/conf/svnserve.conf
        sed -i "s#division_name#${division}#g" ${svnrepo_dir}/conf/svnserve.conf
fi

Just for an explanation - sed will use whatever character follows the 's' as its delimiter.  Since your pathnames had the '/' character in them, it was probably confusing sed, because if repoconf_dir=/my/path and reponame=bob, then a line like this

"s/password_file/$repoconf_dir\/$reponame\/yourmoma/g"

would be passed to sed like this:

"s/password_file//my/path\/bob\/yourmoma/g"

Notice how the forward slashes in /my/path would confuse sed, since they're not escaped.

Changing the delimiter to # however, would make the line

"s#password_file#/my/path/bob/yourmoma#"

And no confusion happens - it treats the forward slashes like regular characters.

Last edited by Cerebral (2007-08-13 00:43:11)

Offline

#5 2007-08-13 00:51:10

jnengland77
Member
From: Black Hills, USA
Registered: 2005-05-06
Posts: 111

Re: need help with a bash script and sed

Yeah that works great, thanks.  Does the rest of my script look fine? 

I did try using # as well, but not work double-qoutes. I guess in the frustion I didn't give it a try.

~jnengland77

Last edited by jnengland77 (2007-08-13 00:53:35)

Offline

#6 2007-08-13 11:17:21

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: need help with a bash script and sed

Yeah, the rest of it looks pretty alright.  There's enough similarity in your input routines that I feel like some refactoring can be done there, (while needinput, get input, validate input) but I'm not sure how I'd do the refactoring personally.

Offline

#7 2007-08-13 22:29:42

jnengland77
Member
From: Black Hills, USA
Registered: 2005-05-06
Posts: 111

Re: need help with a bash script and sed

Yeah I'm not really sure how else to do it.  I basically tried to mimic how adduser script works, and used a couple of bash tutorials.  Here is the newest script.  Not a lot changed, but fixed a few errors.  http://pastebin.archlinux.org/12573

Thanks for your input,
jnengland77

Offline

Board footer

Powered by FluxBB