Slashdot Mirror


Adding Some Spice To *nix Shell Scripts

An anonymous reader writes "Developing GUI script-based applications is time-consuming and expensive. Most Unix-based scripts run in a CLI mode or over a secure ssh session. The Unix shells are quite sophisticated programming languages in their own right: they are easy to design and quick to build, but they are not user-friendly in the same way the Unix commands aren't (see the Unix haters books). Both Unix and bash provide features for writing user friendly scripts using various tools to build powerful, interactive, user-friendly scripts that run under the bash shell on Linux or Unix. What tools do you use that spice up your scripts on the Linux or Unix platforms?"

5 of 411 comments (clear)

  1. None! by Anrego · · Score: 5, Insightful

    I know this is troll-ish, but the way I view it a script is just that.. a script. A series of commands to be executed in a specific order designed to automate a repetative task. Basic logic, control, and input are generally ok.. but interaction is in my opinion an indicator that your task is out of scope for a "script" and should become a full fledged application.

    (you may now freely argue amongst yourselves on the difference between a script and an application)

    There are a metric ass-tonne of dialog-type apps out there .. just google for your favorite toolkits prefix and "dialog" and you'll probably find something..

    gdialog
    kdialog
    xdialog
    etc..

  2. tools I found useful by tpwch · · Score: 5, Informative

    Here are some random things I find useful, related to user interaction (mostly becuase it notifies the user):

    Oven timer:
    sleep $((20*60)); xmessage "Dinner is done"

    Quick macro for automating some repetitive task in a program:
    xdotool type "something"; xdotool key Return; xdotool mousemove $x $y; xdotool click 1; (and so on)

    Copying a file to/from the clipboard (can also copy from /topipe, so the output of any command). Faster than opening a text editor:
    xclip -in file

    Notifying me when some specific thing changed on a website:
    CHECKLINE="$(curl -s http://somewebsite.org/somepage.html | grep "currently undergoing maintenence")"
    while true; do
        sleep 120
        [ -z "$CHECKLINE" ] && xmessage "somewebsite is open again" && exit
    done

    Or just checking for changes in general (I use this for notifying me when something changed when tracking something I ordered, so I know the minute the package is ready to get picked up at the post office):
    while true; do
        OLD_MD5=${MD5}
        CONTENT=$(elinks -dump 1 -dump-charset iso-8859-1 "http://someurl.com/track?id=someid")
        MD5=$(echo -n $CONTENT | md5sum -)

        [ "${MD5}" != "${OLD_MD5}" ] && {
            xmessage "$(printf "New action: :\n\n${CONTENT}")"
        }
        sleep 120
    done

    If you don't want to interrupt what you're doing with a pop-up you can pipe it to osd_cat instead to have the text appear over whatever program you're currently working with. Adding a few beep; beep; beep; beep; is also a good way to get your attention if you're not paying 100% attention to your computer all the time.

    --
    Posted by a Debian GNU/Linux user
  3. Consistency is the only spice ... by bhepple · · Score: 5, Insightful

    As said previously, scripts are scripts and don't often need a GUI. But for grep's sake, make them consistent!!! The only spicing up _really_ needed are some standards:

    o output errors to STDERR; normal output to STDOUT
    o include (-h, --help) processing - and send it to STDOUT so the help can be piped to 'less'
    o use getopt(1) or process-getopt(1) so that options on the CLI parse in a predictable and standard way
    o keep it terse except for errors so that the user can easily see if it worked or not without scanning vast output
    o provide a --verbose option to help with tracking down those errors

    ... and the most annoying thing of all - make sure --help _always_ works, even if the script body itself can't - at least the user can then be told about what the prerequisites are.
    Head over to http://mywiki.wooledge.org/BashFAQ for much wisdom on how to write better bash scripts.

  4. Re:Best book for this -- hands down. by actionbastard · · Score: 5, Informative

    The only bash scripting guide you will ever need:

    http://tldp.org/guides.html

    free as in beer.

    --
    Sig this!
  5. Re:None, I have given up bash scripting by camperdave · · Score: 5, Insightful

    You still need to work with the user's files, which will inevitably have spaces in them. If a space is a valid character in the filesystem then your scripts need to reflect that. Erroring out is not a valid solution.

    --
    When our name is on the back of your car, we're behind you all the way!