2

I am running OS X 10.8 on a MBP 2011. I use some bash-scripts for tasks like backups of remote servers. One of the scripts contains this snippet:

#!/bin/bash
if [ "$PWD" != /Users/myuser/Documents/Backup ]
then
    echo "Wrong path: $PWD"
    exit 0
fi

When I execute this script in /Users/myuser/Documents/Backup (note the upper case B), I get this response:

Wrong path: /Users/myuser/Documents/backup

For some reason, the $PWD gets expanded to a lower case name. How can I alleviate this? My filesystem is not case sensitive: both cd Backup and cd backup work.

3

You have two choices really: convert $PWD to a consistent case before comparing to your other string (which should also have consistent case). Or drop case-sensitive comparisons.

Converting it to lower case before comparing:

#!/bin/bash
if [ `echo $PWD | tr [:upper:] [:lower:]` != /users/myuser/documents/backup ]
then
    echo "Wrong path: $PWD"
    exit 0
fi

Dropping case sensitive comparisons:

#!/bin/bash
shopt -s nocasematch
if [[ "$PWD" != /Users/myuser/Documents/Backup ]]
then
    echo "Wrong path: $PWD"
    exit 0
fi
  • I used the second option. Hower I needed to use the [[ ]] syntax for it to work. – Arne Jul 19 '13 at 8:05
  • I fixed the syntax. Good catch. – Ian C. Jul 19 '13 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .