0

I attempted to follow the answer on How do I compare two string variables in an 'if' statement in Bash?, but the accepted solution did not work. As you can see from the script below, my syntax follows the solutions on that question which gives me the error found here Bash syntax error: "[[: not found". And yes, I tried their solution too.

I have the following script where I am trying to delete all data from a directory. Before I delete all data, I want to compare a variable to a stdout value to verify I have the correct directory.

To avoid deleting all data from the wrong directory, I am attempting to compare the variable in the script with data stored in a *.ini.php file.

Here is the script:

    #!/bin/bash
    #--- script variables ---
     #base path of the timetrex web folder ending with a / character
     timetrex_path=/var/www/timetrex/
     timetrex_cache=/tmp/timetrex/

    #--- initialize script---
     #location of the base path of the current version
     ttrexVer_path=$(ls -d ${timetrex_path}*.*.*)/
     #the timetrex cache folder
     ttrexCache_path=$(sed -n 's/[cache]*dir =*\([^ ]*\)/\1/p' < ${ttrexVer_path}timetrex.ini.php)/
     echo $timetrex_cache
     echo $ttrexCache_path



 #clear the timetrex cache
    if [[ "$ttrexCache_path" = "$timetrex_cache" ]]
    then
      #path is valid, OK to do mass delete
      #rm -R $ttrexCache_path*
      echo "Success: TimeTrex cache has been cleared."
    else
      #path could be root - don't delete the whole server
      echo "Error: TimeTrex cache was NOT cleared."
    fi

The output of the script shows the following:

/tmp/timetrex/
/tmp/timetrex/
Error: Timetrex cache was NOT cleared.

As you can see from the output, both values are the same. However, when the script compares the two variables, it thinks they are different values.

Is this because the values are different types? Am I using the wrong comparison operator in the if statement? Thanks in advance.

Community
  • 1
  • 1
zDaniels
  • 600
  • 1
  • 10
  • 21
  • 1
    Try changing your second echo to `echo "|$ttrexCache_path|"`. I'll bet there's some whitespace in there. – Barmar Sep 06 '13 at 03:43
  • @fotanus The problem in that question was incorrect syntax in the `if` statement, this question doesn't have that problem. – Barmar Sep 06 '13 at 03:56
  • Thanks @Bamar, unfortunately adding pipes to the beginning and end of the variables gives the message `/tmp/timetrex/: Is a directory` for both variables. – zDaniels Sep 06 '13 at 04:08
  • ^^ You missed the double quotes around it. – anishsane Sep 06 '13 at 09:45

1 Answers1

3

After doing some more searching, I found that comparing the directory content was somewhat of an effective way of verifying that both variables pointed to the same directory.

Here is one way to do it:

#clear the timetrex cache
if [ "$(diff -q $timetrex_cache $ttrexCache_path 2>&1)" = "" ]
then
  #path is valid, OK to do mass delete
  rm -R ${ttrexCache_path}*
  echo "Success: TimeTrex cache has been cleared."
else
  #path could be root - don't delete the whole server
  echo "Error: TimeTrex cache was NOT cleared."
fi

If one of the directories is an invalid path, the condition catches the problem and doesn't try to delete the directory contents.

If the directory paths are different but point to valid directories, the condition statement sees that they have different contents and doesn't try to delete the directory contents.

If both directory paths are different and point to valid directories, and the contents of those directories is the same, then the script will delete everything in one of the directories. SO, this is not a foolproof method.

A second method can be seen at https://superuser.com/questions/196572/check-if-two-paths-are-pointing-to-the-same-file. The problem with this method is that this code does not know the difference between /tmp/timetrex and /tmp/timetrex/ which is important when wanting to append a * at the end of the path.

In the end, the best solution for this problem is quite simple. Changing the syntax of the original code is the only thing that needed to be done.

#clear the timetrex cache
if [ ${timetrex_cache} == ${ttrexCache_path} ] && [[ "${timetrex_cache: -1}" = "/" ]]
then
  #path is valid, OK to do mass delete
  rm -R ${ttrexCache_path}*
  echo "Success: TimeTrex cache has been cleared."
else
  #path could be root - don't delete the whole server
  echo "Error: TimeTrex cache was NOT cleared."
fi

Hope this is helpful to someone!

Community
  • 1
  • 1
zDaniels
  • 600
  • 1
  • 10
  • 21