Execute script in Folder with Lower & Upper case

My script test.sh requires an argument and it could be present on a list of servers under a folder with name either in upper or lower case like below:

For example:

i want a generic command for calling the test.sh script and pass one argument

Below is what i tried but it is failing

/u/[Mm]yfolder/scripts/test.sh argument1
Ouptut Error: No such file or directory

Note: ls -ltr /u/[Mm]yfolder/scripts/test.sh works fine and translates to the correct location.

Can you please suggest ?

What shell are you using to run the command:

ls -ltr /u/[Mm]yfolder/scripts/test.sh

?

What shell are you using to run the command:

/u/[Mm]yfolder/scripts/test.sh argument1

?

What operating system are you using to run both of these commands?

Answer: SunOS mymachine1 5.11 11.3 sun4v sparc sun4v

This works fine on Linux. The issue is with SunOS Solaris. I will appreciate a solution that works for Unix and Linux both. Can you please suggest ?

In your question, you say that the *name* of the folder can be upper- or lower case, so I guess this is just an example, where only the first letter of the folder name can be upper- or lower case.

A bash or ksh wildcard expression of all possible folder places for the script would be

/u/[Mm][yY][fF][oO][lL][dD][eE][rR]/scripts/test.sh

If there would be exactly one path matching this expression, you are done. However, in my experience, if you are working in an environment where you can't even predict the upper/lower case of the letters in a directory entry, you will sooner or later find a situation where you have both /u/myfolder/.... and, say, /u/MyFolder/...

Hence I would first store the list of all pathes matching this expression into an array, and if the array size is not equal one, I would abort with an error message.

I can state categorically that if the following are true:

  1. there is one and only one file that matches the shell pathname matching pattern /u/[Mm]yfolder/scripts/test.sh ,
  2. the pathname that matches that pattern is a regular file,
  3. the permissions on the pathname that matches that pattern allows execution by the person entering the following command, and
  4. the command /u/[Mm]yfolder/scripts/test.sh argument1 is typed into a bash or ksh interactive shell at a shell prompt; is entered as a simple command in a valid, non-interactive bash or ksh shell script; and all of the commands in that script are valid on all systems on which you are running that script

then the command will work on both a Solaris system and on a Linux system. If any of the above conditions are not met, there is no way that we can guess at which of the above conditions is causing your attempts to fail with what you have shown us.

Likely problems would include specifying Linux specific options to one or more commands in the script the you are executing that are not valid on your Solaris system, that you have specified a path in the script that you are executing that is valid on your Linux system but is not valid on your Solaris system, or giving that pathname matching pattern to something other than a shell that uses Bourne shell syntax.

With all of the details that you have hidden from us, there is no way for us to tell you what you need to do to fix your problem.

There are two files matching the pattern as shown below:

 
 $ ls -ltr /u/myfolder/scripts/test.sh
-rwxr-xr-x   1 user1  wladmin   15365 Mar 21  2012 /u/myfolder/scripts/test.sh

 $ ls -ltr /u/Myfolder/scripts/test.sh 
 -rwxr-xr-x   1 user1     wladmin   15365 Mar 21  2012 /u/Myfolder/scripts/test.sh
 

The issue is that it is not recognizing the argument being passed

$ /u/[mM]yfolder/scripts/test.sh status
Usage: /u/myfolder/scripts/test.sh { start | stop | restart | status }

When it remove [mM] it works fine as shown below.

$ /u/myfolder/scripts/test.sh status
Apache Server App is running (24281).

Please suggest.

????

But this is EXACTLY the problem I pointed out in my answer to your post. What exactly is not clear with the suggestion I gave?

Ronald

Expanding on what rovf has already said twice...

Run the command:

 ls -ltr /u/[Mm]yfolder/scripts/test.sh

on both systems. Choose and use the name that exists on both systems and use only that name on both systems.

On a system that has both files (apparently Solaris in your case), the command:

/u/[Mm]yfolder/scripts/test.sh start

tries (unsuccessfully) to run the command:

/u/Myfolder/scripts/test.sh /u/myfolder/scripts/test.sh start

since the string /u/myfolder/scripts/test.sh is not one of the valid arguments that the command /u/Myfolder/scripts/test.sh is expecting and it only wants one operand; not the two operands that you are giving it.

1 Like

Although there are two paths i can see that the lower case one [myfolder] is getting called. the Issue now is with regards to be able to pass the argument which is failing.

i wanted a one line command solution if possible else, i will go for a if condition check script.

---------- Post updated at 05:34 AM ---------- Previous update was at 05:32 AM ----------

Got it !! Understood. So i guess there is no one line solution.

If you refuse to find out beforehand which file exists on both systems (which should be trivially easy to do), you can always just use:

[ -x /u/Myfolder/scripts/test.sh ] && /u/Myfolder/scripts/test.sh start || /u/myfolder/scripts/test.sh start
1 Like

This is not correct.

While the maximum line length of a bash command line depends on the operating system where bash is running, I am not aware of systems where this length would be less than 32000. Hence, you can make any bash script a one-liner, as long as it has not more than this number of characters; you just have to replace every newline by a semicolon.

1 Like