View unanswered posts    View active topics

All times are UTC - 6 hours





Post new topic Reply to topic  [ 6 posts ] 
Print view Previous topic   Next topic  
Author Message
Search for:
 Post subject: Help with script writing
PostPosted: Tue Jun 14, 2005 5:55 am 
Offline
Joined: Tue Mar 22, 2005 9:18 pm
Posts: 1422
Location: Brisbane, Queensland, Australia
I am trying to write scripts to automate what I do when I setup my knoppmyth box. I know the very, very basic commands, but I need some help with automating things like edit text files and the like.

An example of a script that I have written is http://users.tpg.com.au/garthk/update-kernel.sh

Please let me know if you could help, thanks.

_________________
Girkers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 6:10 am 
Offline
Joined: Mon May 10, 2004 8:08 pm
Posts: 1891
Location: Adelaide, Australia
Your script seems to have everything you need for editing files.
Code:
sed 's/replace all occurences of this/with this/g'
for changing stuff in files and
Code:
awk '/before this line/{print "insert this line"}{print $0}'
or
Code:
awk '{print $0}/after this line/{print "insert this line"}'
for adding lines to files. What more do you need? You may wish to read up on regular expressions (bits between the //)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 7:57 am 
Offline
Joined: Tue Mar 22, 2005 9:18 pm
Posts: 1422
Location: Brisbane, Queensland, Australia
Yeah Greg thanks, I have been reading a bit on regular experession in the doco with the sed command. Makes sense, wasn't sure had to use awk, but from your script I have learnt how to use sed.

_________________
Girkers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 10:06 pm 
Offline
Joined: Tue Mar 22, 2005 9:18 pm
Posts: 1422
Location: Brisbane, Queensland, Australia
I have used
Code:
sed
in one of my scripts but the problem is, it doesn't work properly. I can give it an input file, but I have no way of outputing back to the same file.

Any pointers on this?

Also I am trying to set up my script so you can say use or no at the start so it will only run the parts that you want it too, eg:
Code:
kernel=yes
webmin=yes
samba=no

...
Code:
if kernel=yes then ...


I know how to set the variables, but how about the if statement? If someone could point me to an online resourse it may help.

PS I just found one, but I thought I would leave the message anyway.

_________________
Girkers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 11:08 pm 
Offline
Joined: Thu Mar 25, 2004 11:00 am
Posts: 9551
Location: Arlington, MA
Girkers wrote:
I have used
Code:
sed
in one of my scripts but the problem is, it doesn't work properly. I can give it an input file, but I have no way of outputing back to the same file.

Classic rookie mistake. ;-) (Sorry, I've taught this stuff, after a while to have to laugh or you cry.) Write to a temp file and replace the old file with that. When I'm writing production quality shell script I usually have a little shell function that "edits a file in place" leaving a backup file with the original contents. Hang on and lt me hunt up an example... Hmmm.. Nothing suitable shows up since I tend to do that with a hunk of Python I wrote these days... Well, here goes nothing... Be warned this will be VERY idomatic and I'll assume BASH so don't say #!/bin/sh at the beginning and expect good things... And this is off the top of my head and COMPLETELY UNTESTED!
Code:
# I've always got these around...  but they're usually much more elaborate...

warn () {
        echo "Warning: $*"
}

fatal () {
        echo "Fatal! $*"
        exit 2
}

d='/'   # In case / is just too painful

replaceStringInFile () {
        # I use this order to make this easier to extend
        from="$1"
        to="$2"
        file="$3"

        [ $# -eq 3 ] || { warn "bad arg count" ; return 1; }
        [ -z "$from" ] || { warn "No from pattern" ; return 1; }
        # Note that an empty to pattern makes perfect sense...
        [ -z "$file" ] || { warn "No filename given" ; return 1; }
        [ -f "$file" ] || { warn "No such file: $file" ; return 1; }
        [ -r "$file" ] || { warn "Cannot read file: $file" ; return 1; }
        [ -w "$file" ] || { warn "Cannot write file: $file" ; return 1; }
        # You should also check that the directory is writable, etc., ...
        tmpFile="$file.tmp.$$"
        cp -p "$file" "$tmpFile" || {
                warn "Cannot create temporary file: $tmpFile" ; return 1; }
        sed -e "$d$from$ds$d$d$to$dg" <"$file" >"$tmpFile" || {
                warn "Cannot edit file: $file" ; return 1; }
        mv "$file" "$file~" || {
                warn "Cannot rename file: $file" ; return 1; }
        mv "$tmpFile" "$file" || {
                warn "Cannot replace file: $file" ; return 1; }
        return 0
}

You should be able to generalize that to other sed commands easily enough... Or even replace the from and to arguments with a sed command to be executed.

Girkers wrote:
Also I am trying to set up my script so you can say use or no at the start so it will only run the parts that you want it too, eg:
Code:
kernel=yes
webmin=yes
samba=no

...
Code:
if kernel=yes then ...


I know how to set the variables, but how about the if statement?

Something like this. Note the spaces and the quotes are VITAL.
Code:
if [ "$variableNameHere"  = "yes" ] ; then
    doYesStuff
else
    doNoStuff
fi

Or this:
Code:
case "$var" in
[Yy]|[Yy][Ee][Ss])
    doYesStuff
    ;;
[Nn]|[Nn][Oo])
    doNoStuff
    ;;
*)
    doWTHStuff
    ;;
esac


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 1:27 am 
Offline
Joined: Tue Mar 22, 2005 9:18 pm
Posts: 1422
Location: Brisbane, Queensland, Australia
Thanks tjc I really appreciate the lesson, whilst I was waiting for a reply I did some googling and found a sed example very similar to yours. I also found about the if statement too. I appreciate you taking the time.

I have since updated my script to reflect the changes I made and commented a little bit more.

_________________
Girkers


Top
 Profile  
 

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


All times are UTC - 6 hours




Who is online

Users browsing this forum: No registered users and 88 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Theme Created By ceyhansuyu