D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] Waiting for keyboard input in a shell script

 

Hi folks,

I'm in the process of trying to write a script to write backup files to
CD/DVD.  I've so far managed to get the script to pick up what files to
write.  What I'm trying to acheve now is for the script to prompt the
user to put a blank CD/DVD in the DVD writer and press a key to commence
the CD writing process.

The bit of code I'm stuck on is:

    diskcount="1"

    ls | grep -i cpio | while read fn; do
        if [ ! `echo "$fn" | grep -i last` ]
    then
        echo
        echo "Please insert a blank disc in your CD/DVD writer and press
RETURN"
        echo "to commence writing disc $diskcount of the backup set."
        echo
        read
        command-to-burn-cd
    else
        echo
        echo "Please insert a blank disc in your CD/DVD writer and press
RETURN"   
        echo "to commence writing of the final disc in this backup set."
        echo
        read
        command-to-burn-cd
        diskcount=`expr $diskcount + 1`
    echo "Disc number: $diskcount"
    fi

    done

The filenames the backup script is giving me are:

200511251348.cpio.bz2.000 and 200511251348.lastcpio.bz2.001

>From what I can work out, the last file of the backup set has lastcpio
in the filename.  I've put in the grep -i last bit in so I can work out
when the last backup file is to be written (the backup files are 650MB
each).

Now the problem I'm having is with the read command which is just before
command used to burn the CD.  I've tried using 'read' and I've even
tried writing a very basic C program which just executes getchar() but
none of the commands seem to work, they just appear to be ignored.

I've tried running the 'read' command and also the C program I wrote and
they both work fine from the shell prompt.

So... I'm tearing my hair out now.  I'm thinking the alternative is
using the sleep command and setting a high value like 1 minute.  I'd
like to avoid that if I can just in case the end user isn't near the
server when they're running the command.

Any advice on this would be grateful.  This is my first serious *nix
script, so I'm sure there is a more elegant way of doing this (perl,
php?), but so far it appears to be working okay apart from that one part
about waiting for a keypress.

Rob



--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe. FAQ: www.dcglug.org.uk/linux_adm/list-faq.html