Execute shell script without using sh

Hi Experts,

I want to execute shell script(on HP-UX) wihout specifying interpreter.

For e.g generally we use following command to execute shell

sh test.sh

but I want to execute it as

 
test.sh 

currently if I execute directly it as test.sh it is giving error Commnd Not Found.

However on another HP-UX box I am able to execute it without specifying sh and it is not giving any error.

Are there any environment variables that needs to be set?

It can't find it. You have to issue ./test.sh . It works on the other system most probably because the PATH variable contains a . or the directory you are in, which might be less probably.
Use following command to check what is in PATH:

echo $PATH

There might be a dot in it.

try running head test.sh on the script for which this works, the "shebang" line at the head of the script (hash bang, "#!" ) defines the interpreter to use, thus aq file starting #! /bin/bash will be run under the bash interpreter if the file is made executable and called.

updateWhat zaxxon said above, your PATH variable is what you were looking for.

The script needs to be executable, see man chmod .

Hi skrynesaver,

Thanks for the reply.

I tried that also as shown in below code. But still it is giving the same error, command not found :frowning:

% more test.sh
#!/bin/sh
echo "hi"
#
 
% test.sh
test.sh: Command not found.
%

---------- Post updated at 08:02 AM ---------- Previous update was at 08:00 AM ----------

Hi Scrutinizer,

Thanks for the reply.

Script already has executable permisson.

---------- Post updated at 08:04 AM ---------- Previous update was at 08:02 AM ----------

Hi zaxxon

So If I add my directory(directory where my shell resides) to PATH env variable,will I be able to execute it without using sh ?

I will check the PATH variable on the server on which it is running fine and post the result here.

Got it!
I can only get that "Command not found" message from /usr/bin/csh .
Your account is set up incorrectly for this job and your default shell is /usr/bin/csh not /usr/bin/sh . Either that or you should be writing the script in csh . Perhaps your Mentor or Tutor can advise.

As a quick temporary fix, first type sh at the csh prompt before trying any Bourne Shell commands.

Most posters on this board would recommend learning Bourne-type Shell scripting and avoid the "C Shell" because no system scripts are written in "C Shell" on any modern unix or Linux system.

In csh the equivalent of the Bourne $PATH environment variable is the $path variable. Type set at the command prompt to see the values of your environment variables (in either Shell).

I think the best advice was given in zaxxons second sentence: call the script by specifying its path. When you issue ./test.sh you specify a relative path with the dot standing for your current directory.
Adding the current directory (dot) to your path-variable can have undesired effects (imagine you mistype something and your current directory contains some 10.000 files...), but using paths is good practice anyway, especially when it comes to automate something using cron.
@methyl: unfortunately SAP uses csh for its os-users, at least the last time I had to work with it...

@cero
I did say modern ... lol.