#!/bin/kill-9.hobbiton.org

news:

stuff to click

home
blog
read news
linux & me
* shell scripting *
unix shells
what is kill-9?
BOFH
got geek?
l33tsp33k
 
hi morgen!

tools used

proud to be WYSIWYG free!

Chris's lack of artistic ability has been cleverly disguised by the GIMP

php

Apache, jump on it!

Powered by openBSD

my first shell script

once upon a time, there was boy named chris who just loved to do web pages. he would spend hour after hour in his text editor tinkering and tinkering with html and javascript code. often, chris would make a mistake, or change his mind about a particular file, and he would have to start again from scratch. sometimes things would change on his host, like the absolute path name of his cgi-bin, or the path to sendmail or perl. this would make chris very sad because his pages wouldn't work. he would say to himself "if only it were easy to archive my whole site periodically..."

then one day, chris ran across an article about writing shell scripts. shell scripts made chris very happy because now he could write one to take care of his archiving problems, and they all lived happily ever after.

maybe shell scripts won't help you to live happily ever after, but they can make your life easier. shell scripts are nothing more than fancy dos batch files. one really cool thing about UNIX is that you can set permissions on any file to make it executable (unlike DOS which you must name a file .bat, or a binary .exe). so i can write any file name i like (not just 8.3) and make it executable. here's how it works:

  1. get the path to the interpreter it will run under and put it at the beginning of the file like this:

    #!/bin/sh

    i don't know if this is the proper terminology, but essentially you can write a script, but it must be interpreted by something. a traditional program is compiled into binary, so it interprets itself. you can write simple scripts like this one, that are interpreted by a shell, but there are scripting languages like perl and python that are much more powerful. you can find this with the which command. just type which bash for the bash shell. there are many shells in unix, and i don't really know enough about them, i usually use bash or ksh (on irix boxes). there are shells in NT called CMD, and i winblows, it's called command.com. the explorer shell (kind of like a window manager is in X) is a different sort of shell that behaves differently. in unix, different shells do different things. an example is csh, which works like C, so if you are a C programmer, you can write shell scripts which are very similar to C. i don't know anything about C, so don't quote me on that.
  2. write in the commands that you want the script to execute like this:
    cp bakup/web.tar.gz bakup/webold.tar.gz
    rm bakup/web.tar.gz
    tar -cvf bakup/web.tar www
    tar -tvf bakup/web.tar > bakup/bakup.txt
    gzip bakup/web.tar
    in case it's not obvious, all this does is copy the current backup to webold, delete the current backup (i could use mv, to do this in one step but i don't trust mv, call me crazy), use tar to create a new backup, user tar again to list the contents of that backup and redirect the output to bakup.txt, then compress the backup with gzip. easy money.

this script might work verbatim for you, it might not. a few minutes with which, man, and your favorite editor can go a long way to boosting your productivity ;-)

after doing more reading i found a Better Way to do the same thing. so here it goes:
#!/bin/sh
ls -lR www > bakup/www_bakup_`date '+%d-%B-%Y'`.txt
tar -zcvpf bakup/www_bakup_`date '+%d-%B-%Y'`.tar.gz --directory www

again, line 1 redirects ls into a text file, only this time it includes the result of the date command. the second line creates the tar file and compresses it in one pass, this time naming it in a similar fashion as the .txt file using the date command. since each tar file now has a different name, there is no need to futz with cp, mv or rm.

webbak, my first shell script
webbak 2, the sequel
getnews, your news authority
su.bat, superuser for NT
sed tutorial, very informative
    home     blog     read news     linux & me     * shell scripting *     unix shells   what is kill-9?     BOFH     got geek?     l33t