Click to See Complete Forum and Search --> : newbie scripting question


hostile_apostle
07-29-2001, 05:57 PM
Hi everyone
I'm attempting to write a simple script (for both practice and practical reasons) that will automate backing up a specific file to a floppy. I've been playing around with it for the past couple of days and sometimes get semi-success and other times none at all.
Please tell me what is going wrong....

code:
---------------------------------------------
#!/bin/bash
#Backup the Moneydance finances file to
#floppy

a=$(date +%T-%d_%m_%Y)
b='finances.md'

grep "fd0" /etc/mtab &> /dev/null

if [ $?=0 ]; then
cp -i $b /mnt/floppy/$b.$a
else
mount /dev/fd0 ;
cp -i $b /mnt/floppy/$b.$a
fi

if [ -e /mnt/floppy/$b.$a ]; then
echo "Backup completed successfully!"
else
echo "An error occurred during the backup process."
fi

---------------------------------------------

I'm pretty sure there's something wrong with the "grep" line... I wasn't quite sure how to let the first conditional know the output of that command.

Also, I would like to know how to stop the script midway and wait for user input. Is there any kind of a "pause" command in bash like there is in DOS?

Thanks for reading my post

hostile_apostle

The Kooman
07-29-2001, 11:41 PM
Originally posted by hostile_apostle:
<STRONG>Hi everyone
I'm attempting to write a simple script (for both practice and practical reasons) that will automate backing up a specific file to a floppy. I've been playing around with it for the past couple of days and sometimes get semi-success and other times none at all.
Please tell me what is going wrong....

code:
---------------------------------------------
#!/bin/bash
#Backup the Moneydance finances file to
#floppy

a=$(date +%T-%d_%m_%Y)
b='finances.md'

grep "fd0" /etc/mtab &&gt; /dev/null

if [ $?=0 ]; then
cp -i $b /mnt/floppy/$b.$a
else
mount /dev/fd0 ;
cp -i $b /mnt/floppy/$b.$a
fi

if [ -e /mnt/floppy/$b.$a ]; then
echo "Backup completed successfully!"
else
echo "An error occurred during the backup process."
fi

---------------------------------------------

I'm pretty sure there's something wrong with the "grep" line... I wasn't quite sure how to let the first conditional know the output of that command.

Also, I would like to know how to stop the script midway and wait for user input. Is there any kind of a "pause" command in bash like there is in DOS?

Thanks for reading my post

hostile_apostle</STRONG>

$? is a number and you should be doing numeric comparison in your "if" statement. So, try this instead:


if [ $? -eq 0 ]; then
cp -i $b /mnt/floppy/$b.$a
else
mount /dev/fd0 ;

hostile_apostle
07-31-2001, 12:41 AM
Thanks a lot! Worked like a charm.

Any suggestions for a good shell programming book?

Bokkenka
07-31-2001, 01:47 AM
<doh!>

The Kooman
07-31-2001, 01:07 PM
Originally posted by Bokkenka:
<STRONG>You can use the "-q" option on grep to make it run quiet, rather than sending to the bit-bucket.</STRONG>

Yes, but if we're bothered about portability of the script with old versions of grep, then its better to redirect it to /dev/null. From "grep man":

Shell scripts intended to be portable to traditional grep should avoid both -q and -s and shouldr edirect output to /dev/null instead

<STRONG>You can also combine the grep into the if statement so you don't have to worry about the exit status variable $?. I've always used...

if df | grep -q fd0

...to check if the floppy is mounted.</STRONG>

That should work, but my personal opinion is that since you're checking whether the device is mounted or not, why not use the "mount" command itself instead of doing a "df" or a "cat /etc/mtab"?

As for shortening the expression, I'd do it in one line like this :):


{ { mount | grep "fd0" &&gt; /dev/null ; } && { cp -i $b /mnt/floppy/$b.$a ; } ; } || { mount /dev/fd0 ; cp -i $b /mnt/floppy/$b.$a; }


Read it out loud and you'll know what it means :)!


<STRONG>
As for pausing, you can use...

read variable_name

...It's how you add user input into a script. Be sure to put an echo before it to tell you it's waiting for input. You can then use the input later, if you want.</STRONG>

hostile_apostle
07-31-2001, 02:24 PM
Thanks for the help, guys.

Would you know of any good shell programming books I could get?

The Kooman
07-31-2001, 11:05 PM
Originally posted by hostile_apostle:
<STRONG>Would you know of any good shell programming books I could get?</STRONG>

The only book I ever read (partially) was by Kernighan and Pike. I think it was called "Unix Shell Programming" or something like that! I just read the first 5 chapthers 'coz I had to do that for some test!!

Apart from that, its been "man bash" countless number of times (and still going on)! Its long, tedious but, IMHO, very rewarding!