Executing shell script files

Whats the difference between executing a file such as test.sh as:

./test.sh

as apposed to

sh test.sh

i've noticed that a simple while loop will not execute for the 2nd way of doing it, but will for the first. Also what do you guys all recom

Maybe read the numerous answers to your previous post?
http://www.unix.com/shell-programming-scripting/140887-simple-while-loop-problem-2.html

The theme throughout that was to find out what Shell you were running which could explain the anomolous behaviour and to actually see the script you were running, how you ran it, and any output messages.

We are unwilling to embark on another guessing game. I don't even think we managed to find out what Operating System you had - only that you were using "tcsh".

Methyl,
I apologise and I see where you are coming from. I understand and all I can say is that I am sorry that my knowledge when it comes to unix is not the same as yours, I apologise for ever thinking I could further it by asking questions, I apologise for having the nerve to come onto this forum in the first place, I apologise for not having told you clearly enough in post 9 on that thread that I was using Solaris, but most of all, I apologise for having found out the problem and found the solution, and then having the bloody nerve to ask why it works like that.
I hope with time you can forgive me, and that one day you will realise the world is also full of people who are not as brilliant as you. Next time please try not to put someone down, as thats how I feel. I have just got off the phone from my mother, adn she would like ot apologise for ever giving birth to me.
Regards

No offence linuxkid ... this is just about stating the problem clearly.

1) What is in test.sh ?

2) Example 1
Which shell is running at the time the command is issued?
Is it "tcsh"?
What does "ps" output?
What happened when the script ran?

3) Example 2
Which shell is started by running "sh" ?
A poster on your previous thread though that it was Bourne Shell. If so, this is a very old Solaris. Which version of Solaris?
What does "uname -a" output?
What happened when the script ran?

I was confused by you having "tcsh" because I though that is was the default shell on old Apple MACs and freeBSD. i.e. not Solaris.

The "C" shells (such as csh, tcsh) are quite different from other common Shells such as: Bourne Shell, Korn Shell, bash, Posx Shell etc..

For example, if the first line in test.sh is:

#! /usr/bin/ksh

Then when you run : ./test.sh , the script will be run in ksh (/usr/bin/ksh).

If you run : sh test.sh ,it will run by /usr/bin/sh (by default).

The syntax for "while" is quite different in "C" shell and Bourne shell. Thus a script can work in one shell and not another.

This script works on "csh" but fails in any Bourne shell. The Bourne versions we suggested in your previous thread all fail in "C" shell with syntax errors in the "while" statement.
Tried with "csh" (I don't have "tcsh" handy).

#!/bin/csh
# Output the word "Hello" exactly 20 times
set i=0
while ( $i < 20 )
       echo "Hello"
       set i=`expr $i + 1`
end

Hey Linuxkid,

There are many different types of shell implementations, ranging from ksh, to csh, to bash. Most coding these days, I'm told, is done on bash (you'll get more information if you wiki around).

If you run `sh test.sh', then the shell that runs test.sh is the shell that is invoked when sh is called. To find out which shell `sh' invokes, see what it directs to. Your `sh' command will probably sit in `/bin'. If you do `ls -al /bin/sh' (`ls -al' shows all files in the long listing format - see `man ls' for more), you will find that /bin/sh is probably a symlink (you will probably see an arrow like this `/bin/sh -> dash'). For more on symlinks, do `man ln'. A simple description is that symlinks are like shortcuts in Windows. They point to the *actual* command you invoke when you run sh.

Getting back to `sh', the word that follows the arrow, is the command or shell that is actually invoked when you run sh. Thus, for me, when I do `sh test.sh', what happens is that test.sh is run as a script in the *dash* shell.

Now, if test.sh starts with the line `#!/bin/bash', then that is a direction that it should be run using the *bash* shell only! However, this constraint is obeyed only when you run ./test.sh. That is, even if you have `#!/bin/bash', and you run `sh test.sh', test.sh will run in dash (or whatever your `sh' points) regardless of your `#!..' statement (this is sometimes called a shebang or hashbang. See Shebang (Unix) - Wikipedia, the free encyclopedia). The shebang is followed only if you do `chmod +x test.sh' and then run `./test.sh'.

I hope this helps?