Unix Shell - Case_insensitive search

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Case_insensitive search





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Case_insensitive search
Tim Frink

2007-12-19, 7:32 am

Hi,

in a bash script (actually configure.in) I have to set a variable
to a path name.

However, depending on the system configuration, the
path might be written in lower cases or use also upper
cases.

Currently I'm doing:
MYVAR=/path_name

so, the other possibility /path_Name will not be found.

What's the best way to search for a path name
in a non case-sensitive manner?

What I need is something like
MYVAR=case_insensitive(/path_name)

Thank you.

Tim




pk

2007-12-19, 7:32 am

Tim Frink wrote:

> Hi,
>
> in a bash script (actually configure.in) I have to set a variable
> to a path name.
>
> However, depending on the system configuration, the
> path might be written in lower cases or use also upper
> cases.
>
> Currently I'm doing:
> MYVAR=/path_name
>
> so, the other possibility /path_Name will not be found.
>
> What's the best way to search for a path name
> in a non case-sensitive manner?
>
> What I need is something like
> MYVAR=case_insensitive(/path_name)


Put that way, you can't do that. MYVAR must have a definite value, not many
at once. What you can do is check for the existence
of /path_name, /path_Name, etc. When you have determined the one that
actually exists, then you assign that to MYVAR.

Andrew Smallshaw

2007-12-19, 7:32 am

On 2007-12-19, Tim Frink <plfriko@yahoo.de> wrote:
>
> in a bash script (actually configure.in) I have to set a variable
> to a path name.
>
> However, depending on the system configuration, the
> path might be written in lower cases or use also upper
> cases.
>
> Currently I'm doing:
> MYVAR=/path_name
>
> so, the other possibility /path_Name will not be found.
>
> What's the best way to search for a path name
> in a non case-sensitive manner?


Since you're using bash:

shopt -s nocaseglob

--
Andrew Smallshaw
andrews@sdf.lonestar.org
Stephane Chazelas

2007-12-19, 7:32 am

On Wed, 19 Dec 2007 12:19:16 +0100, Tim Frink wrote:
> Hi,
>
> in a bash script (actually configure.in) I have to set a variable
> to a path name.
>
> However, depending on the system configuration, the
> path might be written in lower cases or use also upper
> cases.
>
> Currently I'm doing:
> MYVAR=/path_name
>
> so, the other possibility /path_Name will not be found.
>
> What's the best way to search for a path name
> in a non case-sensitive manner?
>
> What I need is something like
> MYVAR=case_insensitive(/path_name)

[...]

You mean you want to find the list of existing path that match
/path/to/some/dir case insensitively?

You could do something like

set /[pP][aA][tT][hH]/[tT][oO]/[sS][oO][mM][eE]/[dD][iI][rR]
for i
do
[ -d "$i" ] && set "$@" "$i"
shift
done
if [ "$#" -gt 1 ]; then
echo >&2 "more than one dir match"
exit 1
fi
if [ "$#" -eq 0 ]; then
echo >&2 "no matching dir"
exit 1
fi

Note that [ -d, returns true both for directories and symlinks
to directories.

--
Stephane
Rikishi 42

2007-12-19, 7:27 pm

On 2007-12-19, Tim Frink <plfriko@yahoo.de> wrote:
> in a bash script (actually configure.in) I have to set a variable
> to a path name.
>
> However, depending on the system configuration, the
> path might be written in lower cases or use also upper
> cases.
>
> Currently I'm doing:
> MYVAR=/path_name
>
> so, the other possibility /path_Name will not be found.
>
> What's the best way to search for a path name
> in a non case-sensitive manner?
>
> What I need is something like
> MYVAR=case_insensitive(/path_name)

Can't help you with that. But find can return the existing path to you, no
matter what case you use in your variable.

find start_dir/ -type d -iname "$MYVAR"

There are, of course, parameters to limit to which depth it'll search, and
such.


--
There is an art, it says, or rather, a knack to flying.
The knack lies in learning how to throw yourself at the ground and miss.
Douglas Adams
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com