Operating System - Linux
1827452 Members
4643 Online
109965 Solutions
New Discussion

Re: A strange script problem?

 
Chowroc
Occasional Advisor

A strange script problem?

I just write such a shell script to backup my working directories:

#!/bin/bash

WORK_DIR="/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}"
DEVICE_FILE="/mnt/back/backup_work_`date +%F`_"
DATE_TAG="work_full-back-date.tag"
BACK_LIST="/tmp/work_backlist"

if [ "$1" == "-full" ]; then
DEVICE_FILE=$DEVICE_FILE"full.tar.gz"
tar -c -f $DEVICE_FILE $WORK_DIR -z
chmod 600 $DATE_TAG
echo "`date +%F` full" > $DATE_TAG
chmod 400 $DATE_TAG
else
DEVICE_FILE=$DEVICE_FILE"inc.tar.gz"
echo $DEVICE_FILE
echo $WORK_DIR
find $WORK_DIR -newer $DATE_TAG > $BACK_LIST
tar -c -T $BACK_LIST -f $DEVICE_FILE -z
rm $BACK_LIST
fi

But it report:
tar: /mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}: Cannot stat: No such file or directory
or:
find: /mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}: No such file or directory

But if I run the tar/find command handly, it's OK? I don't know why?

Is any one can help me? thank you.
11 REPLIES 11
xyko_1
Esteemed Contributor

Re: A strange script problem?

Hi Chowroc,

did you tried to run the script in debug mode, i.e.,

#sh -x script.name


??

Report news to us.

Regards,
Xyko
Emir Faisal
Frequent Advisor

Re: A strange script problem?

try to use single quote for shell globbing.

ex:
workdir='/{tmp,etc}'

EF
Everything is possible, if you don't know what you're talking about.
Michael Schulte zur Sur
Honored Contributor

Re: A strange script problem?

Hi,

according to man bash no quotes can be used with brace expansion i.e.
WORK_DIR=/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}

greetings,

Michael
Chowroc
Occasional Advisor

Re: A strange script problem?

I run the commands handly like this:
(1) $tar cfz archive.tar.gz DIR/{file1,file2,...}
$find DIR/{file1,file2,...}

(2) $tar cfz archive.tar.gz 'DIR/{file1,file2,...}'
$find 'DIR/{file1,file2,...}'

(1) is OK, (2) can't find the files.

I use the way Michael Schulte said, problem still.

so I use #sh -x script.name to see my script,
I saw when tar/find execute, it add '' automatically, how I can strip that ?

Thank you very much.
Michael Schulte zur Sur
Honored Contributor

Re: A strange script problem?

Hi,

the find works for me. What error do you get? There should be no '
Did you delete them from the script?
Can you post the output of bash -x script?

thanks,

Michael
Chowroc
Occasional Advisor

Re: A strange script problem?

so, just like this:
# sh -x back_work.sh

+ WORK_DIR=/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}
++ date +%F
+ DEVICE_FILE=/mnt/back/backup_work_2004-12-15_
+ DATE_TAG=work_full-back-date.tag
+ BACK_LIST=/tmp/work_backlist
+ '[' '' == -full ']'
+ DEVICE_FILE=/mnt/back/backup_work_2004-12-15_inc.tar.gz
+ echo /mnt/back/backup_work_2004-12-15_inc.tar.gz
/mnt/back/backup_work_2004-12-15_inc.tar.gz
+ echo '/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}'
/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}
+ find '/mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}' -newer work_full-back-date.tag
find: /mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}: No such file or directory
+ tar -c -T /tmp/work_backlist -f /mnt/back/backup_work_2004-12-15_inc.tar.gz -z
+ rm /tmp/work_backlist
Stuart Browne
Honored Contributor

Re: A strange script problem?

Simple test:

# touch /root/file1
# touch /root/file2

# echo /root/{file1,file2}
> /root/file1 /root/file2
# blah="/root/{file1,file2}"
# echo ${blah}
> /root/{file1,file2}
# blah='/root/{file1,file2}'
# echo ${blah}
> /root/{file1,file2}
# blah=/root/{file1,file2}
# echo ${blah}
> /root/{file1,file2}

So it would appear that expanding a {} based glob from a variable doesn't work.

Yet:

# blah='/root/file?'
# echo ${blah}
> /root/file1 /root/file2
# blah='/root/file*'
# echo ${blah}
> /root/file1 /root/file2

does work.

Limitation of the shell it seems. I can't find any reference to expansion of globs after being stored in a variable however.
One long-haired git at your service...
Chowroc
Occasional Advisor

Re: A strange script problem?

It means that i must specify every directory separately.

thank you very much.
Michael Schulte zur Sur
Honored Contributor

Re: A strange script problem?

Hi,

try this
WORK_DIR=`eval echo /mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}`

greetings,

Michael

Chowroc
Occasional Advisor

Re: A strange script problem?

Yes, just `eval echo /mnt/file/work/{doc03,__Download,gcc,java,js,lnx_sys_config,sh,src,sum,TeX,__webs}` works.

thanks for your help.
Michael Schulte zur Sur
Honored Contributor

Re: A strange script problem?

Hi,

if this helped, could you please take a moment and assign points to those who tried?

thanks,

Michael
http://forums1.itrc.hp.com/service/forums/helptips.do?#28