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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to figure out a if insensitive file path exists or not?
# 1  
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.
Code:
ls  /app/weblogic/myserver4/logs/`hostname`/data/proc.pid

Output:
Code:
 /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.

Last edited by vbe; 11-30-2019 at 02:24 PM.. Reason: code tags please
# 2  
Instead of
Code:
hostname

you could try:
Code:
hostname|tr '[[:upper:]]' '[[:lower:]]'

# 3  
Quote:
Originally Posted by Scrutinizer
Instead of
Code:
hostname

you could try:
Code:
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
Code:
 /app/weblogic/myserver4/logs/MYHOSTTSTAPP1Cell044Data/data/proc.pid

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

Code:
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.

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

Note: I'm using wildcard "*"

Can you please suggest ?
# 4  
Ah so it is the other way around? The hostname is always lowercase, but the path may be uppercase?

Try something like:
Code:
cd $(ls -d "$(hostname)"* "$(hostname | tr '[[:lower:]]' '[[:upper:]]')"* 2>/dev/null)

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Quote:
Originally Posted by Scrutinizer
Ah so it is the other way around? The hostname is always lowercase, but the path may be uppercase?

Try something like:
Code:
cd $(ls -d "$(hostname)"* "$(hostname | tr '[[:lower:]]' '[[:upper:]]')"* 2>/dev/null)

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 ---

Quote:
Originally Posted by Scrutinizer
Ah so it is the other way around? The hostname is always lowercase, but the path may be uppercase?

Try something like:
Code:
cd $(ls -d "$(hostname)"* "$(hostname | tr '[[:lower:]]' '[[:upper:]]')"* 2>/dev/null)

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

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

Quote:
Originally Posted by Scrutinizer
Ah so it is the other way around? The hostname is always lowercase, but the path may be uppercase?

Try something like:
Code:
cd $(ls -d "$(hostname)"* "$(hostname | tr '[[:lower:]]' '[[:upper:]]')"* 2>/dev/null)

Works !! Thank you Scrutinizer for precise answer !!
# 6  
In ksh, as an alternative to tr, you can define lower case and upper case variables:
Code:
typeset -l lhostname
typeset -u uhostname
hostname=$(hostname)
lhostname=$hostname
uhostname=$hostname
echo "$hostname $lhostname $uhostname"

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 7  
Maybe try greping the directory name with the case insensitive switch (-i):
Code:
(
    cd /app/weblogic/myserver4/logs/ || exit 1
    d=$(ls | grep -i `hostname -s`)  || exit 1
    [[ -e "$d/data/proc.pid" ]]
)

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #925
Difficulty: Medium
The earliest versions of Unix time had a 32-bit integer incrementing at a rate of 60 Hz.
True or False?

8 More Discussions You Might Find Interesting

1. Programming

Makefile missing include path Although the path exists and defined

i have make file which i try to make them generic but it keeps to compline it missing include directory this is the makefile : CXX=g++ CPPFAGS= -Wall -O0 -g -std=c++14 INCLUDES = -I/home/vagrant/libuv/include -Isrc LIBS_DIRS = -L/home/vagrant/libuv/build LDFLAGS=... (7 Replies)
Discussion started by: umen
7 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. Shell Programming and Scripting

Case insensitive file name search and replace

I am trying to find case insensitive file names and then replace that particular file with other name. if then ls | grep -i "update" | xargs -I {} mv {} LineItems.csv echo "File moved from *update*" elif then ls | grep -i "priority" | xargs -I {} mv {} ... (1 Reply)
Discussion started by: ATWC
1 Replies

4. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. Programming

C - How to sheck if a path exists?

How do i check if a path exists in C? (5 Replies)
Discussion started by: omega666
5 Replies

7. Shell Programming and Scripting

color a figure in text file

Good morning, Can someone help to color a figure in columns if its exceeding a threshold using shell script . ex. I have file contains 5 columns showing a value in % I need to show the value in red if it exceed 90 % Appreciate your help (3 Replies)
Discussion started by: Bluetoot
3 Replies

8. Shell Programming and Scripting

How to check if all directories of file's path exists?

I wonder if the script below is possible to write somehow more efficiently. It seems to me the problem is very common.. CreateFolders() # parameter: name of file with relative path with regard to directory $project_root { echo $1 | awk '{ n=split($1, array, "/"); ... (2 Replies)
Discussion started by: MartyIX
2 Replies

Featured Tech Videos