How to figure out a if insensitive file path exists or not?

I use the below command with echo $? to determine if a file path exists.

ls  /app/weblogic/myserver4/logs/`hostname`/data/proc.pid

Output:

 /app/weblogic/myserver4/logs/myhostseven/data/proc.pid

The problem is that I have both AIX and Linux systems.

On some servers hostname is either Uppercase or Lowercase.

Thus, I need a posix solution that works on AiX and non-AiX L/Unix based system where i should be able to determine if the file exists irrespective of whether than server has hostname in UPPER or lower case.

A different approach / solution will also be appreciated.

Instead of

hostname

you could try:

hostname|tr '[[:upper:]]' '[[:lower:]]'

I understand the above solution would work on any AiX or non-Aix L/Unix system.

The blow PATH exists on my SYSTEM

 /app/weblogic/myserver4/logs/MYHOSTTSTAPP1Cell044Data/data/proc.pid

i tried the following but it does not work on AiX:

cd  /app/weblogic/myserver4/logs/`MYHOSTTSTAPP1 | tr '[[:upper:]]' '[[:lower:]]'`*
ksh: MYHOSTTSTAPP1:  not found

cd  /app/weblogic/myserver4/logs/`MYHOSTTSTAPP1* | tr '[[:upper:]]' '[[:lower:]]'`
ksh: MYHOSTTSTAPP1*:  not found

cd  /app/weblogic/myserver4/logs/`MYHOSTTSTAPP1 | tr '[[:upper:]]' '[[:lower:]]'`*
ksh: MYHOSTTSTAPP1*:  not found

The below does not throw any error but it does not take me inside the desired folder.

cd  /app/weblogic/myserver4/logs/MYHOSTTSTAPP1* | tr '[[:upper:]]' '[[:lower:]]'

Note: I'm using wildcard "*"

Can you please suggest ?

Ah so it is the other way around? The hostname is always lowercase, but the path may be uppercase?

Try something like:

cd $(ls -d "$(hostname)"* "$(hostname | tr '[[:lower:]]' '[[:upper:]]')"* 2>/dev/null)
1 Like

The path and the hostname both may be either upper or lower and we are not certain of that

In such a case how will my ls command to the file still work whether a lower or upper case is encountered ?

--- Post updated at 04:31 PM ---

Will back-ticks work instead of "$(" ?

--- Post updated at 04:52 PM ---

Works !! Thank you Scrutinizer for precise answer !!

In ksh, as an alternative to tr, you can define lower case and upper case variables:

typeset -l lhostname
typeset -u uhostname
hostname=$(hostname)
lhostname=$hostname
uhostname=$hostname
echo "$hostname $lhostname $uhostname"
2 Likes

Maybe try greping the directory name with the case insensitive switch (-i):

(
    cd /app/weblogic/myserver4/logs/ || exit 1
    d=$(ls | grep -i `hostname -s`)  || exit 1
    [[ -e "$d/data/proc.pid" ]]
)