[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Output from date command - my little backup script



Adrian Levi wrote:
> Is the output from the date command a string or integer wrt date +%w?

The date command only produces string output.  %w produces one of the
characters 0, 1, 2, 3, 4, 5, 6.

> I'm trying to test a condition in my backup script where i want to
> match on day of week = 0
> 
> The program flow I am trying to achieve is" If the file exists and the
> day of the week is 0 then remove the file and set the day of the week
> to 0, otherwise set the day of the week to 0(perform Level 0 backup)".
>
> From man test i have 2 possibilities that i'm aware of, $backuplevel
> -eq 0 and $backuplevel = 0

In the shell -eq is a numeric comparison and = is a string
comparison.  Sometimes that makes no difference.  Sometimes that is a
critically important difference.  Although current shells will
complain if it isn't a number.

  00 -eq 0
  00 != 0
  02 -eq 2
  02 != 2

> if [ -f "$incrementalfile" ]
>   then
>     backuplevel=`date +%w`
>     if [ $backuplevel -eq 0]

You are missing a space after the 0 and before the ] and I am hoping
that is simply an email glitch.  But you must have a space there.

    if [ $backuplevel -eq 0 ]

>       then
>         rm "$incrementalfile" > /dev/null 2>&1

Redirecting the errors to /dev/null is bad.  Because you have
redirected the errors to /dev/null this may be unable to delete the
file, throwing an error, and you won't get the error.  Since you know
the file was there because of the -f test above it I think you should
just try to remove it, and not use rm -f either.

        rm "$incrementalfile"

>     fi
>   else
>     backuplevel=0
> fi

Is there a question in there?

> I have also tried if [ $backuplevel = 0 ]

Sure.  String comparison should work okay there.

> But neither seems to match, that is to say that the incremental file
> does not get deleted so it continues to do an 'other than 0 level
> backup.

Run the script with sh -x and see what commands are traced.

  sh -x ./myscript

Use echo to print out lines in question.

  echo "DEBUG: if [ $backuplevel -eq 0 ]"
  if [ $backuplevel -eq 0 ]

  echo "DEBUG: Removing with: rm -f $backuplevel"
  rm -f $backuplevel

You can use the command line to try things.

  $ test 0 -eq 0 && echo yes || echo no
  yes
  $ test 0 = 0 && echo yes || echo no
  yes

  $ test 00 -eq 0 && echo yes || echo no
  yes
  $ test 00 = 0 && echo yes || echo no
  no

> The command echo $backuplevel returns 0 but I don't know if it is an
> integer or a string, or is it both?

The shell is only working with strings.  It doesn't become a number
until it is forced into an integer comparison such as [ 0 -eq 0 ].

> According to [1] Bash variables are untyped

They are untyped because they are all strings.

> but I don't know why neither of them match and delete my file.

I don't see a problem (other than the missing space) but by using such
techniques as above you should be able to find it.  If it is the
missing space then you should have seen this error:

  $ [ 0 -eq 0]
  [: 1: missing `]'

So I assume it is a typo in your cut-n-paste.

Bob

Attachment: signature.asc
Description: Digital signature


Reply to: