execution of a script

Hi

i have a small script like this

  $ cat test.sh
#!/usr/bin/sh
name="ram"
echo ${1}
set 1 2 3 4 5 6 7 8 9 0 123 o870
echo $9
echo ${12}

when i am trying to execute like below i am not getting the output

jena samp_perl $ sh test.sh
test.sh: This:  not found

but when i executing like below i am able to get the output

jena samp_perl $ ./test.sh 1
1
9
o870

so may i know wt is the difference between two types of executions and why i am not able to get the output when i tried like this sh test.sh...

please help me in this.. thanks in advance

you have no value in $1 in the first invocation, thus ${1} fails and your script exits.

It works for me.

Even if ${1} in echo ${1} is not defined, the statement will result to echo and should output an empty line.

Can you paste the output of sh -x test.sh

--ahamed

What happens when you replace echo ${1} with echo "${1}" ?
-also-
What does ./test.sh produce?

I think you have two scripts called test.sh . One is in the current working directory (./test.sh) and the other one is somewhere which can be found though $PATH (sh test.sh).
To find the duff one:

whence test.sh
# Or if your Shell doesn't have the "whence" command:
type test.sh

Interestingly if your current directory is not in $PATH the "sh test.sh" won't find the test.sh in your current directory.

@Methyl, but according to the POSIX specification:

sh

Perhaps if it is a non-compliant shell ?

On any system I've come across you would only definitely execute a script in the current directory with "sh scriptname" or just "scriptname" if you had a "." at the start of $PATH.
We routinely remove the "." from $PATH because it is a security risk.

Have you come across a unix where "sh scriptname" works for the current directory when there is no "." in $PATH ?

Ps. Forget to suggest checking for an alias.

alias | grep "test.sh"
1 Like

Yes, I have never had a dot in $PATH for the reason you stated and I think "sh scriptname" always worked if scriptname was in the current directory (perhaps similar to any command that uses a file, where you also don't need to specify the path), as long as it is readable (even though I habitually execute with "sh ./scriptname").

Just "scriptname" is a different matter, that only works without a path if it is in $PATH and if it is both readable and executable (aliases aside).

Thanks methyl..:b:

As you said there was one more script ... with same name in other directory..
as . is not in the path sh test.sh is executing that script... thanks i missed the simple concept .. thank you very much...

And i have one more doubt.. is there any limit on the number of positional parameters tat we can set using "set" command

What OS are you using ragilla?

Just to mention it as well : the "type" shell built-in may also help to display which 'test.sh' would be called

type test.sh

The number of positional parameters which you can set depends on the Shell. In the original Bourne Shell it was 9 ($1-$9). I don't know of a low limit for any modern Bourne-type Shell (ksh, bash, Posix sh).

We'd need to know what Operating System you have in order to know what /usr/bin/sh means on your computer. Nowadays it's usually points to the Posix Shell in unix and to bash in Linux. However in say SUNOS it's the old Bourne Shell.

Hi Scrutinzer,

the OS which i am using is AIX, does the positional parameters depends on the OS.