Re: [SLUG] Difficulty level for a beginner?

From: Kwan Lowe (kwan@digitalhermit.com)
Date: Sat Mar 12 2005 - 06:20:10 EST


404 wrote:
> As you can see from my last post, I want to learn to code. I have been
> using bash scripts to do stuff on my system but I would like to be able
> to code it to an actual program. Example:
>
> #!/bin/bash
> CAT=/bin/cat
> ECHO=/bin/echo
> GREP=/bin/grep
>
> $ECHO "[------- Battery Info ---------]"
> $CAT /proc/omnibook/battery | $GREP Stat
> $CAT /proc/omnibook/battery | $GREP Gau
> $CAT /proc/omnibook/ac
> $ECHO "[------- Fan and Temp ---------]"
> $CAT /proc/omnibook/temperature
> $CAT /proc/omnibook/fan
> $ECHO "[------------------------------]"
>

This is a functional beginning script. As others have mentioned, that's
probably the biggest goal of the script in the first place. I think it's
fair to say that there are classes of script. There are one-of, single
purpose scripts to do machine and task specific things and others that
are more akin to full-featured programs. This purpose determines how
elaborate the script becomes.

Somewhat coincidentally, I've begun to audit some of the scripts on the
machines that I manage and wanted to emphasize the need to think through
every script no matter how simple. Take this simple script:

---start
#!/bin/ksh

cd /var/log/httpd

find . -mtime +21 -type f -exec rm -f {} \;
---end

Looks innocous.. This one in particular runs as a system user that
happens to have $HOME set to the web document root and clears logfiles
older than three weeks. But there are huge problems with this approach.
For one, if /var/log/httpd is ever inaccessible, the script will still
continue and wipe out everything in the default directory when it runs.
Many things can make this happen: the filesystem may not be mounted, the
permissions on the dir may change or even get moved, etc..

As your scripts get more elaborate and/or critical, it's important to
start testing for failure codes. Trap errors. Use case statements or
getopts to handle user input. Start building tested function libraries
to do things like mail logs, test variables, etc..
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:35:00 EDT