PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > linux.debian.user > shell script and variable problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
linux.debian.user debian-user@lists.debian.org.

shell script and variable problem

Réponse
 
LinkBack Outils de la discussion
Vieux 02/12/2006, 03h10   #1
John L Fjellstad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut shell script and variable problem

I have the following script I use for backup:
#######
#!/bin/sh

BACKUPLIST=/etc/backup.lst
HOSTNAME=`hostname`
YEAR=`date +%Y`
MONTH=`date +%B`
BACKUPDIR=/srv/misc/backup/${YEAR}/${MONTH}
DATE=`date +%Y%m%d`

mkdir -m 2775 -p $BACKUPDIR

for entry in `cat $BACKUPLIST`; do
name=`echo $entry | cut -d':' -f1`
startdir=`echo $entry | cut -d':' -f2`
bkdir=`echo $entry | cut -d':' -f3`
exlist=`echo $entry | cut -d':' -f4`
if [ -n $exlist -a -f $exlist ]; then
cd $startdir; tar -X $exlist -cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
else
cd $startdir; tar cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
fi
done
##########

Now, /etc/backup.lst has a list of directories I want to backup, and
looks like this
john:/home:john:/home/john/backupex.lst
vcs:/srv:/srv/vcs:

That is, some entries have 4th field, some has not.
The problem is that my test
if [ -n $exlist -a -f $exlist ]
does not work. For some reason, this always evaluate to true, and then
the tar command fails. Why is this?

My understanding is -n will check if $exlist is empty or not, and if it
is, it will fail. And even if it doesn't, -f $exlist should fail since
it wouldn't be able to find that file.

--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
  Réponse avec citation
Vieux 02/12/2006, 04h00   #2
Ken Irving
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script and variable problem

On Fri, Dec 01, 2006 at 03:42:18PM -0800, John L Fjellstad wrote:
> I have the following script I use for backup:
> #######
> #!/bin/sh
>
> BACKUPLIST=/etc/backup.lst
> HOSTNAME=`hostname`
> YEAR=`date +%Y`
> MONTH=`date +%B`
> BACKUPDIR=/srv/misc/backup/${YEAR}/${MONTH}
> DATE=`date +%Y%m%d`
>
> mkdir -m 2775 -p $BACKUPDIR
>
> for entry in `cat $BACKUPLIST`; do
> name=`echo $entry | cut -d':' -f1`
> startdir=`echo $entry | cut -d':' -f2`
> bkdir=`echo $entry | cut -d':' -f3`
> exlist=`echo $entry | cut -d':' -f4`
> if [ -n $exlist -a -f $exlist ]; then
> cd $startdir; tar -X $exlist -cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
> else
> cd $startdir; tar cf - $bkdir | bzip2 -c > ${BACKUPDIR}/${HOSTNAME}-${name}-${DATE}.tar.bz2
> fi
> done
> ##########
>
> Now, /etc/backup.lst has a list of directories I want to backup, and
> looks like this
> john:/home:john:/home/john/backupex.lst
> vcs:/srv:/srv/vcs:
>
> That is, some entries have 4th field, some has not.
> The problem is that my test
> if [ -n $exlist -a -f $exlist ]
> does not work. For some reason, this always evaluate to true, and then
> the tar command fails. Why is this?


Not sure, but it'll work if you enclose the variable in quotes.

> My understanding is -n will check if $exlist is empty or not, and if it
> is, it will fail. And even if it doesn't, -f $exlist should fail since
> it wouldn't be able to find that file.


My guess is that the variable expands to nothing, and then the -n
test is applied to the next token it sees, perhaps the -a?

I've run into the same issue recently, and would like to somehow become
less baffled by the ins and outs of shell (bash) scripting. Presumably
there's a statement in bash(1) that would clarify what's going on.

Ken

--
Ken Irving, fnkci@uaf.edu


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
  Réponse avec citation
Vieux 02/12/2006, 04h30   #3
Ken Irving
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script and variable problem

On Fri, Dec 01, 2006 at 06:51:05PM -0900, Ken Irving wrote:
> > The problem is that my test
> > if [ -n $exlist -a -f $exlist ]
> > does not work. For some reason, this always evaluate to true, and then
> > the tar command fails. Why is this?

>
> Not sure, but it'll work if you enclose the variable in quotes.
>
> > My understanding is -n will check if $exlist is empty or not, and if it
> > is, it will fail. And even if it doesn't, -f $exlist should fail since
> > it wouldn't be able to find that file.

>
> My guess is that the variable expands to nothing, and then the -n
> test is applied to the next token it sees, perhaps the -a?
>
> I've run into the same issue recently, and would like to somehow become
> less baffled by the ins and outs of shell (bash) scripting. Presumably
> there's a statement in bash(1) that would clarify what's going on.


This may be off the mark, but browsing the bash source I see this
comment in subst.c:

/* Partially and wholly quoted strings which expand to the empty
string are retained as an empty arguments. Unquoted strings
which expand to the empty string are discarded. The single
exception is the case of expanding "$@" when there are no
positional parameters. In that case, we discard the expansion. */

This sounds like it matches the symptoms anyway, though it still
seems odd to me.

Ken

--
Ken Irving, fnkci@uaf.edu


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
  Réponse avec citation
Vieux 02/12/2006, 07h10   #4
John L Fjellstad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shell script and variable problem

Ken Irving <fnkci@uaf.edu> writes:

> Not sure, but it'll work if you enclose the variable in quotes.


That worked perfectly. Thanks.

--
John L. Fjellstad
web: http://www.fjellstad.org/ Quis custodiet ipsos custodes


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h42.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12734 seconds with 12 queries