Register
It is currently Thu Dec 14, 2017 7:09 am

Why does this "if" not work?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Jan 13, 2016 12:13 pm   

Joined: Wed Jan 13, 2016 10:41 am
Posts: 2
Hi,

I'm new here and also pretty new to Bash scripting. I have a problem I can't solve. Here is the script, following will be the problem description:
Code:
#!/bin/sh
Zeit=`date +%H`
Woche=`date +%U`
Wochentag=`date +%A`
BkpDir="Backup-$Wochentag-$Woche"
FBkpDir="$1/$BkpDir"
Datum=`date`
#echo "$BkpDir"

if [ -x $FBkpDir ]
then
  rm -rf $FBkpDir
else
   echo "The folder $FBkpDir does not exist." >> $LogFile
fi

mkdir $FBkpDir

Problem: Even though the folder $FBkpDir exists, the if always jumps to the "else" section and spits out the appropriate line into the logfile.

I have a very similar section later in the script where the variable has slightlly different contents, but is created almost in the same way, and there it works fine. Any ideas?


Top
 Profile  
 PostPosted: Wed Jan 13, 2016 12:45 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
If no argument given when called the path of backup directory evaluates to an absolute path /$BkpDir which is not likely to exist.

To construct the name there is only one command neccessary:
Code:
#!/bin/sh
BkpDir="Backup-"`date +'%A-%U'`"

# this MUST be checked:
FBkpDir="$1/$BkpDir"


Top
 Profile  
 PostPosted: Sat Jan 23, 2016 11:45 pm   

Joined: Wed Jan 13, 2016 10:41 am
Posts: 2
uhelp wrote:
Code:
# this MUST be checked:
FBkpDir="$1/$BkpDir"

Well, I do. I let the script write the variable into a log file. And there, both, the one that functions and the one that doesn't, are exactly the same. That's why I'm so puzzled.


Top
 Profile  
 PostPosted: Sun Jan 24, 2016 12:00 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
it'll fail for two reasons.
If you don't pass an argument #1 to the script.

And secondly if the directory name contains spaces.
Quote properly.
Read this http://www.grymoire.com/Unix/Quote.html

Quote each and every expansion like this var="$someOtherVar"

if you can't get it to work, post entire script AND the calling line.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP