LinHES Forums http://forums.linhes.org/ |
|
Help with script writing http://forums.linhes.org/viewtopic.php?f=5&t=4910 |
Page 1 of 1 |
Author: | Girkers [ Tue Jun 14, 2005 5:55 am ] |
Post subject: | Help with script writing |
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. |
Author: | Greg Frost [ Tue Jun 14, 2005 6:10 am ] |
Post subject: | |
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 andCode: awk '/before this line/{print "insert this line"}{print $0}' orCode: 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 //)
|
Author: | Girkers [ Tue Jun 14, 2005 7:57 am ] |
Post subject: | |
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. |
Author: | Girkers [ Wed Jun 15, 2005 10:06 pm ] |
Post subject: | |
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. |
Author: | tjc [ Wed Jun 15, 2005 11:08 pm ] |
Post subject: | |
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. ![]() 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 |
Author: | Girkers [ Thu Jun 16, 2005 1:27 am ] |
Post subject: | |
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. |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB® Forum Software © phpBB Group http://www.phpbb.com/ |