[prog] [Bash] script question

Conor Daly conor.daly at oceanfree.net
Sun Sep 29 11:01:48 EST 2002


On Sun, Sep 29, 2002 at 05:00:58PM +1000 or so it is rumoured hereabouts, 
Shuying thought:
> Heya,
> 
> I've written a small script that doesn't do what I want it to do,
> namely rename all files in a particular directory and then zip them all
> up...Can any one tell me what I've done wrong?
> 
> #!/bin/sh
> 
> maildir=~shuyingw/mail
> datestamp=`date +%b-%Y`
> 
> for file in $maildir
> do
>   mv $file $file-$datestamp
>   touch $file
> done
> 
> tar cjvf - $maildir/*$datestamp > /tmp/mail-$datestamp.tar.bz2

Without seeing the result of running the script, it can be difficult to
diagnose.  However, your line

   mv $file $file-$datestamp

may be giving you a problem.  Are you getting just a single file called
$datestamp instead of a list of files?  Chances are teh shell is
interpreting 

$file-$datestamp as  $file- followed by $datestamp instead of 

$file followed by a '-' followed by $datestamp.  The formar case will give
you 

foo > $datestamp
bar > $datestamp

while the latter gives

foo > foo-$datestamp
bar > bar-$datestamp


The other possible problem is in:

for file in $maildir

since $maildir = ~shuyingw/mail

$file will only contain that value.  What you need there is something like

cd $maildir
for file in `/bin/ls`

Note the use of /bin/ls rather than ls alone.  This will bypass any "ls
--color" type aliases which will produce odd characters in the filenames.

Last thing.  

#!/bin/sh

will make the script behave differently to your "normal" bash shell.

#!/bin/bash 

should produce smae behaviour.

Looks OK otherwise...

Conor
-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Faenor.cod.ie
 10:58am  up 29 days, 15:25,  0 users,  load average: 0.15, 0.03, 0.01
Hobbiton.cod.ie
 10:53am  up 29 days, 15:02,  2 users,  load average: 0.12, 0.10, 0.09



More information about the Programming mailing list